ORM – primary key – native, sequence or GUID key?
When I read this and this, and then read this (which ironically quotes the other two), I found that I wanted to know how big the discussion on this topic was? I am a SQL server person, so I prefer to use the identity. ID generated automatically in the form of int However, when I know that I need some form of replication between server and server or synchronization between client and server, I tend to use guid as my key
Question: should I always use guid as my primary key in all tables in case I might need this possible extensibility? Does this make my architecture more flexible because it can migrate between platforms at any time? Does this help me keep ORM flexible (regardless of flavor) by not embedding platform specific features?
Countermeasures:
@David Archer: according to your comments, I updated my post without saying "natural key" Your natural key is correct because it is defined as such Thank you for your correction
Solution
I tend to prefer application generated primary keys, usually using the Lo / hi algorithm implemented by NHibernate (when I use it in my project) Otherwise, the sequence guid will work as well This is not just my suggestion, but the person who has been doing the whole development is much longer than myself
The problem I see when using the database generated primary key is that before saving the database to the database, you must access the database to obtain these identity values instead of setting everything Because of this fact, it usually destroys the unit of work pattern in NHibernate If you don't use UOW mode in your application, obviously this disadvantage doesn't apply
If you are using guid for PK, you certainly want to use sequential guid to eliminate index fragmentation This also gives you the "rough sort order" mentioned in another poster, although I usually have a dateinserted column or something similar to these types
By adding the guid column has been shown, the performance overhead is quite small compared with 4-byte integers. I dare say that for non large data sets, the performance difference is insignificant
Natural bonds are the product of the devil
The above is the ORM – primary key – native, sequence or GUID key collected by programming house for you? I hope this article can help you solve the ORM - primary key - native, sequence or GUID key? Program development problems encountered.
If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.