SQLSERVER Union效率难题求解
遇到一个奇怪现象,百思不得奇解
sql是这样的:
第一个查询
with (树 a)
select count(*) from b where b.code = a.code and (b的过滤条件)
耗时0.x秒
第二个查询
with (树 a)
select count(*) from c where c.code = a.code and (c的过滤条件)
耗时也是0.x秒
但这样写就出问题了
with (树 a)
select count(*) from b where b.code = a.code and (b的过滤条件)
union
select count(*) from c where c.code = a.code and (c的过滤条件)
耗时 60+秒......
表数据 a 和 b大的一个是在50万行而已
检查执行路径,似乎c表的全表检索耗时71%,但看不出什么问题来