http://geeksblog.cc/2016/06/11/mysql-optimize/
General steps for Optimizing SQL
- Learn the frequency of various SQL executions with show status
- Locating SQL statements with low execution efficiency
- Analyze inefficient SQL with explain
- Analyze SQL with show profile?
- How the optimizer chooses an execution plan through the trace analysis
- Identify problems and take measures to optimize
Index optimization measures
Typical scenarios for using indexes in MySQL
- Match all values, condition all columns are in the index and are equivalent
- Match the range lookup for the value, the field must be in the index
- Match the leftmost prefix, and the composite index will only be found based on the leftmost column
- Query only the index, that is, all the fields of the query are on the index
- Match the column prefix, such as like ' abc% ', if it's like '%aaa ' you can't
- If the column name is an index, using column is null uses the index
Typical scenario where an index exists but does not use an index
- A like query that starts with% cannot use the B-Tree index
- An implicit conversion of a data type is not possible with an index
- Composite index, query conditions do not conform to the leftmost rule
- Conditions that are split with or, if the previous condition has an index, and the subsequent condition is not indexed
To view the usage of the index
If the value of Handler_read_rnd_next is higher, the index is incorrect or the query is not used to index
There are indexes:
Mysql> Select * fromDD;+----+|A|+----+| 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || Ten || One || A || - |+----+ -Rowsinch Set(0.00sec) MySQL>ShowCreate TableDD;+-------+----------------------------------------| Table | Create Table+-------+----------------------------------------|Dd| CREATE TABLE' DD ' (' a 'int( One) not NULL, PRIMARY KEY (' a ')) ENGINE=InnoDBDEFAULTCHARSET=Utf8|+-------+----------------------------------------1Rowinch Set(0.02Sec
Mysql> Select * fromDdwhereA=Ten;+----+|A|+----+| Ten |+----+1Rowinch Set(0.00sec) MySQL>Show status like 'handler_read%';+-----------------------+-------+|Variable_name|Value|+-----------------------+-------+|Handler_read_first| 0 || Handler_read_key | 1 | This value is added.|Handler_read_last| 0 ||Handler_read_next| 0 ||Handler_read_prev| 0 ||Handler_read_rnd| 0 ||Handler_read_rnd_next| 2 |+-----------------------+-------+7Rowsinch Set(0.00Sec
No index:
Mysql>ShowCreate TableQ;+-------+------------------------------------------------------------------------------------+| Table | Create Table |+-------+------------------------------------------------------------------------------------+|Q| CREATE TABLE' Q ' (' A 'int( One)DEFAULT NULL) ENGINE=InnoDBDEFAULTCHARSET=Utf8|+-------+------------------------------------------------------------------------------------+1Rowinch Set(0.00sec) MySQL> Select * fromQwhereA=Ten;+------+|A|+------+| Ten |+------+1Rowinch Set(0.00sec) MySQL>Show status like 'handler_read%';+-----------------------+-------+|Variable_name|Value|+-----------------------+-------+|Handler_read_first| 1 ||Handler_read_key| 1 ||Handler_read_last| 0 ||Handler_read_next| 0 ||Handler_read_prev| 0 ||Handler_read_rnd| 0 ||Handler_read_rnd_next| About |+-----------------------+-------+7Rowsinch Set(0.00sec) MySQL> Select * fromQwhereA= One;+------+|A|+------+| One |+------+1Rowinch Set(0.00sec) MySQL>Show status like 'handler_read%';+-----------------------+-------+|Variable_name|Value|+-----------------------+-------+|Handler_read_first| 2 ||Handler_read_key| 2 ||Handler_read_last| 0 ||Handler_read_next| 0 ||Handler_read_prev| 0 ||Handler_read_rnd| 0 || Handler_read_rnd_next | - |+-----------------------+-------+7Rowsinch Set(0.01Sec
Simple and practical optimization method
- Periodic checklist and Analysis tables
Parsing table Syntax:
Check table syntax:
- Regularly optimize tables
- For fields with an irregular byte size, data updates and deletions can cause disk space not to be freed, and the row optimizes the table, which can defragment the disk and improve performance
The syntax is as follows:
MySQL optimization measures from SQL optimization