【问题标题】:Select query with covered Index resorting to index scan使用索引扫描选择覆盖索引的查询
【发布时间】: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


【解决方案1】:

您可能会因为Ordering by ID 而得到这样的结果。 尝试删除order by id。除其他外,您在订购时会得到非关系结果。将订购委托给您的客户端应用程序。

您可以使用 HINTS 强制选择使用索引,例如:

from CallRecords WITH (INDEX(_dta_index_CallRecords_5_565577053__K2_K37_K31_1_3_4_5.....)) 

【讨论】:

  • 订单肯定有问题
猜你喜欢
  • 2016-04-04
  • 2011-06-28
  • 1970-01-01
  • 2012-08-22
  • 2021-02-23
  • 1970-01-01
  • 2014-03-24
  • 2014-12-08
  • 2011-06-19
相关资源
最近更新 更多