Mysql database design optimization

Database table field type optimization

Use as little storage space as possible to store the data of a field;

  • Try to reduce the length when using the varchar type.
  • It is best to use the int type for the IP address.
  • The fixed length type is best to use char, for example: zip code.
  • Do not use smallint, int if you can use tinyint.
  • It is best to give each field a default value, preferably not null.
  • Using simple data types, int is easier to handle in mysql than varchar types. (The int type storage time is the best choice)
  • Use not null to define fields as much as possible. (Determined by the characteristics of innodb, non-not null values ​​require additional storage in the field, and will also increase the overhead of IO and storage)
  • Try to use text type as little as possible, and it is better to consider sub-table when it is absolutely necessary.

MYSQL database design principles

Core principles

  • Do not perform operations in the database;
  • CPU calculation must be moved to the business layer;
  • Control the number of columns;
  • Balance paradigm and redundancy (efficiency first; paradigm is often sacrificed);
  • Reject large sql statement: big sql, reject large transaction: big transaction, reject large batch: big batch;

Field class principle

  • Converting characters to numbers: the best conversion that can be converted, which also saves space and improves query performance;
  • Avoid using NULL fields (NULL fields are difficult to query and optimize, NULL field indexes require additional space, and composite indexes on NULL fields are invalid);
  • Use text type less (try to use varchar instead of text field);

Index principle

  1. Use indexes reasonably (improve queries, slow down updates, and more indexes must not be better);

  2. Prefix index for character fields;

  3. Do not perform column operations on the index;

  4. The innodb primary key recommends the use of auto-increment columns (the primary key establishes a clustered index, the primary key should not be modified, and the string should not be used as the primary key);

sql class principle

  1. The sql statement is as simple as possible;

  2. Simple affairs;

  3. Avoid using trig/func (client program instead);

  4. No need to select * (consumption of cpu, io, memory, bandwidth);

  5. OR can be rewritten as IN

Case

int type storage time-time conversion

Use int to store the date and time, use FROM_UNIXTIME(), UNIX_TIMESTAMP() two functions to convert.
Create table

create table test_time(
id int primary key auto_increment,
time_int int
);

Import Data

insert into test_time(time_int) values (UNIX_TIMESTAMP('2020-05-29 18:00:00'));

Query data, as shown in the figure below:
Insert picture description here
Time conversion:

select FROM_UNIXTIME(time_int) from test_time;

Insert picture description here
1. The UNIX_TIMESTAMP() function converts data in date format to int type
2. FROM_UNIXTIME(timestr) function converts int type to time format

The storage of ip address
in our external applications must record the ip address. In most cases, varchar (15) is used for storage, which requires 15 bytes for storage, but bigint only requires 8 bytes for storage. When the amount of data is large (tens of millions of data), the difference is 7 bytes.

Use bigint(8) to store the ip address, and use INET_ATON() and INET_NTOA() to convert.
Create table

create table sessions(
id int auto_increment,
ipaddress bigint,
primary key (id)
);

Import Data:

insert into sessions (ipaddress)values (inet_aton('192.168.0.1'));

View directly
Insert picture description here

Conversion

select inet_ntoa(ipaddress) from sessions;  

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_42494845/article/details/108735187