문제

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