标签:
http://geeksblog.cc/2016/06/11/mysql-optimize/
mysql中使用索引的典型场景
存在索引但不会使用索引的典型场景
查看索引使用的情况
1
|
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
|
analyze table 表名;
|
检查表语法:
1
|
check table 表名;
|
1
|
optimize table user(表名);
|
标签:
原文地址:http://www.cnblogs.com/zengkefu/p/5634755.html