MySQL optimization-MySQL optimization steps, slow queries and indexes

SQL statement optimization

1) General steps for optimizing SQL statements
1. Query logs to find time-consuming SQL
2. EXPLAIN to check whether the index is in effect
3. Check whether the table index is in effect
2) Index optimization
3) Check and optimize usage methods
4) Optimization of common SQL

Check the frequency of use of server additions, deletions, changes and checks: (since this launch)

Through the show status command to understand the execution frequency of various SQL.
Format: mysql>show [session|global] status;
where: session (default) means the current connection, global means from the database to the present

show status like "%Com_update%";
show status like "%Com_insert%";
show status like "%Com_select%";
show status like "%Com_delete%";

Check the number of rows affected by innodb since it was started:

mysql> show status like "%InnoDB_rows%";
+----------------------+-------+
| Variable_name        | Value |
+----------------------+-------+
| Innodb_rows_deleted  | 2     |
| Innodb_rows_inserted | 0     |
| Innodb_rows_read     | 105   |
| Innodb_rows_updated  | 3     |
+----------------------+-------+

Locate SQL statements with low execution efficiency:

View user table index
Index reference document:
http://c.biancheng.net/view/7364.html

show index from user\G

Let the test data grow exponentially

insert into user(name,age,score,created_at,updated_at)  select name,age,score,created_at,updated_at from user;

1. Explain (commonly used) or desc locates the number of rows affected by a sql statement

The premise is that the SQL statement has been queried for a lot of time, the execution efficiency is slow, more than 1ms, etc.
Explain the use of the check document: https://www.jianshu.com/p/18ab39d8dd88

mysql>explain select * from user where username='user8'\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: user
   partitions: NULL //
         type: ALL  //联合查询所使用的类型,type显示的是访问类型,是较为重要的一个指标,结果值从好到坏依次是:
system > const > eq_ref > ref >fulltext > ref_or_null > index_merge > unique_subquery >index_subquery > range > index > ALL
possible_keys: NULL  //这一列显示了查询可以使用哪些索引,是基于查询访问的列和使用的比较操作符来判断的.(可能用到的索引)
如果没有任何索引可以使用,就会显示成null
          key: NULL  //显示了MySQL决定采用哪个索引来优化对该表的访问 (真正用到的索引)
      key_len: NULL //显示mysql决定使用的键长度,如果键是null,则长度为null。
          ref: NULL //显示了之前的表在key列记录的索引中查询值所用到的列或常量
         rows: 11   //显示的是MySQL为了找到所需的值而要读取的行数.
     filtered: 10.00  //进行全文索引检索。
        Extra: Using where

2.View the slow query log of mysql.

1) Check whether the slow query log is enabled

show variables like "%quer%";

slow_query_log  | ON  //ON开启慢查询
slow_query_log_file | mysql-slow.log //保存所在日志
long_query_time | 10.000000   //慢查询秒数

Insert picture description here

2) View the number of slow queries

show status like "%quer%";
Slow_queries | 0  //慢查询次数

3. Modify the time of slow query (my.ini)

long_query_time=6   //定位为6秒,记录为慢查询sql

4. Restart the mysql server

net stop mysql57
net start mysql57

Optimize table space

optimize table sales; that is, put

1.myisam表没有问题.
2.innodb表ibdata1文件无法回收以删除数据表空间.

Index optimization

Index instance

Indexes are used to quickly find rows with a specific value in a certain column. Using indexes on related columns is the best way to improve the performance of SELECT operations.

  1. For multi-column indexes created, as long as the leftmost column is used in the query conditions, the index will generally be used. Create a composite index as follows.
mysql>create index ind_sales2_com_mon on sales2(company_id,moneys);

Then query by company_id and find that a composite index is used

mysql>explain select * from sales2 where company_id=2006\G;

Use the following query to not use the composite index.

mysql>explain select * from sales2 where monye=1\G;

The check table checks whether the structure reports an error:

Not used much in general

mysql> check table v_user;
+-----------+-------+----------+----------+
| Table     | Op    | Msg_type | Msg_text |
+-----------+-------+----------+----------+
| user.user | check | status   | OK       |
+-----------+-------+----------+----------+

Index storage classification:

1.myisam
1) frm table structure
2) myd table data
3) myi table index

2.innodb
1) frm table structure
2) ibd index + part of the table data
3) ibdata1 shared space for all tables

Composite index:

For the created multi-column index, as long as the leftmost column is used in the query condition, the index will generally be used.
1. The leftmost matching principle of Mysql joint index
Reference document:
https://segmentfault.com/a/1190000015416513

like keyword:

When searching with like, the previous index of% may become invalid.

desc select * from user where username like 'linux%'\G

Null judgment:

When judging the null value, the index of the username column is used.

desc select * from user where username is null;

or keyword:

In the case of using or, the indexes on both sides may fail.

desc select * from user where username='user7' or age=15\G

The field and value types are different:

When the type of the field and the value in the condition are different, the index of the field may become invalid.

desc select * from user where username=200\G

Check the frequency of use of the index:

mysql> show status like "%Handler_read%";
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| Handler_read_key      | 42    |    #越大越好
| Handler_read_rnd_next | 306   |    #越小越好
+-----------------------+-------+

Multi-table query index use:

The current multi-table queries commonly used in version 5.7 are indexed.

desc select * from t1 where class_id in(select id from class)\G
desc select t1.* from t1,class where t1.class_id=class.id\G
desc select t1.* from class left join t1 on t1.class_id=class.id\G

Guess you like

Origin blog.csdn.net/weixin_39218464/article/details/112095829