下面的查询似乎没有使用在col1和col2上设置的索引。我不能期望包含“where exists”的SQL使用索引吗?
select a, b
from [dbo].[test] testA
where exists
(
select *
from [dbo].[test] as testB
where
testA.col1 = testB.col1
testA.col2 > testB.col2
)
答案 0 :(得分:1)
您需要添加多列index
create index index_test_all on test ("col1","col2","a","b");
select a, b
from [dbo].[test] testA
where exists (
select 1
from [dbo].[test] as testB
where testA.col1 = testB.col1
and testA.col2 > testB.col2
)
;