我正在针对 SQL Server 2005 数据库构建一些自定义报告。该数据库属于我们运行的第 3 方管理应用程序。我要提取的数据不是该站点的主要用途,因此除了时间戳列之外,这些数据基本上没有被索引。目前,只涉及一个表——大约 7 亿行的表。因此,当我对它运行一个应该只返回 50 行的查询时,它必须轮询所有 7 亿行。
我希望加快速度,但不想索引我添加到 WHERE 子句的每一列——我不知道添加这么多索引最终会大大提高速度(或者我是错误的?)。所以我很好奇如果我不能向表中添加任何新索引,最好的做法是什么!
存储过程似乎不是最合适的。索引视图可能是最好的主意?想法?
这是表架构:
DeviceGuid (PK, uniqueidentifier, not null)
DeviceID (int, not null)
WindowsEventID (PK, int, not null) (indexed)
EventLog (varchar(64), not null)
EventSource (varchar(64), not null)
EventID (int, not null)
Severity (int, not null)
Description (nvarchar(max), not null)
TimeOfEvent (PK, datetime, not null) (indexed)
OccurrenceNbr (int, not null)
这是一个示例查询:
SELECT COUNT(*) AS NumOcc, EventID, EventLog, EventSource, Severity, TimeOfEvent, Description
FROM WindowsEvent
WHERE DeviceID='34818'
AND Severity=1
AND TimeOfEvent >= DATEADD(hh, DATEDIFF(hh, GETDATE(), GETUTCDATE()), '2010/10/27 12:00:00 AM')
AND TimeOfEvent <= DATEADD(hh, DATEDIFF(hh, GETDATE(), GETUTCDATE()), '2010/11/3 12:00:00 AM')
AND EventID<>34113
AND EventID<>34114
AND EventID<>34112
AND EventID<>57755
AND EventSource<>'AutoImportSvc.exe'
AND EventLog='Application'
GROUP BY EventID, EventLog, EventSource, Severity, Description
ORDER BY NumOcc DESC
也许查询很糟糕......它在 4.5 分钟内返回 53 行。