Guid Primary /Foreign Key dilemma SQL Server
Asked Answered
T

6

11

I am faced with the dilemma of changing my primary keys from int identities to Guid. I'll put my problem straight up. It's a typical Retail management app, with POS and back office functionality. Has about 100 tables. The database synchronizes with other databases and receives/ sends new data.

Most tables don't have frequent inserts, updates or select statements executing on them. However, some do have frequent inserts and selects on them, eg. products and orders tables.

Some tables have upto 4 foreign keys in them. If i changed my primary keys from 'int' to 'Guid', would there be a performance issue when inserting or querying data from tables that have many foreign keys. I know people have said that indexes will be fragmented and 16 bytes is an issue.

Space wouldn't be an issue in my case and apparently index fragmentation can also be taken care of using 'NEWSEQUENTIALID()' function. Can someone tell me, from there experience, if Guid will be problematic in tables with many foreign keys.

I'll be much appreciative of your thoughts on it...

Tildi answered 8/6, 2010 at 9:12 Comment(0)
W
28

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 or BIGINT 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 separate INT 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:

  • Base Table with 1'000'000 rows (3.8 MB vs. 15.26 MB)
  • 6 nonclustered indexes (22.89 MB vs. 91.55 MB)

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.

So if you really must change your primary keys to GUIDs - try to make sure the primary key isn't the clustering key, and you still have an INT IDENTITY field on the table that is used as the clustering key. Otherwise, your performance is sure to tank and take a severe hit .

Wish answered 8/6, 2010 at 9:40 Comment(0)
R
3

Disadvantage of using guid over int:

String values are not as optimal as integer values for performance when used in joins, indexes and conditions. More storage space is required than INT.

The generated GUIDs should be partially sequential for best performance (eg, newsequentialid() on SQL 2005) and to enable use of clustered indexes

for more detail :

http://www.codinghorror.com/blog/2007/03/primary-keys-ids-versus-guids.html

http://blog.sqlauthority.com/2010/04/28/sql-server-guid-vs-int-your-opinion/

Reflex answered 8/6, 2010 at 9:17 Comment(1)
In SQL Server GUIDs are stored as 128-bit integers, not strings. But still, that is 4 times larger than an int.Geanine
P
1

My take is: Use autoincrement int as PK on the inside and have a unique Guid column on each primary table that you use to move rows across databases.

Join this column when you export data, do not export the int, and map it back to int when you import data.

Especially in large volumes, int are much smaller and faster.

Pittsburgh answered 8/6, 2010 at 9:41 Comment(0)
C
0

Using GUID or int for PK really depends on the scenario. There will be a performance hit changing from INT to GUID. GUID are 4 times bigger than an INT. There is a good article here about the pros and cons of using GUIDs.

Why do you have to change from Integers anyway?

Cortege answered 8/6, 2010 at 9:22 Comment(0)
G
0

GUIDs do have a performance impact relative to ints, but that impact may be minimal depending on your application so there's no way to be certain without testing. I once converted over an application from ints to GUIDs with some very large tables with many foreign keys doing both very heavy modifications and queries (on the order of hundreds of thousands of records turning over daily.) Things were a slower when run through a profiler, but there wasn't a noticeable difference from the user's perspective.

So the answer is "it depends." Like all things dealing with performance, you can't really be sure until you try it.

Geanine answered 8/6, 2010 at 9:24 Comment(0)
L
-1

bence eğer benzersiz bir kod kullanmamız gerekli durumlarda kullanılabilir. Ama performansa etkisinin göz önünde bulundurulmalıdır. Identıty bir pk ve fk olarak kullanırken performans açısından daha iyidir. Bu yüzden duruma bağlı olarak guid ya clustered key kullanımı yapabiliriz.

Leotaleotard answered 1/1, 2021 at 13:12 Comment(1)
Please post your answer in englishParamecium

© 2022 - 2024 — McMap. All rights reserved.