Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what type of database record id to use: long or guid?

In recent years I was using MSSQL databases, and all unique records in tables has the ID column type of bigint (long). It is autoincrementing and generally - works fine.

Currently I am observing people prefer to use GUIDs for record's identity.

Does it make sense to swap bigint to guid for unique record id?

I think it doesn't make sense as generating bigint as well as sorting would be always faster than guid, but... some troubles come when using two (or more) separated instances of application and database and keep them in sync, so you have to manage id pools between sql servers (for example: sql1 uses id's from 100 to 200, sql2 uses id's from 201 to 300) - this is a thin ice. With guid id, you don't care about id pools.

What is your advice for my mirrored application (and db): stay with traditional ID's or move to GUIDs?

Thanks in advance for your reply!

like image 604
twk Avatar asked Feb 15 '09 22:02

twk


People also ask

Should ID be int or GUID?

int is smaller, faster, easy to remember, keeps a chronological sequence. And as for Guid, the only advantage I found is that it is unique. In which case using sql server guid would be better than and int and why? From what I've seen, int has no flaws except by the number limit, which in many cases are irrelevant.

When should I use GUID?

The guid can be used as needed to globally uniquely identify the row and id can be used for queries, sorting and human identification of the row. The id identifies the row in this table.

Is it good to use GUID as primary key?

GUIDs may seem to be a natural choice for your primary key - and if you really must, you could probably argue to use it for the PRIMARY KEY of the table. What I'd strongly recommend not to do is use the GUID column as the clustering key, which SQL Server does by default, unless you specifically tell it not to.

What is record ID in database?

A Record ID can help you identify a record in the database. A Record ID is determined via the ID Template configuration in the Configuration Manager application. If an ID Template has not been configured for a family, records created in those families will not have a Record ID.


2 Answers

guids have the

Advantages:

  • Being able to create them offline from the database without worrying about collisions.
  • You're never going to run out of them

Disadvantages:

  • Sequential inserts can perform poorly (especially on clustered indexes).
    • Sequential Guids fix this
  • Take up more space per row
  • creating one cleanly isn't cheap
    • but if the clients are generating them this is actually no problem

The column should still have a unique constraint (either as the PK or as a separate constraint if it is part of some other relationship) since there is nothing stopping someone supplying the GUID by hand and accidentally/deliberately violating uniqueness.

If the space doesn't bother you and your performance if not significantly impacted they make a lot of problems go away. The decision is inevitably specific to the individual needs of the application.

like image 176
ShuggyCoUk Avatar answered Sep 17 '22 20:09

ShuggyCoUk


I use GUIDs in any scenario that involves either replication or client-side ID generation. It's just so much easier to manage identity in either of those situations with a GUID.

For two-tier scenarios like a web application talking directly to the database, or for servers that don't need to replicate (or perhaps only need to replicate in one direction, pub/sub style) then I think an auto-incrementing ID column is just fine.

As for whether to stay with autoincs or move to GUIDs ... it's one thing to advocate GUIDs in a green-field application where you can make all these decisions up front. It's another to advise someone to migrate an existing database. It might be more pain than it's worth.

like image 22
Matt Hamilton Avatar answered Sep 21 '22 20:09

Matt Hamilton