我有下表:
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 行。