我应该加入还是应该联合
Should I JOIN or should I UNION
我有四个不同的 table 我正在尝试查询,第一个 table 是我将进行大部分查询的地方,但如果我在汽车中没有匹配项查看其他 table 中的其他字段以查看 VIN 参数是否匹配。
示例:
Select
c.id,
c.VIN,
c.uniqueNumber,
c.anotheruniqueNumber
FROM Cars c, Boat b
WHERE
c.VIN = @VIN(parameter),
b.SerialNumber = @VIN
现在说我在 Cars
中没有匹配,但在 Boat
中有匹配,我如何才能拉出匹配的 Boat 记录与 car 记录?我试图加入 tables,但是 tables 没有唯一标识符来引用其他 table.
我想找出用最少的代码搜索参数中所有 table 的最佳方法。我考虑过 UNION ALL
,但不确定这是否是我真正想要的,因为记录的数量可能会变得非常大。
我目前正在使用 SQL Server 2012
。提前致谢!
更新:
CAR table
ID VIN UniqueIdentifier AnotherUniqueIdentifier
1 2002034434 HH54545445 2016-A23
2 2002035555 TT4242424242 2016-A24
3 1999034534 AGH0000034 2016-A25
BOAT table
ID SerialNumber Miscellaneous
1 32424234243 545454545445
2 65656565656 FF24242424242
3 20023232323 AGH333333333
如果 @VIN
参数匹配船标识符的预期结果:
BOAT
ID SerialNumber Miscellaneous
2 65656565656 FF24242424242
某种 union all
可能是最好的方法——至少是使用正确索引最快的方法:
Select c.id, c.VIN, c.uniqueNumber, c.anotheruniqueNumber
from Cars c
where c.VIN = @VIN
union all
select b.id, b.VIN, b.uniqueNumber, b.anotheruniqueNumber
from Boats b
where b.VIN = @VIN and
not exists (select 1 from Cars C where c.VIN = @VIN);
这假设您在每个表中都有相应的列(您的问题暗示是正确的)。
not exists
的链可以随着您添加更多实体类型而变得更长。一种简单的解决方法是改为进行排序——假设您只需要一行:
select top 1 x.*
from (Select c.id, c.VIN, c.uniqueNumber, c.anotheruniqueNumber, 1 as priority
from Cars c
where c.VIN = @VIN
union all
select b.id, b.VIN, b.uniqueNumber, b.anotheruniqueNumber, 2 as priority
from Boats b
where b.VIN = @VIN
) x
order by priority;
order by
有一点开销。但坦率地说,从性能角度来看,排序 1-4 行是微不足道的。
我有四个不同的 table 我正在尝试查询,第一个 table 是我将进行大部分查询的地方,但如果我在汽车中没有匹配项查看其他 table 中的其他字段以查看 VIN 参数是否匹配。
示例:
Select
c.id,
c.VIN,
c.uniqueNumber,
c.anotheruniqueNumber
FROM Cars c, Boat b
WHERE
c.VIN = @VIN(parameter),
b.SerialNumber = @VIN
现在说我在 Cars
中没有匹配,但在 Boat
中有匹配,我如何才能拉出匹配的 Boat 记录与 car 记录?我试图加入 tables,但是 tables 没有唯一标识符来引用其他 table.
我想找出用最少的代码搜索参数中所有 table 的最佳方法。我考虑过 UNION ALL
,但不确定这是否是我真正想要的,因为记录的数量可能会变得非常大。
我目前正在使用 SQL Server 2012
。提前致谢!
更新:
CAR table
ID VIN UniqueIdentifier AnotherUniqueIdentifier
1 2002034434 HH54545445 2016-A23
2 2002035555 TT4242424242 2016-A24
3 1999034534 AGH0000034 2016-A25
BOAT table
ID SerialNumber Miscellaneous
1 32424234243 545454545445
2 65656565656 FF24242424242
3 20023232323 AGH333333333
如果 @VIN
参数匹配船标识符的预期结果:
BOAT
ID SerialNumber Miscellaneous
2 65656565656 FF24242424242
某种 union all
可能是最好的方法——至少是使用正确索引最快的方法:
Select c.id, c.VIN, c.uniqueNumber, c.anotheruniqueNumber
from Cars c
where c.VIN = @VIN
union all
select b.id, b.VIN, b.uniqueNumber, b.anotheruniqueNumber
from Boats b
where b.VIN = @VIN and
not exists (select 1 from Cars C where c.VIN = @VIN);
这假设您在每个表中都有相应的列(您的问题暗示是正确的)。
not exists
的链可以随着您添加更多实体类型而变得更长。一种简单的解决方法是改为进行排序——假设您只需要一行:
select top 1 x.*
from (Select c.id, c.VIN, c.uniqueNumber, c.anotheruniqueNumber, 1 as priority
from Cars c
where c.VIN = @VIN
union all
select b.id, b.VIN, b.uniqueNumber, b.anotheruniqueNumber, 2 as priority
from Boats b
where b.VIN = @VIN
) x
order by priority;
order by
有一点开销。但坦率地说,从性能角度来看,排序 1-4 行是微不足道的。