当前位置:Gxlcms > 数据库问题 > mysql的优化措施,从sql优化做起

mysql的优化措施,从sql优化做起

时间:2021-07-01 10:21:17 帮助过:2人阅读

show status like ‘Handler_read%‘;

如果Handler_read_rnd_next的值比较高,说明索引不正确或者查询没有使用到索引

有索引:


mysql> select * from dd; +----+ | a | +----+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | | 8 | | 9 | | 10 | | 11 | | 12 | | 13 | +----+ 13 rows in set (0.00 sec) mysql> show create table dd; +-------+---------------------------------------- | Table | Create Table +-------+---------------------------------------- | dd | CREATE TABLE `dd` ( `a` int(11) NOT NULL, PRIMARY KEY (`a`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +-------+---------------------------------------- 1 row in set (0.02 sec)

 

mysql> select * from dd where a=10;
+----+
| a  |
+----+
| 10 |
+----+
1 row in set (0.00 sec)

mysql> show status like Handler_read%;
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| Handler_read_first    | 0     |
| Handler_read_key      | 1     |    //增加的是这个值
| Handler_read_last     | 0     |
| Handler_read_next     | 0     |
| Handler_read_prev     | 0     |
| Handler_read_rnd      | 0     |
| Handler_read_rnd_next | 2     |
+-----------------------+-------+
7 rows in set (0.00 sec)
无索引:




mysql> show create table q; +-------+------------------------------------------------------------------------------------+ | Table | Create Table | +-------+------------------------------------------------------------------------------------+ | q | CREATE TABLE `q` ( `a` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | +-------+------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> select * from q where a=10; +------+ | a | +------+ | 10 | +------+ 1 row in set (0.00 sec) 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 | 56 | +-----------------------+-------+ 7 rows in set (0.00 sec) mysql> select * from q where a=11; +------+ | a | +------+ | 11 | +------+ 1 row in set (0.00 sec) 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 | 70 | +-----------------------+-------+ 7 rows in set (0.01 sec)

 

简单实用的优化方法

  1. 定期检查表和分析表
    分析表语法:
    1
    analyze table 表名;

检查表语法:

1
check table 表名;

 

  1. 定期优化表
    • 对于字节大小不固定的字段,数据更新和删除会造成磁盘空间不释放,这时候就行优化表,可以整理磁盘碎片,提高性能
      语法如下:
      1
      optimize table user(表名);

mysql的优化措施,从sql优化做起

标签:

人气教程排行