Grunf
Grunf

Reputation: 472

unique identifier strategy

I'm going to use c# and mssql database with nhibernate orm layer and I'm trying to figure which approach will be faster to use in the production.

As far as I know Guids take 4x more space than ints does 16 against 4 bytes.

Synchronization of data will be frequent. This is touchy subject since bad approach can lead to massive memory consumption. Using integers can cause faster response but every data insertion must hit database to get value from sequence.

So, should I use integer approach with identity on inside mssql or using HiLo generator inside nhibernate mapping for identity columns?

Will I experience performance issues using guids or not?

Upvotes: 0

Views: 195

Answers (1)

Patrick
Patrick

Reputation: 17973

Performance between comparing a 32 bit datatype (4 byte int) as opposed to comparing a 128 bit datatype (guid) is probably very minor. I don't have any numbers, and the biggest factor would be that the 128 bit datatype would occupy more than one register as opposed to a smaller datatype. Since we're moving towards 64-bit architechtures, a guid will take two registers instead of one when comparing.

Still, I would prefer using a maintainable strategy based on the requirements of the problem rather than trying to optimize on either a 32 bit or 128 bit identifier.

Since you stated that one of the requirements is to synchronize databases, a guid as opposed to an incremented int is preferred. If you use an incremented int you basically need to make sure the incremented int is not "taken" in another database before inserting.

Sure, you can have some strategies for this, such as every client only generating in a specific subset of the range, but this is already covered by using a guid. Thanks to its size and how it's created, each client can create a guid with a minimal possibility of it being duplicated by someone else.

So, to rephrase, I would recommend using a Guid

Upvotes: 2

Related Questions