简体   繁体   English

为什么不使用SQL Server索引?

[英]Why SQL Server index is not used?

Most of my SQL queries have WHERE rec_id <> 'D'; 我的大多数SQL查询都具有WHERE rec_id <> 'D'; as for example: 例如:

 select * from Table1 where Field1 = 'ABC' and rec_id <> 'D'

I added index on REC_ID . 我在REC_ID上添加了索引。 But when I run this query and look at the execution plan, the new index (REC_ID) is not used. 但是,当我运行此查询并查看执行计划时,未使用新索引(REC_ID) The Execution plan shows Cost of 50% of nonClustered index Field1 and 50% RID Lookup (Heap) in Table1 . 执行计划显示的非聚簇索引的50%成本Field150% RID Lookup (堆)的Table1

Why the index REC_ID not used? 为什么不使用索引REC_ID

For this query: 对于此查询:

select *
from Table1
where Field1 = 'ABC' and rec_id <> 'D';

The best index is table1(Field1, rec_id) . 最好的索引是table1(Field1, rec_id)

However, your query may not be able to take advantage of an index. 但是,您的查询可能无法利用索引。 The goal of using an index for a where clause is to reduce the number of pages that need to be read. where子句中使用索引的目的是减少需要读取的页面数。 To understand the concept for non-clustered indexes on normal rows, you need some basic ideas: 要了解普通行上非聚集索引的概念,您需要一些基本的想法:

  • Records are stored on pages. 记录存储在页面上。
  • Each page is 8,192 bytes (slightly fewer used for data) and can store some number of records. 每页为8,192字节(用于数据的字节数略少),并且可以存储一定数量的记录。
  • The entire page is loaded into memory to read a record. 整个页面被加载到内存中以读取记录。

Say a record is about 80 bytes and there are 100 records on each page. 假设一条记录约为80个字节,并且每页上有100条记录。 If 10% of the records have Field1 = 'ABC' , then there will be about ten on each page. 如果10%的记录具有Field1 = 'ABC' ,则每页上将有大约十个记录。 That means that using the index would not (typically) save any page reads. 这意味着使用索引不会(通常)保存任何页面读取。 If 1% of the records match, then there is about one on each page. 如果有1%的记录匹配,则每页大约有一个。 The index still isn't helpful. 索引仍然没有帮助。

If only 0.01% of the records match (30 in your case), then only a fraction of the pages need to be read. 如果只有0.01%的记录匹配(在您的情况下为30),则仅需要读取一部分页面。 This is the sweet spot for indexes, and where they are really helpful. 这是索引的最佳选择,并且在它们真正有用的地方。

The number of matching records is called "selectivity". 匹配记录的数量称为“选择性”。 If the where clause is not very selective, then a non-clustered index will not be useful. 如果where子句不是非常有选择性,那么非聚集索引将无用。

Sometimes, a clustered index can be helpful in this situation. 有时,聚集索引在这种情况下可能会有所帮助。 However, clustered indexes may have more overhead for insert and certain update transactions. 但是,聚簇索引可能具有更多的insert和某些update事务开销。 So, the choice of index needs to be based on the queries being processed and other ways that the table is used. 因此,索引的选择需要基于正在处理的查询以及使用该表的其他方式。

SQL Server uses many factors to decide which indices to use. SQL Server使用许多因素来决定要使用的索引。 It must have determined that using the index on Field1 would be more effective that using the index on rec_id - meaning that field1={value} defines a smaller set than rec_id <> {value} based on data dispersion, etc., so there are fewer records to compare against the other condition. 必须确定使用Field1的索引比使用rec_id上的索引更有效-意味着基于数据分散等, field1={value}定义的集合比rec_id <> {value}小,因此,与其他条件进行比较的记录较少。 Note that the actual value is usually irrelevant in determining which index to use. 注意,实际值通常与确定使用哪个索引无关。

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

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