简体   繁体   English

包括所有列的聚集索引与非聚集索引(SQL Server)

[英]Clustered index vs. Non-clustered index including ALL columns (SQL Server)

A brief overview of the scenario: 场景的简要概述:

My database uses GUID's as primary keys, and, for what I've been reading, it seems like it's somewhat bad to have clustered indexes on GUID's (increases fragmentation, slows down inserts etc.). 我的数据库使用GUID作为主键,就我所读的内容而言,在GUID上具有聚集索引似乎有些不好(增加了碎片化,减慢了插入速度等)。 My project uses hibernate so we usually deal with jpql and fetching of full entities (a lot of queries end up turning into select p.* from person p [...] ) 我的项目使用休眠模式,因此我们通常会处理jpql并获取完整实体(很多查询最终select p.* from person p [...]变成select p.* from person p [...]

I would like to know if it would be a good approach to create non-clustered indexes covering all columns of a table (in order to avoid RID lookups, etc.). 我想知道这是否是创建覆盖表所有列的非聚集索引的一种好方法(以避免RID查找等)。

Thanks for the help, already! 感谢您的帮助!

No, it is not a good approach. 不,这不是一个好方法。 It sounds like you've already read that having the clustered index on a GUID is a bad idea. 听起来您已经读过,在GUID上具有聚集索引是一个坏主意。 Instead, create an int (or bigint, if necessary) identity field and make that the clustered index, unless another field makes more sense. 取而代之的是,创建一个int(或bigint,如有必要)标识字段,并使其成为聚集索引,除非另一个字段更有意义。 Then just create a nonclustered index on the GUID field, and let SQL do an RID lookup for each query that uses it. 然后,只需在GUID字段上创建一个非聚集索引,然后让SQL对使用该索引的每个查询进行RID查找。 This way you can avoid fragmentation and slow inserts/updates/deletes. 这样,您可以避免碎片和缓慢的插入/更新/删除。

Premature optimization is a bad idea. 过早的优化不是一个好主意。 Is the data size cost and effort added to inserts, updates, and deletes worth adding the index? 值得添加索引的数据大小成本和工作量是否添加到插入,更新和删除中? Unless you measure and test performance and the impact of your index, you won't know. 除非您测量和测试性能以及索引的影响,否则您将一无所知。 Look at the queries that read the table and see which, if any, are unacceptably long. 查看读取该表的查询,看看哪些长(如果有的话)太长了。 Then tune that specific query. 然后调整该特定查询。

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

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