我有一个数据库,其中只有一个Logs
包含列的表:
Id
(PK 集群,int,不为空),ServiceName
(nvarchar(255), not null) 和其他一些列,如TaskVariant
(nvarchar(1024)),Source
(nvarchar(1024))。
我在列上创建了一个索引INDEX_SERVICENAME
(非唯一,非聚集)ServiceName
,其中包括除Id, ServiceName
.
- 数据库大小约为 4 GB。
- 表包含大约 3 500 000 行。
- 表包含大约 1 400 000 行 Source = N'IpJob'。
- 表包含大约 2 400 000 行,TaskVariant = N'Ip'。
- 表包含大约 600 000 行,ServiceName = '1' 和 TaskVariant = N'Ip'。
- 表包含大约 350 000 行,ServiceName = '1' 和 Source = N'IpJob'。
问题:
我想通过分页或分页从表过滤中选择ServiceName
所有TaskVariant
列Source
。我的原始查询是用于选择最后 100 个过滤项Source
:
SELECT TOP (100)
[Filter1].[Id] AS [Id],
[Filter1].[Date] AS [Date],
[Filter1].[Data] AS [Data],
[Filter1].[ServiceName] AS [ServiceName],
[Filter1].[LogLevel] AS [LogLevel],
[Filter1].[StackTrace] AS [StackTrace],
[Filter1].[TaskVariant] AS [TaskVariant],
[Filter1].[Source] AS [Source],
[Filter1].[Message] AS [Message]
FROM ( SELECT [Extent1].[Id] AS [Id], [Extent1].[Date] AS [Date], [Extent1].[Data] AS [Data], [Extent1].[ServiceName] AS [ServiceName], [Extent1].[LogLevel] AS [LogLevel], [Extent1].[StackTrace] AS [StackTrace], [Extent1].[TaskVariant] AS [TaskVariant], [Extent1].[Source] AS [Source], [Extent1].[Message] AS [Message], row_number() OVER (ORDER BY [Extent1].[Id] DESC) AS [row_number]
FROM [dbo].[Logs] AS [Extent1]
WHERE (@serviceName = [Extent1].[ServiceName]) AND (@source = [Extent1].[Source])
) AS [Filter1]
WHERE [Filter1].[row_number] > 0
ORDER BY [Filter1].[Id] DESC
此查询的工作速度非常快~ 00:00:00 时间。
但是当我尝试按TaskVariant
查询过滤时,大约需要 00:02:18 分钟(下一个查询)。
SELECT TOP (100)
[Filter1].[Id] AS [Id],
[Filter1].[Date] AS [Date],
[Filter1].[Data] AS [Data],
[Filter1].[ServiceName] AS [ServiceName],
[Filter1].[LogLevel] AS [LogLevel],
[Filter1].[StackTrace] AS [StackTrace],
[Filter1].[TaskVariant] AS [TaskVariant],
[Filter1].[Source] AS [Source],
[Filter1].[Message] AS [Message]
FROM ( SELECT [Extent1].[Id] AS [Id], [Extent1].[Date] AS [Date], [Extent1].[Data] AS [Data], [Extent1].[ServiceName] AS [ServiceName], [Extent1].[LogLevel] AS [LogLevel], [Extent1].[StackTrace] AS [StackTrace], [Extent1].[TaskVariant] AS [TaskVariant], [Extent1].[Source] AS [Source], [Extent1].[Message] AS [Message], row_number() OVER (ORDER BY [Extent1].[Id] DESC) AS [row_number]
FROM [dbo].[Logs] AS [Extent1]
WHERE (@serviceName = [Extent1].[ServiceName]) AND (@taskVariant = [Extent1].[TaskVariant])
) AS [Filter1]
WHERE [Filter1].[row_number] > 0
ORDER BY [Filter1].[Id] DESC
问题:为什么第二个查询执行这么慢,如何解决这个问题?
非常感谢您的建议。
执行计划1