美文网首页
慢sql优化

慢sql优化

作者: vale_nbg | 来源:发表于2018-04-24 14:15 被阅读0次

背景:通过阿里云RDS后台,查看慢SQL,发现如下慢sql。

SELECT
  T0.`id`,
  T0.`instance_i_d`,
  T0.`create_user_id`,
  T0.`update_user_id`,
  T0.`create_date`,
  T0.`update_date`,
  T0.`sales_man_tpl_id`,
  T0.`order_id`,
  T0.`channel_tpl_id`,
  T0.`is_emerge`,
  T1.`id`,
  T1.`instance_i_d`,
  T1.`create_user_id`,
  T1.`update_user_id`,
  T1.`create_date`,
  T1.`update_date`,
  T1.`name`,
  T1.`contact`,
  T1.`tel`,
  T1.`wechat`,
  T1.`delete_at`,
  T2.`id`,
  T2.`instance_i_d`,
  T2.`create_user_id`,
  T2.`update_user_id`,
  T2.`create_date`,
  T2.`update_date`,
  T2.`name`,
  T2.`identity`,
  T2.`birthday`,
  T2.`customer_sex`,
  T2.`mobile`,
  T2.`ali_pay`,
  T2.`address`,
  T2.`job_number`,
  T2.`department`,
  T2.`supervisor_tpl_id`,
  T2.`staff_status`,
  T2.`entry_work_date`,
  T2.`regular_work_date`,
  T2.`leave_work_date`,
  T2.`remark`,
FROM `xxx_order` T0 LEFT OUTER JOIN `xxx_channel` T1 ON T1.`id` = T0.`channel_tpl_id`
  LEFT OUTER JOIN `xxx_staff` T2 ON T2.`id` = T0.`sales_man_tpl_id`
GROUP BY T0.`id`
ORDER BY T0.`id` DESC
LIMIT 20

分析原因:
使用mysql explain

mysql> explain SELECT
  T0.`id`,
  T0.`instance_i_d`,
  T0.`create_user_id`,
  T0.`update_user_id`,
  T0.`create_date`,
  T0.`update_date`,
  T0.`sales_man_tpl_id`,
  T0.`order_id`,
  T0.`channel_tpl_id`,
  T0.`is_emerge`,
  T1.`id`,
  T1.`instance_i_d`,
  T1.`create_user_id`,
  T1.`update_user_id`,
  T1.`create_date`,
  T1.`update_date`,
  T1.`name`,
  T1.`contact`,
  T1.`tel`,
  T1.`wechat`,
  T1.`delete_at`,
  T2.`id`,
  T2.`instance_i_d`,
  T2.`create_user_id`,
  T2.`update_user_id`,
  T2.`create_date`,
  T2.`update_date`,
  T2.`name`,
  T2.`identity`,
  T2.`birthday`,
  T2.`customer_sex`,
  T2.`mobile`,
  T2.`ali_pay`,
  T2.`address`,
  T2.`job_number`,
  T2.`department`,
  T2.`supervisor_tpl_id`,
  T2.`staff_status`,
  T2.`entry_work_date`,
  T2.`regular_work_date`,
  T2.`leave_work_date`,
  T2.`remark`,
FROM `xxx_order` T0 LEFT OUTER JOIN `xxx_channel` T1 ON T1.`id` = T0.`channel_tpl_id`
  LEFT OUTER JOIN `xxx_staff` T2 ON T2.`id` = T0.`sales_man_tpl_id`
GROUP BY T0.`id`
ORDER BY T0.`id` DESC
LIMIT 20;
+----+-------------+-------+--------+------------------------------------+---------+---------+--------------------------------+------+----------------------------------------------------+
| id | select_type | table | type   | possible_keys                      | key     | key_len | ref                            | rows | Extra                                              |
+----+-------------+-------+--------+------------------------------------+---------+---------+--------------------------------+------+----------------------------------------------------+
|  1 | SIMPLE      | T0    | ALL    | PRIMARY,fg_visa_order_instance_i_d | NULL    | NULL    | NULL                           | 3464 | Using temporary; Using filesort                    |
|  1 | SIMPLE      | T1    | ALL    | PRIMARY                            | NULL    | NULL    | NULL                           |    1 | Using where; Using join buffer (Block Nested Loop) |
|  1 | SIMPLE      | T2    | eq_ref | PRIMARY                            | PRIMARY | 8       | xxx.T0.sales_man_tpl_id |    1 | NULL                                               |
+----+-------------+-------+--------+------------------------------------+---------+---------+--------------------------------+------+----------------------------------------------------+
7 rows in set (0.01 sec)

这里可知left join T1表的时候,没有用索引,所以整个sql的执行时间被拖慢了。但是很奇怪的是T1和T0的关系,T2和T0的关系,完全一致,为什么T2和T0使用外键查询,而T1却不使用?

查看T1 表,然后insert一条数据,数据量从6 -> 7

mysql> select * from xxx_channel;
+----+--------------+----------------+----------------+---------------------+---------------------+--------------+---------+-------------+--------------+-----------+
| id | instance_i_d | create_user_id | update_user_id | create_date         | update_date         | name         | contact | tel         | wechat       | delete_at |
+----+--------------+----------------+----------------+---------------------+---------------------+--------------+---------+-------------+--------------+-----------+
|  1 |          1484 |            1484 |            1484 | 2018-03-07 17:18:05 | 2018-03-07 17:18:05 | 自有渠道     |         |             |              |         0 |
|  2 |          1485 |            1485 |            1485 | 2018-03-07 20:55:18 | 2018-03-07 20:55:18 | 线下渠道     | 小李    | 15688779966 | 微微儿额     |         0 |
|  3 |          1429 |            1429 |           1429 | 2018-04-09 11:13:28 | 2018-04-09 11:13:28 | 杜微信       |         |             |              |         0 |
|  4 |          1429 |            1429 |            1429 | 2018-04-15 17:05:11 | 2018-04-15 17:05:11 | 同行小周     | 小周    |             |              |         0 |
|  5 |          1429 |            1429 |            1429 | 2018-04-15 17:05:26 | 2018-04-15 17:05:26 | 同行小邵     | 小邵    |             |              |         0 |
|  6 |          1429 |            1429 |            1429 | 2018-04-15 17:05:48 | 2018-04-15 17:05:48 | 不详         | 不详    |             |              |         0 |
+----+--------------+----------------+----------------+---------------------+---------------------+--------------+---------+-------------+--------------+-----------+
6 rows in set (0.00 sec)

mysql> insert into fg_channel (instance_i_d, create_date, update_date, name, delete_at) values(0, now(), now(), "for_slow_sql", 1);
Query OK, 1 row affected (0.00 sec)

mysql> select * from xxx_channel;
+----+--------------+----------------+----------------+---------------------+---------------------+--------------+---------+-------------+--------------+-----------+
| id | instance_i_d | create_user_id | update_user_id | create_date         | update_date         | name         | contact | tel         | wechat       | delete_at |
+----+--------------+----------------+----------------+---------------------+---------------------+--------------+---------+-------------+--------------+-----------+
|  1 |          1484 |            1484 |            1484 | 2018-03-07 17:18:05 | 2018-03-07 17:18:05 | 自有渠道     |         |             |              |         0 |
|  2 |          1485 |            1485 |            1485 | 2018-03-07 20:55:18 | 2018-03-07 20:55:18 | 线下渠道     | 小李    | 15688779966 | 微微儿额     |         0 |
|  3 |          1429 |            1429 |            1429 | 2018-04-09 11:13:28 | 2018-04-09 11:13:28 | 杜微信       |         |             |              |         0 |
|  4 |          1429 |            1429 |            1429 | 2018-04-15 17:05:11 | 2018-04-15 17:05:11 | 同行小周     | 小周    |             |              |         0 |
|  5 |          1429 |            1429 |            1429 | 2018-04-15 17:05:26 | 2018-04-15 17:05:26 | 同行小邵     | 小邵    |             |              |         0 |
|  6 |          1429 |            1429 |            1429 | 2018-04-15 17:05:48 | 2018-04-15 17:05:48 | 不详         | 不详    |             |              |         0 |
|  7 |            0 |           NULL |           NULL | 2018-04-24 13:41:02 | 2018-04-24 13:41:02 | for_slow_sql |         |             |              |         1 |
+----+--------------+----------------+----------------+---------------------+---------------------+--------------+---------+-------------+--------------+-----------+

再次执行explain

mysql> explain SELECT
  T0.`id`,
  T0.`instance_i_d`,
  T0.`create_user_id`,
  T0.`update_user_id`,
  T0.`create_date`,
  T0.`update_date`,
  T0.`sales_man_tpl_id`,
  T0.`order_id`,
  T0.`channel_tpl_id`,
  T0.`is_emerge`,
  T1.`id`,
  T1.`instance_i_d`,
  T1.`create_user_id`,
  T1.`update_user_id`,
  T1.`create_date`,
  T1.`update_date`,
  T1.`name`,
  T1.`contact`,
  T1.`tel`,
  T1.`wechat`,
  T1.`delete_at`,
  T2.`id`,
  T2.`instance_i_d`,
  T2.`create_user_id`,
  T2.`update_user_id`,
  T2.`create_date`,
  T2.`update_date`,
  T2.`name`,
  T2.`identity`,
  T2.`birthday`,
  T2.`customer_sex`,
  T2.`mobile`,
  T2.`ali_pay`,
  T2.`address`,
  T2.`job_number`,
  T2.`department`,
  T2.`supervisor_tpl_id`,
  T2.`staff_status`,
  T2.`entry_work_date`,
  T2.`regular_work_date`,
  T2.`leave_work_date`,
  T2.`remark`,
FROM `xxx_order` T0 LEFT OUTER JOIN `xxx_channel` T1 ON T1.`id` = T0.`channel_tpl_id`
  LEFT OUTER JOIN `xxx_staff` T2 ON T2.`id` = T0.`sales_man_tpl_id`
GROUP BY T0.`id`
ORDER BY T0.`id` DESC
LIMIT 20;;
+----+-------------+-------+--------+------------------------------------+---------+---------+-------------------------+------+-------+
| id | select_type | table | type   | possible_keys                      | key     | key_len | ref                     | rows | Extra |
+----+-------------+-------+--------+------------------------------------+---------+---------+-------------------------+------+-------+
|  1 | SIMPLE      | T0    | index  | PRIMARY,fg_visa_order_instance_i_d | PRIMARY | 8       | NULL                    |   20 | NULL  |
|  1 | SIMPLE      | T1    | eq_ref | PRIMARY                            | PRIMARY | 8       | xxx.T0.channel_tpl_id   |    1 | NULL  |
|  1 | SIMPLE      | T2    | eq_ref | PRIMARY                            | PRIMARY | 8       | xxx.T0.sales_man_tpl_id |    1 | NULL  |
+----+-------------+-------+--------+------------------------------------+---------+---------+-------------------------+------+-------+
7 rows in set (0.00 sec)

神奇的是这次使用外键关联了,猜测mysql根据数据库的数量自己决定是否使用外键查询。

相关文章

  • 数据库索引相关问题

    如何定位并优化慢查询Sql 根据慢日志定位慢查询sql 使用explain等工具分析sql 修改sql或者尽量让s...

  • 性能分析与 EXPLAIN 详解

    一、sql查询慢原因及优化 1、导致慢 SQL 的原因 在遇到慢 SQL 情况时,不能简单的把原因归结为 SQL ...

  • 面试八股文(一) mySQL的select语句该如何优化?

    1. 慢SQL定位与分析 做SQL优化的第一步,就是确定慢SQL,分析出它速度慢的原因。通常,我们通过查看慢SQL...

  • MySQL慢查询日志相关笔记

    MySQL优化SQL,针对慢SQL语句的查询及相关配置。相关命令: 需要永久开启慢SQL查询日志,需要在my.in...

  • 2018-07-01

    SQL优化改写之美--MySQL虚拟列、伪列实现对SQL的优化 慢SQL文本如下: SQL执行时长达38S,获取3...

  • MySQL慢查询:慢SQL定位、日志分析与优化方案

    一个sql执行很慢的就叫慢sql,一般来说sql语句执行超过5s就能够算是慢sql,需要进行优化了 为何要对慢SQ...

  • MySQL实战14 慢查询优化join、order by、gro

    1.慢查询的优化思路 1.1优化更需要优化的SQL 优化SQL是有成本的高并发低消耗的比低并发高消耗影响更大 优化...

  • 慢sql优化

    背景:通过阿里云RDS后台,查看慢SQL,发现如下慢sql。 分析原因:使用mysql explain 这里可知l...

  • 慢SQL优化

    前言 慢SQL会增加数据库压力,影响系统的访问速度及用户体验。 影响查询效率的因素 1、全表扫描。 2、无...

  • 慢sql优化

    1.组合索引中字段不能为空,为空会失效,要添加~之类的默认值。2.count(*)会优化成count(1)3.co...

网友评论

      本文标题:慢sql优化

      本文链接:https://www.haomeiwen.com/subject/dccjlftx.html