Guid vs INT - Which is better as a primary key?
This has been asked in Stack Overflow here and here.
Jeff's post explains a lot about pros and cons of using GUID.
###GUID Pros
- Unique across every table, every database and every server
- Allows easy merging of records from different databases
- Allows easy distribution of databases across multiple servers
- You can generate IDs anywhere, instead of having to roundtrip to the database, unless partial sequentiality is needed (i.e. with
newsequentialid()
)- Most replication scenarios require GUID columns anyway
###GUID Cons
- It is a whopping 4 times larger than the traditional 4-byte index value; this can have serious performance and storage implications if you're not careful
- Cumbersome to debug (
where userid='{BAE7DF4-DDF-3RG-5TY3E3RF456AS10}'
)- The generated GUIDs should be partially sequential for best performance (eg,
newsequentialid()
on SQL Server 2005+) and to enable use of clustered indexes
If you are certain about performance and you are not planning to replicate or merge records, then use int
, and set it auto increment (identity seed in SQL Server).
I have used a hybrid approach with success. Tables contain BOTH an auto-increment primary key integer id
column AND a guid
column. The guid
can be used as needed to globally uniquely identify the row and id
can be used for queries, sorting and human identification of the row.
If you're synchronizing your data with an external source, a persistent GUID can be much better. A quick example of where we're using a GUIDs is a tool that is sent to the customer to crawl their network and do certain classes of auto-discovery, store the records found, and then all the customer records are integrated into a central database back on our end. If we used an integer, we would have 7,398 "1"s, and it'd be a lot harder to keep track of which "1" was which.