For SQL server is it better to use an uniqueidentifier(GUID) or a bigint for an identity column?
views:
1432answers:
10In general I'd recommend a BIGINT
over a GUID
(as guids are big and slow), but the question is, do you even need that? (I.e. are you doing replication?)
If you're expecting less than 2 billion rows, the traditional INT
will be fine.
That depends on what you're doing.
If speed is the primary concern then a plain old int
is probably big enough.
If you really will have more than 2 billion (with a B ;) ) records, then use bigint
or a sequential guid.
If you need to be able to easily synchronize with records created remotely, then Guid
is really great.
Update
Some additional (less-obvious) notes on Guids:
- They can be hard on indexes, and that cuts to the core of database performance
- You can use sequential guids to get back some of the indexing performance, but give up some of the randomness used in point two.
- Guids can be hard to debug by hand (
where id='xxx-xxx-xxxxx'
), but you get some of that back via sequential guids as well (where id='xxx-xxx' + '123'
). - For the same reason, Guids can make ID-based security attacks more difficult- but not impossible. (You can't just type
'http://example.com?userid=xxxx'
and expect to get a result for someone else's account).
It really depends on whether or not the information coming in is somehow sequential. I highly recommend for things such as users that a GUID might be better. But for sequential data, such as orders or other things that need to be easily sortable that a bigint may well be a better solution as it will be indexed and provide fast sorting without the cost of another index.
Depends no what you need. DB Performance would gain from integer while GUIDs are useful for replication and not requiring to hear back from DB what identity has been created, i.e. code could create GUID identity before inserting into row.
It really depends whether you're expecting to have replication in the picture. Replication requires a row UUID, so if you're planning on doing that you may as well do it up front.
If you're planning on using merge replication then a ROWGUIDCOL
is beneficial to performance (see here for info). Otherwise we need more info about what your definition of 'better' is; better for what?
Are you doing replication or do you have sales people who run disconnected databses that need to merge, use a GUID. Otherwise I'd go for an int or bigint. They are far easier to deal with in the long run.
Jeff Atwood has already answered everything in this comprehensive post.
Unless you have a real need for a GUID, such as being able to generate keys anywhere and not just on the server, then I would stick with using INTEGER-based keys. GUIDs are expensive to create and make it harder to actually look at the data. Plus, have you ever tried to type a GUID in an SQL query? It's painful!
I'm with Andrew Rollings.
Now you could argue space efficiency. An int is what, 8 bytes max? A guid is going to much longer.
But I have two main reasons for preference: readability and access time. Numbers are easier for me than GUIDs (since I can always find the next/previous record easily).
As for access time, note that some DBs can start to have BIG problems with GUIDs. I know this is the case with MySQL (MySQL InnoDB Primary Key Choice: GUID/UUID vs Integer Insert Performance). This may not be much of a problem with SQL Server, but it's something to watch out for.
I'd say stick with INT or BIGINT. The only time I would think you'd want the GUID is when you are going to give them out and don't want people to be able to guess the IDs of other records for security reasons.