views:

326

answers:

3

When inserting a guid as a primary key does the db server go through every single row to make sure there's no duplicate or does it just assume that a duplicate is not possible or very unlikely?

with int the db server can simply increment the key but not the case with GUIDs

A: 

I guess only if it is a primary key or a unique key

But as i know GUIDs are unique.

Issa Qandil
GUIDs are unique in a probabilistic sense, but not guaranteed (in many implementations).
John Zwinck
Yes, if you use a random GUID generator, you have get a duplicate (very unlikely). But when you try to insert those two duplicates as primary keys, the server will flag an error.
Matthew Flaschen
+1  A: 

All primary keys are unique. So the server is responsible for verifying no two rows ever have the same primary key. This is true if the primary key is int, varchar, guid, whatever. How the uniqueness constraint is verified is implementation-defined. Basically, don't let it concern you unless you've shown it to be a performance barrier.

Always profile before worrying excessively about performance.

Matthew Flaschen
+5  A: 

That's the databases responsibility once you tell it that a column is a primary key.

It's nothing that you have to be concerned with at this point, let the database worry about this for you.

Also, since this is a primary key it's going to be indexed. So your preformance concern about going though each row (or a table scan) would not really occur at this level. The database would use the index file to ensure that only a unique value could be inserted.

Richard West