聚集和非聚集索引性能
我有一个巨大的表(约 1000 万行),在随机的 uniqueidentifier 列上有集群 PK。我对此表执行的最多操作是如果尚不存在具有相同 pk 的行,则插入新行。 (为了提高它的性能,我使用 IGNORE_DUP_KEY = ON 选项)
我的问题是
我可以完全摆脱这个表上的聚集索引吗?我的意思是,当我将一行插入具有聚集索引的表时,它应该重新排列数据的物理位置。也许最好删除聚集索引并在该列上创建非聚集索引以避免数据重新排列?
我无法在实时数据库上进行实验,因为如果性能下降,那将是一件令人头痛的事情。在测试数据库上,在使用聚集索引的情况下,我只能看到“聚集索引插入 100%”,在使用非聚集索引的情况下,我只能看到“表插入”+非聚集索引中的一些查找操作。
提前致谢
I have a huge table (~ 10 million rows) with clustered PK on a random uniqueidentifier column. The most operations I do with this table is inserting a new row if there is not yet a row with the same pk. (To improve performance of it I use IGNORE_DUP_KEY = ON option)
My question is
Can I get rid of clustered index at all on this table? I mean when I insert a row into a table with clustered index it should rearrange data physicaly. May be it is better to drop clustered index and create nonclustered index on that colum to avoid data rearrangement?
I can't do an experiment on the live db because if performance falls down it will be a headache. On the test db I can only see 'Clustered Index Insert 100%' in the case with clustered index and 'table insert' + some seeking opertations in the nonclustered index in the case with non-clustered index.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GUID 似乎是主键的自然选择 - 如果您确实必须这样做,您可能会争论将其用作表的主键。我强烈建议不要这样做,即使用 GUID 列作为聚集键,这是 SQL Server 默认执行的操作,除非您明确告诉它不要这样做。
您确实需要区分两个问题:
1)主键是一个逻辑构造 - 唯一且可靠地标识表中每一行的候选键之一。这实际上可以是任何东西——一个 INT、一个 GUID、一个字符串——选择对你的场景最有意义的。
2)聚集键(定义表上“聚集索引”的一列或多列) - 这是一个物理与存储相关的东西,这里是一个小的,稳定、不断增加的数据类型是您的最佳选择 -
INT
或BIGINT
作为您的默认选项。默认情况下,SQL Server 表上的主键也用作聚簇键 - 但不必如此!我个人看到,当将之前基于 GUID 的主键/聚集键分解为两个单独的键时,性能得到了巨大的提升 - GUID 上的主(逻辑)键和 GUID 上的聚集(排序)键一个单独的
INT IDENTITY(1,1)
列。作为 Kimberly Tripp - 女王索引 - 和其他人已经说过很多次 - GUID 作为集群键并不是最佳的,因为由于它的随机性,它将导致大量页面和索引碎片以及通常较差的性能。
是的,我知道 - SQL Server 2005 及更高版本中有
newsequentialid()
- 但即便如此,它也不是真正完全顺序的,因此也遇到了与 GUID 相同的问题 - 只是稍微不那么突出,所以。然后还有另一个问题需要考虑:表上的聚集键也将添加到表上每个非聚集索引的每个条目中 - 因此您确实希望确保它尽可能小。通常,具有 2+ 十亿行的 INT 对于绝大多数表来说应该足够了 - 与作为集群键的 GUID 相比,您可以在磁盘和服务器内存中节省数百兆字节的存储空间。
快速计算 - 使用 INT 与 GUID 作为主键和聚集键:
总计:25 MB 与. 106 MB - 而且仅在一个表上!
还有一些值得深思的东西 - Kimberly Tripp 写的很棒的东西 - 读它,再读它,消化它!这确实是 SQL Server 索引的福音。正如她在“聚集索引辩论继续”中所展示的那样,拥有一个良好的聚集键(而不是没有或不好的聚集键)确实可以加快几乎所有数据库操作的速度!这是一个好主意 - 但它必须是一个好的集群键......
Marc
GUIDs may seem to be a natural choice for your primary key - and if you really must, you could probably argue to use it for the PRIMARY KEY of the table. What I'd strongly recommend not to do is use the GUID column as the clustering key, which SQL Server does by default, unless you specifically tell it not to.
You really need to keep two issues apart:
1) the primary key is a logical construct - one of the candidate keys that uniquely and reliably identifies every row in your table. This can be anything, really - an INT, a GUID, a string - pick what makes most sense for your scenario.
2) the clustering key (the column or columns that define the "clustered index" on the table) - this is a physical storage-related thing, and here, a small, stable, ever-increasing data type is your best pick -
INT
orBIGINT
as your default option.By default, the primary key on a SQL Server table is also used as the clustering key - but that doesn't need to be that way! I've personally seen massive performance gains when breaking up the previous GUID-based Primary / Clustered Key into two separate key - the primary (logical) key on the
GUID
, and the clustering (ordering) key on a separateINT IDENTITY(1,1)
column.As Kimberly Tripp - the Queen of Indexing - and others have stated a great many times - a GUID as the clustering key isn't optimal, since due to its randomness, it will lead to massive page and index fragmentation and to generally bad performance.
Yes, I know - there's
newsequentialid()
in SQL Server 2005 and up - but even that is not truly and fully sequential and thus also suffers from the same problems as the GUID - just a bit less prominently so.Then there's another issue to consider: the clustering key on a table will be added to each and every entry on each and every non-clustered index on your table as well - thus you really want to make sure it's as small as possible. Typically, an INT with 2+ billion rows should be sufficient for the vast majority of tables - and compared to a GUID as the clustering key, you can save yourself hundreds of megabytes of storage on disk and in server memory.
Quick calculation - using INT vs. GUID as Primary and Clustering Key:
TOTAL: 25 MB vs. 106 MB - and that's just on a single table!
Some more food for thought - excellent stuff by Kimberly Tripp - read it, read it again, digest it! It's the SQL Server indexing gospel, really. As she shows in her "The Clustered Index Debate contiues", having a good clustering key (as opposed to none or a bad one) really does speed up pretty much all database operations! It's a good idea - but it has to be a good clustering key....
Marc