查看一些mysql性能参数
show status like 'value'
其中value
可以为:
connections#连接数
uptime#服务器上线时间
slow_queries#慢查询次数
com_select#查询操作次数
com_insert#插入操作次数
com_update#更新操作次数
com_delete#删除操作次数
分析查询语句
使用explain
分析查询预计
explain select * from test where a=2\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: test
partitions: NULL
type: ref
possible_keys: a
key: a
key_len: 4
ref: const
rows: 3
filtered: 100.00
Extra: Using index
1 row in set, 1 warning (0.00 sec)
- 查询结果分析
id
:select
标识符,select
查询的序列号select_type
:select
查询的类型,可以为一下几种类型:
simple
简单的查询,不包括连接查询和子查primary
主查询 ,或者是最外面的查询union
连接查询 的第二个或者后面的查询dependent union
联合查询的第二个查询或者后面的查询语句union result
连接查询的结果subquery
子查询的第一个select
语句dependent subquery
子查询的第一个select
语句,取决于外面的查询
table
:查询的表- type:表示表的连接类型
1.system
:mysql
系统的仅有一行的系统表,是const
类型的特例
2.const
: 数据表最多有一个匹配行,它将在查询时被读取,并在余下的查询优化中当做常量对待,const查询会很快,因为它只读取一次,const使用在常量值比较主键或者唯一索引所有部分的场合。
3.eq_ref
:当表的索引是unique
或者primary key的
时候(索引的全部都在where里面),where条件使用=
比较且比较的字段都是这两种索引的时候,就可以使用这种索引
```
select * from ref_table, other_table where ref_table.key_column=other_table.column;
select * from ref_table, other_table where ref_table.key_column_part1 = other_table.column1 and ref_table.key_column_part2 = other_table.column2;
```
4.ref
:当使用的索引的值不是唯一的时候,即使用的索引不是unique
或者primary key
的全部的时候(索引的左子集),ref能使用= <=> 比较
select * from ref_table where not_all_unique_primary_key = value;
select * from ref_table, other_table where ref_table.not_all_unique_primary_key = other_table.column;
select * from ref_table ,other_table where ref_table.key_column_part1=other_table.column and ref_table.key_column_part2=2;
5.index_or_null
: 同ref
,但是索引的字段可以使用null
select * from ref_table where key_column = value or key_colmn is null
6.index_merge
:该类型表示使用了索引合并优化方法,key
会列出包含索引的清单,key_len
包含了使用索引的最长的关键元素
7.unique_subquery
:该类型替换了in
子查询的ref
value in (select primary_key form table where some_expr)
unique_subquery
是一个索引查找函数,完全可以可以替换子查询,效率更高。
8.index_subquery
:该连接类型类似unique_subquery
,但是只能用于以下类型
value in (select key_column form single_table where some_expr)
9.range
:只检索给定范围的行,key
列会显示使用的索引,key_len
列包含了索引关键的最长关键元素。
当使用 =、<>、>、<、>=、<=、is null 、!= 、between 或者in
的操作符,用常量比较关键字列时,类型为range
select * from table where key_column = 1;
select * from table where key_column in (1,2,6);
select * from table where key_part1 = 1 where key_part2 in (2,3,4);
10.index
:直接扫描索引树。
11.all
:全表扫描,应该避免这种情况
possible_keys
:mysql
能使用哪些索引找到 该行,如果是null,表示无法使用索引找到该行key
:表示实际找到该行使用到的索引,如果没有使用使用到索引则该行为null,若要强制或者忽略possible_keys
的值,可以使用force index
、use index
或者ignore index
key_len
:表示索引字段按字节计算的长度,如果键是Null
,则长度为null
,通过key_null
可以确定mysql
将实际使用一个多列索引的几个字段ref
:表示使用哪个列或常数与索引一起查询记录rows
:显示mysql
在表中进行查询的行数extra
:表示在mysql
中查询处理的详细信息