1

我有下表:

CREATE TABLE [Cache].[Marker](
    [ID] [int] NOT NULL,
    [SubID] [varchar](15) NOT NULL,
    [ReadTime] [datetime] NOT NULL,
    [EquipmentID] [varchar](25) NULL,
    [Sequence] [int] NULL
) ON [PRIMARY]

使用以下聚集索引:

CREATE UNIQUE CLUSTERED INDEX [IX_Marker_EquipmentID_ReadTime_SubID] ON [Cache].[Marker] 
(
    [EquipmentID] ASC,
    [ReadTime] ASC,
    [SubID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO

这个查询:

Declare @EquipmentId nvarchar(50)

Set @EquipmentId = 'KLM52B-MARKER'

SELECT TOP 1 
    cr.C44DistId,
    cr.C473RightLotId
From Cache.Marker m
    INNER JOIN Cache.vwCoaterRecipe AS cr ON cr.MarkerId = m.ID
Where m.EquipmentID = @EquipmentId And m.ReadTime >= '3/1/2013'
ORDER BY m.Id desc  

这是正在生成的查询计划:

在此处输入图像描述

我的问题是这个。为什么 Cache.Marker 表上的聚集索引不用于查找而不是扫描另一个索引?此外,SSMS 查询分析器建议我在 Marker.ReadTime 上添加一个索引,其中包含 ID 和 EquipmentID 列。

Cache.Marker 表中大约有 1M 行。

4

1 回答 1

3

你有多少个唯一的设备 ID?它可能决定 date 是一个更好的第一次查找(可能是错误的)。WITH( INDEX() )您可以通过语句强制它使用您的索引。FORCESEEK也可以提供帮助。我强烈推荐这样做,因为随着数据库增长到大型,索引行为是可以预测的。

SELECT TOP 1 
    cr.C44DistId,
    cr.C473RightLotId
From Cache.Marker m
     WITH ( INDEX( IX_Marker_EquipmentID_ReadTime_SubID ), FORCESEEK )
    INNER JOIN Cache.vwCoaterRecipe AS cr 
    ON cr.MarkerId = m.ID
Where m.EquipmentID = @EquipmentId And m.ReadTime >= '3/1/2013'
ORDER BY m.Id desc  
于 2013-03-07T20:44:35.150 回答