简体   繁体   English

如何将非聚集索引转换为覆盖索引

[英]how to turn non-clustered Index into covering indexs

I googled covering index and found: 我用谷歌搜索了索引,发现:

"A covering index is a special type of composite index, where all of the columns exist in the index." “覆盖索引是一种特殊类型的复合索引,其中所有列都存在于索引中。”

I understand the main purpose is to make non-clustered index don't lookup clustered Index, and for SQL Server, we can use 'INCLUDE' columns when creating an index, so SQL Server adds them at the leaf level of the index. 我知道主要目的是使非聚集索引不查找聚集索引,并且对于SQL Server,我们可以在创建索引时使用“ INCLUDE”列,因此SQL Server在索引的叶级添加它们。 So there is no need to look up via cluster-index. 因此,无需通过cluster-index查找。

But image we have an Customers table(CustID, FirstName, City) that has a clustered Index on CustID. 但是,我们有一个Customer表(CustID,FirstName,City),该表在CustID上具有聚簇索引。

if we create a non-clustered Index(called IX_FIRSTNAME) on FirstName column, and include this column as payload in the leaf node of the index and query as: 如果我们在FirstName列上创建非聚集索引(称为IX_FIRSTNAME),并将此列作为有效负载包括在索引的叶节点中,并查询为:

select FirstName from Customers where FirstName Like 'T*';

so in this case, there is no need to look up through clustered Index, so can IX_FIRSTNAME be considered as a covering index? 因此,在这种情况下,无需通过聚簇索引进行查找,那么IX_FIRSTNAME是否可以视为覆盖索引?

or it has to meet the requirement for all columns? 还是必须满足所有列的要求? and we need to create a non-clustered for all three columns to be a covering index? 并且我们需要为所有三列创建一个非聚集索引以作为覆盖索引?

There are two concepts here: 这里有两个概念:

  • clustered versus non-clustered indexes 聚簇索引与非聚簇索引
  • covering indexes 覆盖指标

A covering index is one that can satisfy the where clause in your query. 覆盖索引是可以满足查询中where子句的索引。 As you are likely to have more than one query running against your data, a given index may be "covering" for one query, but not for another. 由于您可能对数据运行多个查询,因此给定索引可能会“覆盖”一个查询,而不是另一个。

In your example, IX_FIRSTNAME is a covering index for the query 在您的示例中,IX_FIRSTNAME是查询的覆盖索引

select FirstName from Customers where FirstName Like 'T*'; ,

but not for the query 但不适用于查询

select FirstName from Customers where FirstName Like 'T*' and City Like 'London'; .

A lot of performance optimization boils down to "understand your queries, especially the where clause, and design covering indexes". 许多性能优化可以归结为“了解您的查询,尤其是where子句,并设计覆盖索引”。 Creating indexes for every possible combination of columns is a bad idea as indexes do have a performance cost of their own. 为列的每种可能的组合创建索引是一个坏主意,因为索引确实有其自己的性能成本。

The second concept is "clustered" versus "non-clustered" indexes. 第二个概念是“聚集”索引与“非聚集”索引。 This is more of a physical concern - with a clustered index, the data is stored on disk in the order of the index. 这更多是物理上的问题-使用聚簇索引时,数据按索引顺序存储在磁盘上。 This makes it great for creating an index on the primary key of a table (if your primary key is an incrementing integer). 这非常适合在表的主键上创建索引(如果主键是递增整数)。 In your example, you would create a clustered index on custid, which would be covering the following query: 在您的示例中,您将在custid上创建一个聚集索引,该索引将涵盖以下查询:

select FirstName from Customers where custid = 12

It would also help joins (eg from customer to order). 这也将有助于加入(例如,从客户到订单)。

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

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