我有以下sql查询,只需1秒执行:
select a.date, b.rate, c.type, a.value from
a inner join b on a.id = b.aid
c inner join b.id = c.bid
where a.name = 'xxx'
Run Code Online (Sandbox Code Playgroud)
但是我需要一个结果集来获得速率大于0的结果.所以当我将查询更改为此时需要7分钟才能执行:
select a.date, b.rate, c.type, a.value from
a inner join b on a.id = b.aid
c inner join b.id = c.bid
where a.name = 'xxx' and b.rate>0
Run Code Online (Sandbox Code Playgroud)
为什么这会使查询时间从1秒增加到7分钟?由于b表很大,我甚至尝试使用CTE,但这也没有改善性能.我认为使用CTE会有较小的值集来过滤,所以它应该更快但是没有帮助:
;with x as
(select a.date, b.rate, c.type, a.value from
a inner join b on a.id = b.aid
c inner join b.id = c.bid
where a.name = 'xxx') …Run Code Online (Sandbox Code Playgroud)