Is it better to use self-growth or UUID for the primary key of the database?

Hello students, today I will tell you which data type should be used when designing the primary key of the MySQL database. We know that when we design the database, the primary key must be set. Under normal circumstances, we will choose to use the two primary key strategies of primary key auto-increment or UUID. So which of the two is better? Which one should we use, please come and learn with Lao Xu today.

1. Primary key auto-increment

  1. Advantages of using self-growth as primary key

  • Small data storage space;

  • Best performance;

  • easy to remember.

  1. Disadvantages of using self-growth as primary key

  • If there is a large amount of data, it may exceed the value range of self-growth;

  • It is difficult (not impossible) to deal with distributed storage data tables, especially when tables need to be merged;

  • The security is low, because it is regular, and it is easy to obtain data illegally.

2. UUID

  1. Advantages of using UUID as primary key

  • It is unique and there is little chance of duplication;

  • Suitable for insert and update operations in large amounts of data, especially in high concurrency and distributed environments;

  • It is very convenient to merge data across servers;

  • Higher security.

  1. Disadvantages of using UUID as primary key

  • The storage space is large (16 byte), so it will take up more disk space;

  • will degrade performance;

  • It's hard to remember.

3. Use options

So in general, for the above two primary key strategies, how should we choose?

In general, MySQL recommends that we use auto-increment IDs. This is because in MySQL's InnoDB storage engine, the primary key index is a clustered index, and the B+ tree leaf nodes of the primary key index store the values ​​and data of the primary key in order. If the primary key index is an auto-incrementing ID, you only need to arrange it in order; if it is a UUID, the ID is randomly generated, so a large amount of data will be moved during data insertion, resulting in a large number of memory fragments, resulting in poor insertion performance. decline.

If we want to combine specific projects, which annotation strategy should we choose? At this time, the following situations should be divided into:

  • When the project is a stand-alone version and the amount of data is relatively large (millions), use self-growth. At this time, it is best to consider security and take some security measures.

  • The project is a stand-alone version, and the amount of data is not so large. When the speed and storage requirements are not high, use UUID.

  • If the project is distributed, then UUID is the first choice. Distributed generally does not require high speed and storage.

  • When the project is distributed and the amount of data reaches tens of millions or higher, self-growth can be used when there are requirements for speed and storage.

Now you know how to choose a primary key strategy in MySQL? Pay attention to Xiaoqian, dry goods continue every day!

Of course, if you feel that the above content is not enough, and you want to learn more about MySQL-related technical knowledge, watch MySQL professional and comprehensive technical courses, free of charge!

MySQL database tutorial, mysql installation to mysql advanced set of customs clearance

Guess you like

Origin blog.csdn.net/GUDUzhongliang/article/details/132096849