mysql tuning 1 (profile, data type optimization, index optimization, execution plan)

table of Contents

Performance monitoring: use profile to query running performance

Schema and data type optimization:

1. Optimization of data types

2. Reasonable use of paradigm and anti-paradigm

3. The choice of primary key

4. The choice of character set

5. The choice of storage engine

6. Appropriate data redundancy-is to join back to reduce query speed

7. Appropriate split-some fields in the table are very large and not commonly used, and can be split out,

Implementation plan

 

Optimize by index

1. Basic knowledge of indexing

2. Why does mysql adopt the b+ number structure?

3. Both myISAM and InnorDB are b+ trees, but there are differences:


Performance monitoring: use profile to query running performance

set profiling=1; Not turned on by default, need to be turned on manually

show profiles; show running time

show profile for query 1; show a piece of data time

show processlist; 

Check the number of connected threads to observe whether there are a large number of threads in an abnormal state or other abnormal characteristics

 

Schema and data type optimization:

1. Optimization of data types

  • Smaller is better, which means that small data types occupy less disk space, memory and cpu cache
  • Simple is good, do not use strings if you can use shaping. Date and time use data type, do not use string storage
  • Try to avoid null, because columns that can be null make indexing more complicated
  • Actual rules:

 Integer type:

Several integer types that can be used: TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT use 8, 16, 24, 32, and 64 bits of storage space respectively. Try to use the smallest data type that meets your needs

String: char (maximum 255, the space at the end will be automatically deleted), varchar (variable length), text (do not set the length, generally try not to use it)

  • datetime (8 bytes) and timestamp (4 bytes, accurate to the second), data (3 bytes, function calculation)
  • Use enumeration class instead of string

 create table enum_test(e enum('fish','apple','dog') not null);  insert into enum_test(e) values('fish'),('dog'),('apple');  select e+0 from enum_test;

  • Special types of data,

INET_ATON() and INET_NTOA functions

 

2. Reasonable use of paradigm and anti-paradigm

Paradigm advantage: prevent data redundancy

Anti-paradigm advantage, no need to cascade, sometimes a lot of quick queries, space for time

3. The choice of primary key

Surrogate Primary Key>Natural Primary Key

The surrogate primary key is easier to maintain

4. The choice of character set

  latin1 can be stored in Latin, there is no need to use a character set other than

   Chinese utf-8 (occupies 3 bytes), it may be garbled without GBK

5. The choice of storage engine

MyISAM directly locks the table,

InnorDB locks rows, mysql defaults, locking is through locking the index, so if you index duplicate data, you may also have the problem of locking a few rows

6. Appropriate data redundancy-is to join back to reduce query speed

7. Appropriate split-some fields in the table are very large and not commonly used, and can be split out,

 

Implementation plan

See - for details . . .

 

Optimize by index

1. Basic knowledge of indexing

Advantages: reduce the amount of data that needs to be scanned

          Help the server avoid sorting and temporary tables

          Random io table order io

Index classification:

       Primary key index

       Unique index

       Normal index

       Full-text index

       Composite index

Technical terms:

     Back to the table (build a common index to query the entire column of data)

    Index coverage (build a normal index, only query the id, do not return to the table)

    Leftmost match (combined index use, such as (name+age), you can check name alone, but you cannot check age alone)

   Index push down

2. Why does mysql adopt the b+ number structure?

Binary tree-avl number-red-black tree (disadvantages: deep level, each layer is equivalent to one io)

B number: multi-branch tree, branch nodes store pointers, and store actual data. MySQL reads 16k data by default once io. Then assume that a disk block of 16k stores a node. If the data data in this node is too large, you can If there are fewer pointers stored, then the number of forks of this multi-cross number may become less, then the depth of the tree will increase,

 

B+ tree: B-tree upgrade, only store data in leaf nodes, and store pointers in non-leaf nodes, so this multi-branch should be as many points as possible to reduce the number depth.

3. Both myISAM and InnorDB are b+ trees, but there are differences:

InnorDB, index and data are in the same file clustered index

MyISAM index and data are stored separately Non-clustered index

Guess you like

Origin blog.csdn.net/yanfei464486/article/details/114123787