我有下表:
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
)
CREATE NONCLUSTERED INDEX [IX_Marker_Id] ON [Cache].[Marker]
(
[ID] ASC
)
INCLUDE ( [EquipmentID])
CREATE NONCLUSTERED INDEX [IX_Marker_SubID] ON [Cache].[Marker]
(
[SubID] ASC
)
INCLUDE ( [EquipmentID],[ReadTime])
我正在执行以下查询:
Declare @EquipmentId varchar(50)
SELECT TOP 1
C44DistId,
C471DistId,
C473RightLotId
From Cache.vwMarker m
INNER JOIN Cache.vwCoaterRecipe AS cr ON cr.MarkerId = m.ID AND M.EquipmentID = @EquipmentId
Order By m.Id Desc
查询计划如下所示:
Marker 表大约有 7M 行,因此扫描非常昂贵。7M 行中只有 24 个唯一的 EquipmentId 值,这可能是进行扫描的原因。
此查询通常需要 40 多秒才能执行,具体取决于服务器负载。我一直在想办法(通过创建某种索引)让 SQL Server 在 Marker 表上进行搜索。我什么都想不出来。也许没有办法,但我想我会先问。