RJ.*_*RJ. 0 sql sql-server indexing
我不确定我在这里做错了什么,但我在一个有数百万行的表上运行查询.
查询是这样的:
select *
from dbo.table with (index (index_1), nolock)
where col1 = 15464
and col2 not in ('X', 'U')
and col3 is null
and col4 = 'E'
Run Code Online (Sandbox Code Playgroud)
索引看起来像这样:
CREATE NONCLUSTERED INDEX [index_1] ON [dbo].[table] ([col1], [col2], [col3], [col4]) WITH (FILLFACTOR=90) ON [PRIMARY]
GO
Run Code Online (Sandbox Code Playgroud)
此选择仍需要一分钟才能运行.我错过了什么?
对于此查询:
select *
from table
where col1 = 15464 and
col2 not in ('X', 'U') and
col3 is null and
col4 = 'E';
Run Code Online (Sandbox Code Playgroud)
最好的指标是table(col1, col4, col3, col2).查询应该自动使用索引,没有提示.
在根据where子句选择索引时,应首先放置相等条件 - 然后是一个具有不等式的列.索引的目的,in而not in在一般的不等式条件.
此外,如果混合数据类型,则有时不使用索引.所以,这假设col1是数字.