我在 SQL Server 中有一个如下所示的表:
CREATE TABLE [dbo].[FCT_RawEvents](
[EquipID] [int] NOT NULL,
[EventTimeStamp] [int] NOT NULL,
[EventMilliSeconds] [smallint] NULL,
[EventID] [int] NOT NULL,
[EventOn] [bit] NOT NULL,
[JobID] [int] NULL,
[FirstEvent] [bit] NULL,
[OperatorId] [int] NULL,
[Suppressed] [bit] NULL,
[ManualOverride] [bit] NULL
)
这包含打开或关闭的事件(EventOn = True,EventOn = False)。现在我需要在给定时间获取所有“活动”(未抑制)的事件。我有一些有效的 SQL,但是由于该表有数百万行,因此运行速度非常慢(5 个equipId 需要 10 秒)。
这里是:
DECLARE @StartDateTime datetime = '2013/01/01'
DECLARE @csvEquipIds nvarchar(MAX) = '5,6,7,8'
DECLARE @StartTimeStamp int = dbo.GetSecondsFromDate(@StartDateTime)
DECLARE @StartMilliSeconds smallint = DATEPART(Ms, @StartDateTime)
DECLARE @EquipIds TABLE (EquipId int)
INSERT INTO @EquipIds(EquipId) SELECT EquipID FROM dbo.getEquipmentIDs(null,@csvEquipIds)
SELECT dbo.getDateFromSeconds(EventTimeStamp), * FROM
( SELECT re.EquipID,EventTimeStamp,EventMilliSeconds,EventID,eventon,
ROW_NUMBER() OVER (PARTITION BY re.EquipId,EventID ORDER BY EventTimeStamp DESC,EventMilliSeconds DESC) AS RowNo
FROM dbo.FCT_RawEvents re
JOIN @EquipIds eq
ON eq.EquipId = re.EquipID
WHERE (re.EventTimeStamp < @StartTimeStamp OR(re.EventTimeStamp = @StartTimeStamp AND re.EventMilliSeconds <= @StartMilliSeconds)) AND re.EventID > 0
AND (re.Suppressed IS NULL)-- OR re.Suppressed = 0)
) ev
WHERE RowNo = 1 AND EventOn = 1
ORDER BY EquipID,EventID, EventTimeStamp desc, EventMilliSeconds desc
执行计划表明 80% 的时间都花在排序上,也就是 Partition/Order 窗口函数。
我绝不是 INDEX 专家,但添加了以下内容:
CREATE CLUSTERED INDEX [IX_Clustered] ON [dbo].[FCT_RawEvents]
(
[EquipID] ASC,
[EventTimeStamp] DESC,
[EventMilliSeconds] DESC,
[EventID] ASC,
[EventOn] DESC
)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
CREATE NONCLUSTERED INDEX [IX_EquipEventTime] ON [dbo].[FCT_RawEvents]
(
[EquipID] ASC,
[EventID] ASC,
[EventTimeStamp] DESC,
[EventMilliSeconds] DESC
)
INCLUDE ( [EventOn]) 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
CREATE NONCLUSTERED INDEX [IX_Suppressed] ON [dbo].[FCT_RawEvents]
(
[Suppressed] 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
似乎排序正在扫描表的大部分,我真的希望它“回顾”直到它找到第一个匹配事件。
通过使用索引或改进 SQL,任何指针都将不胜感激。