我刚刚开始研究如何通过索引优化我的查询,因为SQL数据正在快速增长。我查看了优化器如何通过SSMS中的执行计划处理我的查询,并注意到正在使用排序操作符。我听说排序操作符在查询中表明了一个糟糕的设计,因为通过索引可以过早地进行排序。下面是一个示例表和数据,类似于我正在做的事情:
IF OBJECT_ID('dbo.Store') IS NOT NULL DROP TABLE dbo.[Store]
GO
CREATE TABLE dbo.[Store]
(
[StoreId] int NOT NULL IDENTITY (1, 1),
[ParentStoreId] int NULL,
[Type] int NULL,
[Phone] char(10) NULL,
PRIMARY KEY ([StoreId])
)
INSERT INTO dbo.[Store] ([ParentStoreId], [Type], [Phone]) VALUES (10, 0, '2223334444')
INSERT INTO dbo.[Store] ([ParentStoreId], [Type], [Phone]) VALUES (10, 0, '3334445555')
INSERT INTO dbo.[Store] ([ParentStoreId], [Type], [Phone]) VALUES (10, 1, '0001112222')
INSERT INTO dbo.[Store] ([ParentStoreId], [Type], [Phone]) VALUES (10, 1, '1112223333')
GO
下面是一个查询示例:
SELECT [Phone]
FROM [dbo].[Store]
WHERE [ParentStoreId] = 10
AND ([Type] = 0 OR [Type] = 1)
ORDER BY [Phone]
我创建了一个非聚集索引来帮助提高查询速度:
CREATE NONCLUSTERED INDEX IX_Store ON dbo.[Store]([ParentStoreId], [Type], [Phone])
为了构建IX_Store索引,我从简单的谓词开始
[ParentStoreId] = 10
AND ([Type] = 0 OR [Type] = 1)
然后,我为ORDER BY和添加了[Phone]
列,以涵盖SELECT输出
因此,即使构建了索引,优化器仍然使用排序操作符(而不是索引排序),因为[Phone]
是在[ParentStoreId]
和[Type]
之后排序的。如果我从索引中删除[Type]
列并运行查询:
SELECT [Phone]
FROM [dbo].[Store]
WHERE [ParentStoreId] = 10
--AND ([Type] = 0 OR [Type] = 1)
ORDER BY [Phone]
当然,优化器不会使用排序操作符,因为[Phone]
是按[ParentStoreId]
排序的。
因此,问题是如何创建一个覆盖查询(包括[Type]
谓词)的索引,而不让优化器使用排序?
编辑:
我正在处理的表有2000多万行
https://stackoverflow.com/questions/6001197
复制相似问题