I have a query like below

    Select top(10) *
    from myTable
    where CreateTime between '2014-2-3' and '2014-2-5' 
    and (Result is null or Result != 1) 

But when i build an index like below

    CREATE NONCLUSTERED INDEX [IX_Index] ON [dbo].[mytable] 
    (
[CreateTime] ASC,
    Result ASC
    )
    INCLUDE ( [ID])

([ID] is primary Key of mytable)

Sql server does not use that index

how can i speed up above query?

有帮助吗?

解决方案 2

Try to simply build the index like

CREATE NONCLUSTERED INDEX [IX_Index] ON [dbo].[mytable] 
(
   [CreateTime]
)

Since you are filtering on CreateTime, just build a covering index on that column. I don't think that adding Result to the index will help the query.

Also you don't have to include the primary key column in the index, SQL Server does that automatically.

其他提示

Try to use if you want to force sql server to use specific index.

Select top(10) *
from myTable WITH (INDEX(IX_Index))
where CreateTime between '2014-2-3' and '2014-2-5' 
and (Result is null or Result != 1) 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top