GUID Vs Int data type as primary key

Recently one of my friend ask me when I should go for GUID and When I should go for Int as primary key in table. So decided to write a blog post for it. Here are advantages and disadvantage of the GUID and INT. 

INT Data Type:

Advantages:

  1. Its required small space in terms of the storage it will only allocates 4 bytes to store data.
  2. Insert and update performance will be faster then the GUID. It will increase the performance of the application.
  3. Easy to index and Join will give best performance with the integer.
  4. Easy to understand and remember
  5. Support of function that will give last value generated like Scope_Indentity()

Disadvantages:

  1. If you are going to merge table frequently then there may be a chance to duplicated primary key.
  2. Limited range of uniqueness if you are going to store lots of data then it may be chance to run out of storage for INT data type.
  3. Hard to work with distributed tables.

GUID Data Type:

Advantages:

  1. It is unique for the current domains. For primary key is uniquely identifies the table.
  2. Less chances of for duplication.
  3. Suitable for inserting and updating large amount of data.
  4. Easy for merging data across servers.

Disadvantages:

  1. Bigger storage size (16bytes) will occupy more disk size then integer.
  2. Hard to remember and lower performance with Join then integer.
  3. Don’t have function to get last uniquely generated primary key.
  4. A GUID primary Key will added to all the other indexes on tables. So it will decrease the performance.

Conclusion:

From above the advantages and disadvantages we can conclude that if you are having very large amount of data in table then go for the GUID as primary key in database. Otherwise INT will give best performance. Hope this will help you. Please post your comment as your opinion.

 

 

Shout it
Published Wednesday, December 01, 2010 2:06 PM by Jalpesh P. Vadgama

Comments

# re: GUID Vs Int data type as primary key

Wednesday, December 01, 2010 3:52 AM by NFL Jerseys

thans for sharing.it`s very helpful.<a href="http://www.jerseysonline.co" title="NFL Jerseys">NFL Jerseys</a>

# re: GUID Vs Int data type as primary key

Wednesday, December 01, 2010 5:17 AM by osmirnov

Hi,

Very useful post. In past I read about identity generator in NHibernate and saw similar comparison, but your version is much better.

# re: GUID Vs Int data type as primary key

Wednesday, December 01, 2010 6:57 AM by Gee

You've touched on it under data merging but it's also worth emphasising that guids can safely be generated by application code outside of the database; often needed in disconnected/queued scenarios.

# re: GUID Vs Int data type as primary key

Wednesday, December 01, 2010 12:08 PM by Juma

bigint might solve your problem if you are worried about the range of the int datatype (It does take double the space though 8 bytes) - But still wont help you with the merging of tables issue. :-)

# Twitter Trackbacks for GUID Vs Int data type as primary key - DotNetJaps [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 GUID Vs Int data type as primary key - DotNetJaps         [asp.net]        on Topsy.com

# re: GUID Vs Int data type as primary key

Thursday, December 02, 2010 1:11 AM by Jalpesh P. Vadgama

@Juma- Big INT will allow more range but still its difficult to merge tables across servers.

# re: GUID Vs Int data type as primary key

Thursday, December 02, 2010 10:10 AM by Juma

@Jalpesh - exactly what I had said in my post  - "it still wont help you with the merging of tables issue" :-)

# re: GUID Vs Int data type as primary key

Friday, December 03, 2010 8:34 PM by Mike

Primary keys are, by default, clustered indexes.  The randomness of guids wreak havoc with INSERT performance since the clustered index key isn't sequential. If you need guids in your data, fine - just put them somewhere other than the primary key :)

# re: GUID Vs Int data type as primary key

Tuesday, December 21, 2010 10:03 PM by mikeb

>> "The randomness of guids wreak havoc with INSERT performance"

Absolutely the truth, just use auto increment integers for your clustered primary key.

Leave a Comment

(required) 
(required) 
(optional)
(required)