mysql table building best practice

table of Contents

An auto-incrementing primary key id

Two creation time, update time

Add index to three fields

Four data logical deletion

Five flexible use of database coding

Six add version field


This article mainly summarizes the good practical experience of building watches over the years of work, hoping to give you a little inspiration or help.

An auto-incrementing primary key id

Why do I need to increase the primary key id?

Consider from two perspectives of performance and storage space :

Performance : The auto-incrementing primary key belongs to sequential writing when adding records. The utilization of disk data pages is high and will not trigger data page splitting; business-related fields are used as primary keys, which can easily trigger random writes, in order to maintain the orderliness of the index , It is necessary to move the child nodes of the index tree page, which is easy to cause the data page to split.

Storage space : The length of the auto-incrementing primary key is generally smaller than that of the business primary key. The leaf nodes of the non-primary key index store the value of the primary key. Obviously, the smaller the primary key length, the smaller the leaf node of the non-primary key index. The space occupied by the primary key index is also smaller.

Therefore, from the perspective of performance and storage space, self-incrementing the primary key is reasonable.

Nothing is absolute, is it possible not to create an id when creating a table?

Don't tell me, I really encountered it. At the beginning, there was a student following teacher table in this section. This table does not have a primary key id. Student id and teacher id are used as joint primary key id. To be honest, add students to the teacher table Since the primary key id is incremented, this field has no business meaning.

If it is said that creating an auto-incrementing primary key id has no business meaning and can accept a certain performance impact from file writing, I think it is okay not to add an auto-increment id.

Two creation time, update time

Why do we need to add creation time and update time?

With creation time and update time, it is of great significance for data statistics and data tracking ; the update time is naturally a concept of version, which is convenient for the realization of optimistic lock.

Where is the creation time and update time? WEB server time? DB server time?

It is recommended to use the WEB server time (Tomcat, Jboss, Apache), but not the DB server time . Don't tell me, I have encountered the problem of DB server time being disordered, causing online data errors. It's all bloody lessons.

Add index to three fields

Why do you need an index?

The purpose of the index is to reduce the number of queries and improve query efficiency. You can fully use the covering index, the leftmost prefix principle, and the unique index to optimize the query.

Four data logical deletion

What is data tombstone?

The so-called logical deletion is to label the data for deletion.

Why is it not recommended to physically delete?

Because the data is gone once it is physically deleted, it is inconvenient to check problems and trace the data in the future. Besides, we are principled people. From deleting the library to running away, we are determined not to do it .

Five flexible use of database coding

If the table needs to store special characters such as emoji, you can use utf8mb4 encoding, and utf8 encoding is not recommended.

Six add version field

The version field is the version field added to the table. Every time the table is updated, the version field is also updated. The version field is used to implement optimistic locking.

Guess you like

Origin blog.csdn.net/jack1liu/article/details/112342170