Mysql table design issues need to pay attention

Discussion of the following database storage engine for the MySQL innodb because it is the most common, and most use a database engine

What is a page split?

This is because the clustered index uses a balanced binary tree algorithm, and each node keeps the data from the master key corresponding to the row, assuming inserting data primary key is self growth, it will quickly add the data according to the binary tree algorithm a node to the next, while the other nodes do not move; but if the insert is irregular data, so each time you insert the data will change the state before the binary tree. Resulting in a page split.

Why must set a primary key

Because even if you do not take the initiative to set up a primary key, innodb will help you generate a hidden column, to be used as auto-increment primary keys. So, anyway, there will increment primary keys, not as their own designated a primary key index can improve query efficiency.

Use increment primary keys or UUID

Primary key must be incremented by. innodb species clustered index is the primary key, if the primary key is the increment, then add each new data are sequentially inserted into the current position of the subsequent index node, when a full, will automatically open the second page, if than self-energizing a primary key, then it may be inserted in the middle, it will lead to split pages, generate a lot of debris, in short, with auto-increment primary keys to better performance.

The index file UUID generated greater when the amount of data over one million rows, primary keys, indexes and query performance than the file size UUID to be more efficient and smaller.

 

Why not have a primary key recommendation business meaning

1) because of the possibility of any column that contains the service has changed, once the tape business meaning primary key, the master key is likely to change. The primary key is changed, the data storage location on the disk change occurs, could trigger a page split, the creation of space debris.

2) primary keys with business meaning is not necessarily the order from growing, it may lead behind the insertion of data in the primary key must be better than the previous big, if there is behind the insertion of data in the main key is inserted is smaller than before, it is willing to be caused by page splitting , the creation of space debris.

 

Why do not represent an enum enum type field

First of all, the enumeration in MySQL general use tinyint type, why not use it enum

1) ENUM type, low order by operational efficiency, the need for additional operations

2) If the enumeration is value, there is a trap

For example: the following table structure create table test (sex enum ( '0', '1')) In this case the results of executing the insert statement insert into test values ​​(1) is queried 0

Only so inserted insert into test values ​​( '1') In this case the result is a

 

What type of currency field

Currency: If the currency unit is divided, you can use an int, if you stick with the dollar, you can use the Decimal type, float and double type can not be used, because these two types stored in binary, all have a certain degree of error.

For example: create table 't' ( 'price' float (10,2) default null) engine = innodb default charset = utf8 data and insert a price = 120.23, then you will find data becomes 120.25, precision misalignment.

 

What type of time field

1) If you are using varchar, the advantage of visual display, the disadvantage is the time to do the comparison operation, the need to use str_to_date other functions convert it to the type of event, you will find that it is impossible to hit the index, the amount of data a large, this is a pit.

2) If the timestamp type, which is 4 bytes, the time range represented by 1970-01-01 to 2038-01-19 2038. That time you can not use the timestamp storage. This type has an advantage that comes with time zone, if you modify the time zone, the field value will change with it.

3) dtatime type, which is 8 bytes, a timestamp their maintenance, than the timestamp represents a large range, because of the need to maintain its own, it is not convenient.

 

Why not direct high-capacity storage pictures, audio, video and other content

In actual production, we are all stored on the file server real file, the corresponding file can be stored in the database Road King, MySQL has a type, text, and blob type of storage of large files, but does not substantially use

1) MySQL memory temporary tables do not support text, blob such a large data type, if the query contains such data, when sorting and other operations, you can not use memory temporary tables, you must use a temporary table disk, resulting in low efficiency.

2) Too many binlog content, because the data is too big and will lead to more binlog content, and MySQL master-slave synchronization to synchronize by binlog, binlog too much will lead to the master-slave synchronization problems efficiency.

 

Why fields to be defined as NOT NULL

1) Index performance is not good

2) the query will be some unexpected results

Guess you like

Origin www.cnblogs.com/yjp372928571/p/11221667.html
Recommended