[英]Speed up SQL Server 2008 query using index
我有一個如下查詢
Select top(10) *
from myTable
where CreateTime between '2014-2-3' and '2014-2-5'
and (Result is null or Result != 1)
但是當我建立如下的索引時
CREATE NONCLUSTERED INDEX [IX_Index] ON [dbo].[mytable]
(
[CreateTime] ASC,
Result ASC
)
INCLUDE ( [ID])
([ID]是mytable的主鍵)
SQL Server不使用該索引
如何加快查詢速度?
如果要強制sql server使用特定索引,請嘗試使用。
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)
嘗試像這樣簡單地建立索引
CREATE NONCLUSTERED INDEX [IX_Index] ON [dbo].[mytable]
(
[CreateTime]
)
由於您要過濾CreateTime
,因此只需在該列上構建覆蓋索引。 我認為將Result
添加到索引不會幫助查詢。
另外,您不必在索引中包含主鍵列,SQL Server會自動執行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.