[英]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.