Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I get rid of clustered indexes on Guid columns

I am working on a database that usually uses GUIDs as primary keys.

By default SQL Server places a clustered index on primary key columns. I understand that this is a silly idea for GUID columns, and that non-clustered indexes are better.

What do you think - should I get rid of all the clustered indexes and replace them with non-clustered indexes?

Why wouldn't SQL's performance tuner offer this as a recommendation?

like image 920
cbp Avatar asked Nov 10 '08 11:11

cbp


People also ask

Should you always have a clustered index?

Yes, every table should have a clustered index. The clustered index sets the physical order of data in a table. You can compare this to the ordering of music at a store, by bands name and or Yellow pages ordered by a last name.

Is it good to use GUID as primary key?

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.

What is the disadvantage of clustered index?

Disadvantages of Clustered IndexExtra work for SQL for inserts, updates, and deletes. A clustered index takes longer time to update records when the fields in the clustered index are changed. The leaf nodes mostly contain data pages in the clustered index.

Which columns are not good for indexing?

A GUID column is not the best candidate for indexing. Indexes are best suited to columns with a data type that can be given some meaningful order, ie sorted (integer, date etc).


1 Answers

A big reason for a clustered index is when you often want to retrieve rows for a range of values for a given column. Because the data is physically arranged in that order, the rows can be extracted very efficiently.

Something like a GUID, while excellent for a primary key, could be positively detrimental to performance, as there will be additional cost for inserts and no perceptible benefit on selects.

So yes, don't cluster an index on GUID.

As to why it's not offered as a recommendation, I'd suggest the tuner is aware of this fact.

like image 173
Mike Woodhouse Avatar answered Sep 21 '22 20:09

Mike Woodhouse