【发布时间】:2015-03-18 07:48:09
【问题描述】:
我有一个大约 31 列和大约 1000 万行的表
给定以下查询
Select top (1000)
[CallType],
[AttributedCallType],
[Extension],
[CallDurationBacking],
[AnswerTimeBacking],
[AccountCode],
[AuthorisationCode],
[RequestedRoute],
[SelectedRoute],
[SelectedTrunk],
[ConditionCode],
[CalibratedTime],
[StoredDialledNumber],
[AttributedSiteNumber],
[AttributedExtension],
[Cost],
[AttributedCountry],
[PlanName],
[ClassificationName]
from CallRecords
where ClientId = 15 and
Reported = 0 and
ResultCodeId > 1
order by id
还有下面的覆盖索引
CREATE NONCLUSTERED INDEX [_dta_index_CallRecords_5_565577053__K2_K37_K31_1_3_4_5_6_7_8_9_10_11_12_13_14_15_16_17_18_19_25_26_28_32_33_34_35_36_38_39] ON [dbo].[CallRecords]
(
[ClientId] ASC,
[Reported] ASC,
[ResultCodeId] ASC
)
INCLUDE ( [Id],
[CallType],
[AttributedCallType],
[Extension],
[CallDurationBacking],
[AnswerTimeBacking],
[AccountCode],
[AuthorisationCode],
[RequestedRoute],
[SelectedRoute],
[SelectedTrunk],
[ConditionCode],
[CalibratedTime],
[StoredDialledNumber],
[AttributedSiteNumber],
[AttributedExtension],
[Cost],
[AttributedCountry],
[PlanName],
[ClassificationName]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
为什么执行计划会使用聚集索引扫描?
我原以为 SQL 服务器会选择一个搜索
如果使用索引查找/键查找,我该如何强制?
【问题讨论】:
-
您错过了 INCLUDE 列列表中的“[AttributedCallType]”列?
-
将其添加到索引中,它仍在扫描
-
确保您的索引已启用且受信任:查询:SELECT * FROM sys.check_constraints;
-
@sdrzymala 在 sys.check_constraints 中没有任何返回
-
不要假设计划是错误的。如果条件不是很有选择性或表统计信息已过时,查询优化器将求助于扫描。尝试使用 INDEX 提示执行查询并比较两个查询的执行计划。此查询返回多少行?
标签: sql-server