这两个查询之间应该有选择还是可以使用任何一个?
内联查询
select
*,
(select count(*) from t2 where Id=t1.RefId) as Count
from t1
外部申请
select
*,
c.Count
from t1
outer apply (select count(*) as Count from t2 where Id=t1.RefId) c
答案 0 :(得分:1)
正如戈登在上面的评论中所述,代码中使用的外部应用程序应与非外部应用程序查询具有相同或相似的执行计划。但是,如果您有多个这样的标量子查询:
select
*,
(select Min(Val) from t2 where Id=t1.RefId) as Minimum
(select Max(Val) from t2 where Id=t1.RefId) as Maximum
(select count(*) from t2 where Id=t1.RefId) as Count
from t1
其中子查询之间唯一的区别是返回值,那么外部应用查询可能更有效:
select
*,
c.Minimum,
c.Maximum,
c.Count
from t1
outer apply (select Min(Val) as Minimum,
Max(Val) as Maximum,
count(*) as Count,
from t2 where Id=t1.RefId) c