Whether the database primary key is self-growing (INT) good or UUID

Source: Internet
Author: User
Tags uuid

In fact, for the use of self-growth or UUID, the most discussed is the speed and storage space, here I joined the security and distributed, the specific comparison is as follows:

advantages of using self-growth key:
1. Very small data storage space
2, the best performance
3, easy to remember
disadvantages of using self-growth key:
1, if there is a large amount of data, may exceed the value of the growth range
2. It is difficult (not impossible) to handle distributed storage data tables, especially if you need to merge tables
3, Low security, because it is regular, easy to get data illegally
advantages of using the GUID master key:
1, it is unique, there is less chance of duplication
2. Suitable for inserting and updating in large amount of data, especially in high concurrency and distributed environment
3, cross-server data merge is very convenient
4. High Security
disadvantages of using the GUID master key:
1, storage space is large (in bytes), so it will occupy more disk space
2, will reduce performance
3. Hard to remember

Combined with the actual application of each project, how should we choose? As follows:

1, the project is a stand-alone version, and the data volume is larger (millions), with self-growth, it is best to consider the security, do some security measures.
2, the project is a stand-alone version, and the amount of data is not so large, the speed and storage requirements are not high, with UUID.
3, the project is distributed, then the preferred UUID, distributed generally to speed and storage requirements are not high.
4, the project is distributed, and the amount of data reached tens can be higher, the speed and storage requirements, you can use self-growth.

Now someone asks, why not just use the 4th option, which satisfies all the requirements of the previous three scenarios? It is because of the use of self-growth of distributed processing will be very complex (the specific scheme can be Baidu), in the case of limited resources can be used in the previous three simple implementation scenarios.

So why must you use self-growth or UUID as the database primary key? Is there a better solution? There must be: snowflake

In fact, most projects can use Twitter's snowflake to generate primary keys, and the primary key generated by Snowflake is a primary key between self-growth and UUID (small storage space, fast, distributed, time series). It is said that snowflake can generate 260,000 IDs per second, up to 1024 nodes, and the team can use snowflake as the underlying database primary key tool class for team members.

My own view is: for the project involved in the Business Name table of the primary key with UUID, and business and business relationship between the table of the primary key with self-increment;


Reprint to: http://www.yyjjssnn.cn/articles/754.html

Whether the database primary key is self-growing (INT) good or UUID

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.