tags:

views:

210

answers:

3

I am doing a web-application on asp.net mvc and I'm choosing between the long and Guid data type for my entities, but I don't know which one is better. Some say that long is much faster. Guid also might have some advantages. Anybody knows ?

+2  A: 

SIZE: Long is 8 bytes Guid is 16 bytes

GUID has definitely high probability for going to be unique and is best to use for identification of individual records in a data base(s).

long (Identity in DB), might represent a unique record in a table but you might have records represented by same ID (Identity), in one or more different table like as follows:

TableA: PersonID int, name varchar(50)
TableB: ProductID int, name varchar(50)

SELECT PersonID from TableA where name =''
SELECT ProductID from TableB where name =''

both can return same value, but in case of GUID :

TableA: PersonID uniqueidentifier, name varchar(50)
TableB: ProductID uniqueidentifier, name varchar(50)

SELECT PersonID from TableA where name =''
SELECT ProductID from TableB where name ='

you can rarely have same value as id returned from two tables

Have a look here

Asad Butt
"GUID is definitely going to be unique" is actually not correct (thoiugh for all intent and purposes you're right) it's a probabilistic algorithm so the statement would be more correct something like this:"GUID is with a very high probability going to be unique" but it's actually _not_ guaranteed (but the risk is close to the risk of being hit by a meteor)
Rune FS
@Rune FS: Thanks mate for correction, have edited accordingly
Asad Butt
+2  A: 

Guids make it much easier to create a 'fresh' entity in your API because you simply assign it the value of Guid.NewGuid(). There's no reliance on auto-incremented keys from a database, so this better decouples the Domain Model from the underlying persistence mechanism.

On the downside, if you use a Guid as the Clustered Index in SQL Server, inserts become expensive because new rows are very rarely added to the end of the table, so the index needs to be rebuilt very often.

Another issue is that if you perform selects from such a database without specifying an explicit ordering, you get out results in an essentially random order.

Mark Seemann
Sequential guids avoid the clustered index problem you describe. See http://stackoverflow.com/questions/665417/sequential-guid-in-linq-to-sql
Lachlan Roche
Id is going to be the Primary Key, that means that it will be automatically a clustered index ?
Omu
On SQL Server, the Primary Key will be a clustered index by default, but you specify otherwise.
Mark Seemann
+5  A: 

When GUIDs can be Inappropriate

GUIDs are almost always going to be slower because they are larger. That makes your indexes larger. That makes your tables larger. That means that if you have to scan your tables, either wholly or partially, it will take longer and you will see less performance. This is a huge concern in reporting based systems. For example, one would never use a GUID as a foreign key in a fact table because its length would usually be significant, as fact tables are often partially scanned to generate aggregates.

Also consider whether or not it is appropriate to use a "long". That's an enormously large number. You only need it if you think you might have over 2 BILLION entries in your table at some point. It's rare that I use them.

GUIDs can also be tough to use and debug. Saying, "there's a problem with Customer record 10034, Frank, go check it out" is a lot easier than saying "there's a problem with {2f1e4fc0-81fd-11da-9156-00036a0f876a}..." Ints and longs are also easier to type into queries when you need to.

Oh, and it's not the case that you never get the same GUID twice. It has been known to happen on very large, disconnected systems, so that's something to consider, although I wouldn't design for it in most apps.

When GUIDs can be Appropriate

GUIDs are the appropriate when you're working with disconnected systems where entities are created and then synchronized. For example, if someone makes a record in your database on a mobile device and syncs it, or you have entities being created at different branch offices and synced to a central store at night. That's the kind of flexibility they give you.

GUIDs also allow you the ability to associate entities without persisting them to the database, in certain ORM scenarios. Linq to SQL (and I believe the EF) don't have this problem, though there are times you might be forced to submit your changes to the database to get a key.

If you create your GUIDs on the client, it's possible that since the GUIDs you create are not sequential, that insert performance could suffer because of page splits on the DB.

My Advice

A lot of stuff to consider here. My vote is to not use them unless you have a compelling use case for them. If performance really is your goal, keep your tables small. Keep your fields small. Keep your DB indexes small and selective.

Dave Markle