[英]Included index on table with 200+ million rows
我很难在包含略超过2亿条记录的表上创建包含索引。 表的结构如下:
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](60) NOT NULL,
[VatId] [int] NOT NULL,
[UserId] [int] NULL,
..some additional [int] columns
问题是当我执行以下查询时:
set statistics time on;
select top 20 [Id] from tblArticle where UserId = 7 order by Id desc;
set statistics time off;
..then,然后在〜27ms内检索到结果( UserId
列上存在non-clustered index
)。
但是,当我尝试选择其他列时,例如:
set statistics time on;
select top 20 [Id], [VatId] from tblArticle where UserId = 8 order by Id desc;
set statistics time off;
..then结果回到〜2,000ms。
查看执行计划: ..显然,“
Key Lookup
”在这里花费的时间最多。
我尝试在VatId
上创建包含的索引,例如:
CREATE NONCLUSTERED INDEX [NonClusteredIndex-UserIdIncVatId] ON [dbo].[tblArticle]
(
[UserId] ASC
)
INCLUDE ([VatId])
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF,
ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
..但是经过几个小时的运行,此查询最终出现错误
池中的内存不足(默认)
(我的SQL Server实例在8GB RAM,Core i7上运行)
我的问题:还有其他可能的技巧可以摆脱此Clustered Key Lookup
并提高性能吗?
非常感谢
编辑:列Id
具有聚集索引。
调用set statistics io on;
产生以下内容:
Table 'tblArticle'.
Scan count 1,
logical reads 730,
physical reads 1,
read-ahead reads 1351,
lob logical reads 0,
lob physical reads 0,
lob read-ahead reads 0.
尝试:
WITH cte AS (
select top 20 [Id]
from tblArticle
where UserId = 7
order by Id desc
)
SELECT t.[Id], t.[VatId]
FROM tblArticle t
JOIN cte
ON cte.[Id]= t.[Id]
我也来自另一个问题,在这里建议创建一个复合索引可能会有所帮助,因为不需要查找
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.