Click here to Skip to main content
Licence CPOL
First Posted 6 May 2008
Views 26,414
Bookmarked 19 times

INT versus GUID on a table's primary key

By | 6 May 2008 | Article
This article tries to show the pros and cons of using INT or GUID types on primary keys.
 
Part of The SQL Zone sponsored by
See Also

Introduction

This article shows the pros and cons of using the INT or GUID types on primary keys. Hope it helps you decide which suites your project best.

Using INT on primary keys

Advantages
  • Small amount of storage size (and integer is 4 bytes).
  • Increased readability, practical use in testing, and easy to remember.
  • Chronology of data; if two records are in ascending order, we can deduce that the second record was inserted after the first one.
  • Support for functions that return the last primary key generated (@@IDENTITY, SCOPE_IDENTITY()).
Disadvantages
  • Difficulty in the case of merging tables (the need to make remapping because the primary keys may be duplicated).
  • Hard to work with distributed tables.
  • Primary key in the form of INT/BIGINT is LUID, local unique identifier, which is only used locally in a table.
  • After a large number of operations (insert, delete), the counter for primary key can be reset, bringing the problem of chronology to 1.

Using GUID on primary keys

Advantages
  • Easy to merge tables.
  • Easy to work with distributed tables.
  • The primary key is uniquely identified in the entire system (the number of machines or tables doesn't matter).
  • Don't have problems when inserting a large number of operations.
Disadvantages
  • Bigger storage size (16 bytes - four times the size of an integer).
  • Don't have the chronology assumption.
  • Hard to remember and use in testing.
  • Don't benefit from a mechanism to obtain the last generated primary key (MS SQL Server).

My decision

I had a hard time deciding which solution suites best for my project, but I finally used GUIDs because of the advantages mentioned. But this happened only once, the rest of my projects have databases with INT primary keys. Hope someone finds this useful. Special thanks to Iancu Caputa for helping with this article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Liviu Holhos

Software Developer

Romania Romania

Member

Follow on Twitter Follow on Twitter
Web developer, designer and basketball enthusiast.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalok but PinmemberDonsw15:30 14 Jun '09  
QuestionComposite PK on two integer fields? Pinmemberpashaper22:22 11 Nov '08  
GeneralGUIDs fragmentation PinmemberGeyzerskiy Dmitriy1:45 8 May '08  
GeneralRe: GUIDs fragmentation [modified] PinmemberLiviu Holhos20:42 8 May '08  
GeneralRe: GUIDs fragmentation PinmemberMike Lang2:54 13 May '08  
GeneralRe: GUIDs fragmentation PinmemberLiviu Holhos20:05 13 May '08  
GeneralRe: GUIDs fragmentation PinmemberGeyzerskiy Dmitriy22:48 13 May '08  
QuestionWhat about Performance PinmemberDoug K. Wilson17:07 7 May '08  
AnswerRe: What about Performance PinmemberLiviu Holhos19:46 7 May '08  
Performance wasn't an issue for me, I mean the cost of maintenance vs. the performance is tricky. If you need speed and don't have the problems I've mentioned in Disadvantages section for INT's, you should probably use INT's.
I've tested once the performance fetching 1.000.0000 GUIDs on a 1.5 GHz with 1GB RAM. I used two techniques of generating the GUIDs:
1) NEWID()
2) DECLARE @aGuid UNIQUEIDENTIFIER
SET @aGuid = CAST(CAST(NEWID() AS BINARY(10)) + CAST(GETDATE() AS BINARY(6)) AS UNIQUEIDENTIFIER)
(algorithm named COMBs, by Jimmy Nilsson)
 
The results were the following:
1) fetching 1.000.0000 GUIDs was 10 times consuming than fetching 1.000.0000 INTs
2) fetching 1.000.0000 GUIDs was 1.2 times consuming than fetching 1.000.0000 INTs (almost the same)
 
After this test I was convinced that using GUIDs was the best solution for my project, since I've kinda needed the advantages specified. The best thing to do is take a shot and test this yourself, see what results you get.
GeneralRe: What about Performance Pinmemberastanton19781:50 20 May '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 7 May 2008
Article Copyright 2008 by Liviu Holhos
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid