UUID versus auto increment number for primary key

爷,独闯天下 提交于 2019-12-18 13:02:44

问题


Why should I choose UUID over an auto increment number for my entity's primary key? What are the pros and cons?


回答1:


Andrey and Mjg both had good points, but I would add a related performance issue that is significant.

With the decoupling of database and key generation also allows applications that have complex relationships between objects to create them all with the keys in place, so that bulk inserts are possible.

In the case of auto-increment, all of the objects that own relationships (ie the tables with foreign keys) have to wait for the other side of the relationship (ie the table the foreign key comes from) to save, query the assigned ids, and then individually update the records former records.




回答2:


UUIDs are globally unique, and can be generated in parallel without communication between clustered DB servers. So "Object IDs are more synergistic with sharding and distribution.". UUIDs are typically stored in 128 bits.

Auto-incremented integers are more user-friendly (well, shorter and more memorable anyway), and automatically allow sorting by insertion-order. Integers are typically stored in 32 or 64 bits.




回答3:


The main pro that you can generate them independently. Con is that it is larger.



来源:https://stackoverflow.com/questions/5159413/uuid-versus-auto-increment-number-for-primary-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!