我很难过。如果我删除查询未使用的索引,则查询从46秒减少到6秒。
以下是查询:
SELECT TOP (10)
[Extent1].[alarm_class_id] AS [alarm_class_id],
[Extent2].[domain_id] AS [domain_id],
[Extent3].[description] AS [description],
[Extent1].[list_id] AS [list_id],
[Extent2].[list_source] AS [list_source],
[Extent1].[list_detail_id] AS [list_detail_id],
[Extent1].[plate] AS [plate],
[Extent1].[notes] AS [notes],
[Extent1].[locale_id] AS [locale_id],
[Extent1].[end_date] AS [end_date],
[Extent2].[eoc_list_id] AS [eoc_list_id],
[Extent2].[list_type_id] AS [list_type_id]
FROM [dbo].[list] AS [Extent2]
INNER JOIN [dbo].[ron_list_detail] AS [Extent1] ON [Extent1].[list_id] = [Extent2].[list_id]
INNER JOIN [dbo].[domain_lookup] AS [Extent3] ON [Extent2].[domain_id] = [Extent3].[domain_id]
WHERE ([Extent2].[domain_id] IN (7)) AND (([Extent1].[end_date] IS NULL) OR ([Extent1].[end_date] > (SysDateTimeOffset())))
这是索引:
CREATE NONCLUSTERED INDEX [ron_CI_list_detail-list_id-plate]
ON [dbo].[ron_list_detail] ([list_id] ASC, [plate] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
GO
其他信息:除非我提供提示,否则查询计划不会使用此索引。如果我提供提示,则查询在一秒钟内。
我很困惑 - 想法? (会显示查询计划,但我无法弄清楚如何粘贴图像)
答案 0 :(得分:3)
我能想到的其中一个原因是其他指数的过时统计数据。以下是它可能发生的方式:
优化索引时,请确保所有索引都已重新刷新并更新所有统计信息(不一定使用fullscan
,默认模式通常也可以正常工作)。最有可能的是,在执行类似
alter index all on [dbo].[ron_list_detail] rebuild;
该计划可能会在不需要任何提示的情况下理顺。
另一件事是您的索引对于此特定查询看起来不是最佳的。我宁愿考虑domain_id, list_id
,因为plate
列仅作为输出返回,并且不会在任何连接条件中使用。
答案 1 :(得分:0)
原来它是查询中的TOP。
SELECT ...很快 SELECT TOP(1191)......很慢 SELECT TOP(1192)....很快
任何数字< = 1191都很慢 任何数字> = 1192都很快 没有顶部是快速的
怪异!!!