简体   繁体   中英

SQL Server uniqueidentifier Ids

I had a table which has its primary key column of type nchar.

I was about to replace that type, I added a new uniqueidentifier column as id and set it as primary key and then I reset the relations to other tables.

I did this because I thought that a uniqueidentifier is a better choice than a string for id, because this string id was typed by users. It is kind of a name attribute used as primary key.

But it seems to be slowed down when I execute queries, and I don't understand why.

Uniqueidentifier are bad choice for primary key in general. This is a radom unordered string and PK are clustered indexes (by default) it means if you insert that id the engine ill try to insert that in the middle of that table and to do it the engine ill split the table taking a lot of processing.

Try to use a int identity for you PK, that original strign column can remain as a non clustered unique index if you need it for historical.

And read this (very small) article. SQL Server central

PS:If you are not already signed up for this forum, you can do it for free.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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