简体   繁体   English

为什么这个SQL查询会进行密钥查找?

[英]Why does this sql query do a key lookup?

I have a table User with a bunch of indexes. 我有一个表用户有一堆索引。 One of them is a unique index on the AccountIdentifier column. 其中一个是AccountIdentifier列的唯一索引。

Since this is a unique index, why is a key lookup required in addition to the index seek? 由于这是一个唯一索引,为什么除了索引查找之外还需要键查找? The index seek tooltip reports that only one record is returned. 索引查找工具提示报告只返回一条记录。 I've also tried converting the index to a "unique key" type. 我也尝试将索引转换为“唯一键”类型。

alt text http://s3.amazonaws.com/brandonc.baconfile.com/pitchurs/tmp/capture_2.png alt text http://s3.amazonaws.com/brandonc.baconfile.com/pitchurs/tmp/capture_2.png

Because it is selecting * . 因为它正在选择*

It uses the non clustered index to locate the row(s) but then needs to go and fetch the data to return. 它使用非聚集索引来定位行,但随后需要去获取要返回的数据。

To avoid the bookmark lookup you would need to make the non clustered index a covering index (ideally by reducing the number of columns in the select list but possible also by adding new columns into the index itself or as included columns ) 要避免书签查找,您需要使非聚集索引成为覆盖索引(理想情况下,通过减少选择列表中的列数,但也可以通过向索引本身或作为包含列添加新

If you have a clustered index on the table the row locator in the non clustered index will include the clustered index key so it won't need a bookmark lookup to satisfy queries on just the AccountIdentifier and clustered index columns. 如果表上有聚簇索引,则非聚簇索引中的行定位符将包含聚簇索引键,因此不需要书签查找来仅满足对AccountIdentifier和聚簇索引列的查询。

密钥查找并不意味着“查找密钥”,而是“根据密钥查找行”。

See these articles and blog posts for some more background info on key lookups / bookmark lookups: 有关密钥查找/书签查找的更多背景信息,请参阅这些文章和博客文章:

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

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