简体   繁体   English

当使用 NEWSEQUENTIALID() 作为主键时,我的聚集索引应该是什么?

[英]When using NEWSEQUENTIALID() as primary key, what should be my clustered index?

I'm using newsequentialid to generate GUIDs for my primary key in a table.我正在使用 newsequentialid 为表中的主键生成 GUID。

According to the documentation ( https://docs.microsoft.com/en-us/sql/t-sql/functions/newsequentialid-transact-sql?view=sql-server-ver15 ), Sequential GUIDs aren't guarantined to be generated in order.根据文档( https://docs.microsoft.com/en-us/sql/t-sql/functions/newsequentialid-transact-sql?view=sql-server-ver15 ),不保证顺序 GUID按顺序生成。

After restarting Windows, the GUID can start again from a lower range, but is still globally unique重启 Windows 后,GUID 可以从较低的范围重新开始,但仍然是全局唯一的

Basically, they're in order until you reboot the machine.基本上,它们是有序的,直到您重新启动机器。

For autoincrement primary key, it makes sense for it to be the clustered index cause it's guaranteed an inserted row will be at the end.对于自动增量主键,它是聚集索引是有意义的,因为它保证插入的行将在末尾。

For a GUID primary key, it doesn't make sense for it to be the clustered index cause it's random it's unlikely an inserted row will be at the end.对于 GUID 主键,将其作为聚集索引没有意义,因为它是随机的,插入的行不太可能位于末尾。

What about for a sequential GUID primary key?那么顺序 GUID 主键呢? Should the primary key be the clustered index or should I try to find another column like a DateCreated field?主键应该是聚集索引,还是应该尝试查找另一列,例如 DateCreated 字段? The problem is fields like DateCreated isn't going to be a unique field.问题是像 DateCreated 这样的字段不会是唯一字段。 If I don't have any fields that are unique fields, what should I make as the clustered index?如果我没有任何唯一字段,我应该将什么作为聚集索引?

Sequential GUIDs are much safer for clustered indexes than non-sequential GUIDs.对于聚集索引,顺序 GUID 比非顺序 GUID 更安全。 In general, databases are not restarted particularly often.通常,数据库不会特别频繁地重新启动。 It is true that restarting can result in page splits and fragmentation, but that is usually not too big a consideration because restarting is rare.重新启动确实会导致页面拆分和碎片,但这通常不是太大的考虑因素,因为重新启动很少见。

That said, the primary key does not need to be the clustered index key.也就是说,主键不需要是聚集索引键。 You can have an identity column or creation date/time as the clustered index, pretty much eliminating this issue.您可以将identity列或创建日期/时间作为聚集索引,几乎可以消除此问题。

I wrote a long post about this a while ago.不久前我写了一篇关于这个的长文。 The TL/DR is that using a sequential GUID as a clustered index key is fine. TL/DR 是使用顺序 GUID 作为聚集索引键很好。 The GUIDs are actually inserted in the middle of the index, but having a small number (here one) mid-index insertion point does not cause expensive page splits or lead to harmful fragmentation. GUID 实际上是插入到索引的中间,但是具有少量(这里是一个)中间索引插入点不会导致昂贵的页面拆分或导致有害的碎片。

Good Page Splits and Sequential GUID Key Generation 良好的页面拆分和顺序 GUID 密钥生成

This same behavior applies to using a compound key as clustered index, where the leading key column has lower cardinality.同样的行为适用于使用复合键作为聚集索引,其中前导键列具有较低的基数。 Eg (CustomerId,TransactionId).例如(客户 ID、交易 ID)。 Each CustomerId will have a half-full page with space for the next TransactionId, and when that page fills a new one is allocated.每个 CustomerId 将有一个半满的页面,其中包含下一个 TransactionId 的空间,当该页面填满时,将分配一个新页面。

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

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