简体   繁体   English

主键和索引-查询性能

[英]Primary Keys and Indexes - Query Performance

I have a table which contains over 1 million records. 我有一个包含超过一百万条记录的表。 There are no keys defined on the table. 表格上没有定义键。 However we do have clustered and non-clustered index defined. 但是,我们确实定义了聚集索引和非聚集索引。

I want to know if there will be actually a performance improvement when I do a SELECT query and include the columns where we have index created in the WHERE clause? 我想知道在执行SELECT查询并包括在WHERE子句中创建索引的列时,实际上是否会提高性能?

Is having primary / unique key a must to leverage the true benefits of Indexes? 要使用索引的真正优势,是否必须拥有主键/唯一键?

Table details: 表格详情:

  • 260 columns 260栏
  • No Keys 没有钥匙
  • No identity column defined. 未定义身份列。
  • No rowguidcol column defined. 未定义rowguidcol列。
  • 1 clustered index and 1 non clustered index 1个聚集索引和1个非聚集索引

Query: 查询:

select * 
from employee(nolock)
where employeeID = '15' and employeeType = 'FT' 

Personally based on my experience the clustered index is best when it's narrow, unique and non-changing. 根据我的经验,聚集索引在狭窄,唯一且不变的情况下是最佳的。 The smaller the clustered key (non-unique clustered indexes add a 4 byte "uniquifier") the better because it creates a denser leaf page on the subsequent non-clustered indexes. 聚簇键越小(非唯一聚簇索引添加4字节“ uniquifier”)越好,因为它会在随后的非聚簇索引上创建更密集的叶子页。

Assuming you currently have a non-unique clustered index on an integer (assuming employeeID is an integer), then the size of each key is effectively doubled do to the uniquifier (4 from the integer and 4 from the uniquifier). 假设您当前在整数上有一个非唯一的聚集索引(假设employeeID是一个整数),则每个键的大小实际上将加倍到唯一化符(整数中的4个,唯一符中的4个)。

The difference between a non-unique/unique clustered index would be the non-clustered leaf rows fitting, respectively, 1000 and 2000 rows to a page. 非唯一/唯一聚集索引之间的差异将是非聚集叶行分别适合页面的1000和2000行。 A denser non-clustered leaf page means traversing the index levels faster as well as saving on disk space (1000000 rows at 4byte saved a row amounts to roughly 3 gigs per non-clustered index). 密集的非群集叶子页意味着更快地遍历索引级别以及节省磁盘空间(4字节中的1000000行保存每个非群集索引大约3 gigs行)。

A lot of this is based on assumptions on your table. 其中很多是基于您桌上的假设。

TLDR: A narrow clustered index key is best; TLDR:最好使用窄的聚集索引键; make it a primary key to enforce uniqueness and prevent a wasteful 4byte uniquifier. 使其成为强制执行唯一性并防止浪费的4字节uniquifier的主键。

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

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