文章目录
一、影响服务器性能的几个方面
1.硬件条件原因
- 服务器硬件:
内存大小等。 - 服务器的操作系统:
不同系统(Linux、Windows)。 - 数据库存储引擎的选择:
根据不同的需求选择不同的存储引擎。 - 数据库参数配置:
不同的参数配置会影响到性能。 - 数据库结构设计和SQL语句:
拆表后,小表的查询效率更快。
2.SQL本身性能下降原因
- 查询语句写的不好:
语句太长,有太多连接。 - 索引失效:
建立的索引未用上。 - 关联查询太多join:
有太多连接,降低性能。 - 服务器调优及各个参数设置。
3.SQL语句加载顺序
手写SQL代码的顺序:
select distinct
<select _list>
from
<left_table>
join <right_table> on <join_codition>
where
<where_condition>
group by
<group_by_list>
having
<having_condition>
order by
<order_by_condition>
limit <limit number>
机器读取执行代码的顺序:
如图
4.MySQL常见瓶颈
- CPU:
CPU在饱和的时候一般发生在数据装入内存或从磁盘读取数据的时候; - IO:
磁盘I/O瓶颈发生在装入数据远大于内存容量的时候; - 服务器硬件的性能瓶颈:
内存、硬盘大小等。
二、explain分析SQL
1.基本定义
使用explain关键字可以模拟优化器执行SQL查询语句,从而知道MySQL是如何处理SQL语句的。
2.explain的作用:
- 表的读取顺序;
- 数据读取操作的操作类型;
- 哪些索引可以使用;
- 哪些索引被实际使用;
- 表之间的引用;
- 每张表有多少行被优化器查询。
3.explain的使用
语法:
explain + SQL语句;
例如
explain select * from students;
打印
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------+
| 1 | SIMPLE | students | NULL | ALL | NULL | NULL | NULL | NULL | 27 | 100.00 | NULL |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
4.explain字段解释
id:
表的读取顺序:
select查询的顺序号,包含一组数字,表示查询中执行select子句或操作表的顺序。
两种情况:
(1)id相同,执行顺序由上至下:
例如:
explain select test2.* from test1,test2,test3 where test1.id = test2.id and test1.id = test3.id;
打印
+----+-------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+-------------+
| 1 | SIMPLE | test1 | NULL | index | PRIMARY | PRIMARY | 4 | NULL | 1 | 100.00 | Using index |
| 1 | SIMPLE | test2 | NULL | eq_ref | PRIMARY | PRIMARY | 4 | demo.test1.id | 1 | 100.00 | Using index |
| 1 | SIMPLE | test3 | NULL | eq_ref | PRIMARY | PRIMARY | 4 | demo.test1.id | 1 | 100.00 | Using index |
+----+-------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+-------------+
3 rows in set, 1 warning (0.00 sec)
(2)id不同,如果是子查询,id的序号会递增,id值越大优先级越高,越先被执行:
例如:
explain select test2.* from test2 where id = (select id from test1 where id = (select test3.id from test3 where test3.other_columns = ''));
打印
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+
| 1 | PRIMARY | test2 | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | Using index |
| 2 | SUBQUERY | test1 | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | Using index |
| 3 | SUBQUERY | test3 | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | Using where |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+
3 rows in set, 1 warning (0.00 sec)
显然,根据id的大小,表读取的顺序依次是test3、test1、test2,先执行子查询,再执行父查询,从底层到上层依次执行。
(3)id相同不同:
id时同时执行,不同时id越大越先被执行。
select_type:
数据读取操作的操作类型:
(1)simple:
简单的select查询,查询中不包含子查询或者union;
(2)primary:
查询中若包含任何复杂的子部分,最外层查询则被标记,就是最后加载的那一层查询;
(3)subquery:
在select或where列表中包含了子查询;
(4)union:
union是把两个表的查询结果连接起来。
若第二个select出现在union之后,则被标记为union,
explain select * from test2 union select * from test4;
打印
+----+--------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+
| 1 | PRIMARY | test2 | NULL | index | NULL | PRIMARY | 4 | NULL | 1 | 100.00 | Using index |
| 2 | UNION | test4 | NULL | index | NULL | PRIMARY | 4 | NULL | 1 | 100.00 | Using index |
| NULL | UNION RESULT | <union1,2> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary
+----+--------------+------------+------------+-------+---------------+---------+---------+------+------+----------+-----------------+
3 rows in set, 1 warning (0.01 sec)
只有两张表的字段个数相同才能使用。
(5)union result:
从union表获取结果的select。
table:
显示这一行的数据是关于哪张表的。
partitions:
查询访问的分区。
type:
显示SQL的执行效率,包括ALL,index,range,ref,eq_ref,const、system,NULL等。
从最好到最差依次是:
system > const > eq_ref > ref > range > index > ALL。
(1)system:
表只有一行记录(等于系统表),这是const类型的特例,意义不大。
例如
explain select * from mysql.db;
打印
+----+-------------+-------+------------+---------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+---------+---------------+------+---------+------+------+----------+-------+
| 1 | SIMPLE | db | NULL | system | NULL | NULL | NULL | NULL | 6 | 100.00 | NULL |
+----+-------------+-------+------------+---------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
(2)const:
表示通过索引一次就找到了,const用于比较primary key,使用较少。
例如
explain select * from test1 where id = 1;
打印
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| 1 | SIMPLE | test1 | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.01 sec)
(3)eq_ref:
唯一索引扫描,对于每个索引键,表中只有一条记录与之匹配
例如
explain select * from test1,test2 where test1.id = test2.id;
打印
+----+-------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+-------------+
| 1 | SIMPLE | test1 | NULL | ALL | PRIMARY | NULL | NULL | NULL | 1 | 100.00 | NULL |
| 1 | SIMPLE | test2 | NULL | eq_ref | PRIMARY | PRIMARY | 4 | demo.test1.id | 1 | 100.00 | Using index |
+----+-------------+-------+------------+--------+---------------+---------+---------+---------------+------+----------+-------------+
2 rows in set, 1 warning (0.01 sec)
(4)ref:
非唯一性索引扫描,返回匹配某个单独值的所有行,本质上也是一种索引访问,它返回所有匹配某个单独值的行
例如
create index idx_col1_col2 on test3(col1,col2);
explain select * from test3 where col1 = 'test';
打印
+----+-------------+-------+------------+------+---------------+---------------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+---------------+---------+-------+------+----------+-------+
| 1 | SIMPLE | test3 | NULL | ref | idx_col1_col2 | idx_col1_col2 | 93 | const | 1 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+---------------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
(5)range:
只检索给定范围的行,使用一个索引来选择行,where语句中包含between…and…、in…条件时即为range,一般where语句中的字段是主键时才可能出现range。
例如
explain select * from test1 where id between 1 and 10;
打印
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| 1 | SIMPLE | test1 | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 1 | 100.00 | Using where |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
(6)index:
Full Index Scan,index与ALL区别为index类型只遍历索引树。
例如
explain select id from test1;
打印
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| 1 | SIMPLE | test1 | NULL | index | NULL | PRIMARY | 4 | NULL | 1 | 100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.01 sec)
(7)ALL:
遍历全表找到匹配的行。
例如
explain select * from test1;
打印
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| 1 | SIMPLE | test1 | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
possible_keys:
显示可能应用在这张表中的索引,一个或多个。
key:
实际使用的索引,如果为null,则没有使用索引。
例如
explain select col1,col2 from test3;
打印
+----+-------------+-------+------------+-------+---------------+---------------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------------+---------+------+------+----------+-------------+
| 1 | SIMPLE | test3 | NULL | index | NULL | idx_col1_col2 | 186 | NULL | 1 | 100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+---------------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.01 sec)
覆盖索引,查询列要被所建的索引覆盖,上例中查询的列col1、col2即被创建的索引idx_col1_col2覆盖。
查询中若使用了覆盖索引,则该索引仅出现在key列表中。
key_len:
表示索引中使用的字节数,可通过该列计算查询中使用的索引长度。
在不损失精确性的情况下,长度越短越好。
key_len显示的值为索引字段的最大可能长度,并非实际使用长度,即key_len是根据表定义计算而得,不是通过表内检索出的。
索引长度计算:
varchr(n)变长字段且允许NULL:
n*(Character Set:utf8=3,gbk=2,latin1=1)+1(NULL)+2(变长字段);
varchr(n)变长字段且不允许NULL:
n*(Character Set:utf8=3,gbk=2,latin1=1)+2(变长字段);
char(n)固定字段且允许NULL:
n*(Character Set:utf8=3,gbk=2,latin1=1)+1(NULL);
char(n)固定字段且不允许NULL:
n*(Character Set:utf8=3,gbk=2,latin1=1)。
上例中,字符集为utf8为3,允许非空1,可变长2,并且有2个字段,所以有
key_len=(30×3+1+2)×2=186。
ref:
显示索引哪一列被使用到了。
在id表的读取顺序部分示例的结果中,ref列的第2、3个值为demo.test1.id,表示demo数据库中的test1表中的id字段被用到。
rows:
根据表统计信息及索引选用情况,大致估算出找到所需的记录所需要读取的行数。
如图
Extra:
包含不适合在其他列中显示但十分重要的额外信息。
(1)Using filesort:
说明mysql会对数据使用一个外部的索引排序,而不是按照表内的索引顺序进行读取,MySQL中无法利用索引完成的排序操作称为"文件排序",说明需要进行优化。
例如
drop index idx_col1_col2 from test3;
create index idx_col1_col2_col3 on test3(col1,col2,col3);
explain select * from test3 where col1 = 'test' order by col3;
打印
+----+-------------+-------+------------+------+--------------------+--------------------+---------+-------+------+----------+---------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+--------------------+--------------------+---------+-------+------+----------+---------------------------------------+
| 1 | SIMPLE | test3 | NULL | ref | idx_col1_col2_col3 | idx_col1_col2_col3 | 93 | const | 1 | 100.00 | Using index condition; Using filesort |
+----+-------------+-------+------------+------+--------------------+--------------------+---------+-------+------+----------+---------------------------------------+
1 row in set, 1 warning (0.00 sec)
显然,此时出现了filesort,需要进行优化。
例如
explain select * from test3 where col1 = 'test' order by col2,col3;
打印
+----+-------------+-------+------------+------+--------------------+--------------------+---------+-------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+--------------------+--------------------+---------+-------+------+----------+-----------------------+
| 1 | SIMPLE | test3 | NULL | ref | idx_col1_col2_col3 | idx_col1_col2_col3 | 93 | const | 1 | 100.00 | Using index condition |
+----+-------------+-------+------------+------+--------------------+--------------------+---------+-------+------+----------+-----------------------+
1 row in set, 1 warning (0.01 sec)
此时不再有filesort。
(2)Using temporary:
使用临时表保存中间结果,MySQL在对查询结果排序时使用临时表,一般出现在使用了order by、group by的时候,性能极差。
例如
explain select * from test3 where col1 in ('te','st') group by col2;
打印
+----+-------------+-------+------------+------+--------------------+------+---------+------+------+----------+----------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+--------------------+------+---------+------+------+----------+----------------------------------------------+
| 1 | SIMPLE | test3 | NULL | ALL | idx_col1_col2_col3 | NULL | NULL | NULL | 1 | 100.00 | Using where; Using temporary; Using filesort |
+----+-------------+-------+------------+------+--------------------+------+---------+------+------+----------+----------------------------------------------+
1 row in set, 1 warning (0.01 sec)
显然,同时出现了filesort、temporary,需要优化。
例如
explain select * from test3 where col1 in ('te','st') group by col1,col2;
打印
+----+-------------+-------+------------+-------+--------------------+--------------------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+--------------------+--------------------+---------+------+------+----------+-------------+
| 1 | SIMPLE | test3 | NULL | index | idx_col1_col2_col3 | idx_col1_col2_col3 | 279 | NULL | 1 | 100.00 | Using where |
+----+-------------+-------+------------+-------+--------------------+--------------------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.01 sec)
此时filesort、temporary均消失,我们应该尽量按照建立索引的顺序进行分组、排序。
(3)Using index:
使用了索引,避免了全表扫描。
(4)Using where:
使用了where过滤。
(5)Using join buffer:
使用了连接缓存。
(6)impossible where:
不可能的条件,where子句的值总是false。
例如
explain select * from test3 where id = 1 and id = 2;
打印
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Impossible WHERE |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------+
1 row in set, 1 warning (0.01 sec)
欢迎大家加入群聊【python知识交流分享群】,进行技术交流:https://jq.qq.com/?_wv=1027&k=5m5AduZ