Analysis of Mysql using UUID as the primary key

Preconditions
Mysql innodb storage engine

One: Why use UUID

UUID is the abbreviation of Universally Unique Identifier. It is a standard for software construction and a part of the Open Software Foundation in the field of distributed computing environments. Its purpose is to allow all elements in the distributed system to have unique identification information, without the need for the central control terminal to specify identification information. In this way, everyone can create a UUID that does not conflict with others.
UUID is easy to generate and will not be repeated. It is very common in a distributed architecture system, but for using the mysql innodb storage engine, UUID is not a good choice.

2. Analysis of the disadvantages of UUID as the primary key

2.1 UUID is relatively long and takes up space

The default 32-bit char occupies more table space when used as the primary key. The business requires entity relationships and table-connected queries, resulting in the table space being occupied by the UUID field, and no later database optimization is used.

2.2 UUID has no order and is randomly generated

The default in innodb is the primary key B+tree index. UUID generation has no order, so the insert operation is random, and the data is stored discretely, causing frequent page splits in innodb, making the insert operation very inefficient.

Innodb's table storage is in order, and UUID generation has no order.

2.3 Affecting secondary indexes

Innodb's non-primary key index is affected. We know that the primary key index leaf node stores
data data, and the secondary index (non-primary key index) leaf node stores the primary key id. In this way, the secondary index in the table must store UUID ,take up space.

2.4 The comparison between uuids is much slower than the number, which affects the query speed.

This point needs to be verified. If it is a primary key index, then theoretically there is an order, and it is possible if the number comparison size is faster than the string comparison size.

Reference material: https://rdcqii.hundsun.com/portal/article/953.html

Third, the snowflake algorithm

mybatisplus built-in snowflake algorithm.

Details:
https://www.jianshu.com/p/2a27fbd9e71a

Guess you like

Origin blog.csdn.net/keep_learn/article/details/108700052