简体   繁体   English

包含200+百万行的表的索引

[英]Included index on table with 200+ million rows

I'm having difficulties creating an included index on table that contains slightly over 200 million records. 我很难在包含略超过2亿条记录的表上创建包含索引。 The structure of table is as follows: 表的结构如下:

    [Id] [int] IDENTITY(1,1) NOT NULL,
  [Name] [nvarchar](60) NOT NULL,
 [VatId] [int] NOT NULL,
[UserId] [int] NULL,
..some additional [int] columns

The problem is that when I do a following query: 问题是当我执行以下查询时:

set statistics time on;

 select top 20 [Id] from tblArticle where UserId = 7 order by Id desc;

set statistics time off;

..then the result is retrieved in ~27ms (there is a non-clustered index on column UserId ). ..then,然后在〜27ms内检索到结果( UserId列上存在non-clustered index )。

However when I try to select additional columns, eg: 但是,当我尝试选择其他列时,例如:

set statistics time on;

 select top 20 [Id], [VatId] from tblArticle where UserId = 8 order by Id desc;

set statistics time off;

..then the result is back in ~2,000ms. ..then结果回到〜2,000ms。

Looking at the execution plan: 查看执行计划: 在此处输入图片说明 ..obviously, the Key Lookup is what takes the most of the time here. ..显然,“ Key Lookup ”在这里花费的时间最多。

I have tried to create an included index on VatId , such as: 我尝试在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)

..but after several hours of running this query ends up with error ..但是经过几个小时的运行,此查询最终出现错误

Insufficient memory in pool (default) 池中的内存不足(默认)

(My SQL Server instance runs on 8GB RAM, Core i7) (我的SQL Server实例在8GB RAM,Core i7上运行)

My question: Are there any other possible tricks to get rid of this Clustered Key Lookup and improve the performance? 我的问题:还有其他可能的技巧可以摆脱此Clustered Key Lookup并提高性能吗?

Many thanks 非常感谢

EDIT: The column Id has a clustered index. 编辑:Id具有聚集索引。

Calling the set statistics io on; 调用set statistics io on; produces the following: 产生以下内容:

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.

EDIT 2: Just to make the full picture, execution plan with hints: 编辑2:只是为了使整体图,带有提示的执行计划: 在此处输入图片说明

Try: 尝试:

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]

Also I just came from another question where suggest create a composited index may help because wont need do the look up 我也来自另一个问题,在这里建议创建一个复合索引可能会有所帮助,因为不需要查找

oracle Update comparing Varchar oracle更新比较Varchar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM