MySQL5.7新特性之优化union all

 MySQL5.6中,使用union all相当于创建一张临时表,这在执行大的联合查询时候会增加I/O开销,降低查询速度。
 例如执行以下SQL语句:
 (select id from accessLog order by id) union all (select id from access_test order by id);
 在MySQL5.6环境:

点击(此处)折叠或打开

  1. mysql> select version();
  2. +------------+
  3. | version() |
  4. +------------+
  5. | 5.6.14-log |
  6. +------------+
  7. 1 row in set (0.00 sec)
  8. mysql> explain (select id from accessLog order by id) union all (select id from access_test order by id);
  9. +----+--------------+-------------+-------+---------------+--------------+---------+------+--------+-----------------+
  10. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  11. +----+--------------+-------------+-------+---------------+--------------+---------+------+--------+-----------------+
  12. | 1 | PRIMARY | accessLog | index | NULL | loginuserId | 9 | NULL | 535513 | Using index |
  13. | 2 | UNION | access_test | index | NULL | idx_loginuid | 9 | NULL | 477248 | Using index |
  14. | NULL | UNION RESULT | <union1,2> | ALL | NULL | NULL | NULL | NULL | NULL | Using temporary |
  15. +----+--------------+-------------+-------+---------------+--------------+---------+------+--------+-----------------+
  可以看到执行计划中提现到了创建的临时表。
 在MySQL5.7环境:

点击(此处)折叠或打开

  1. mysql> select version();
  2. +------------+
  3. | version() |
  4. +------------+
  5. | 5.7.18-log |
  6. +------------+
  7. 1 row in set (0.00 sec)
  8. mysql> explain (select id from accessLog order by id) union all (select id from access_test order by id);
  9. +----+-------------+-------------+------------+-------+---------------+-------------+---------+------+--------+----------+-------------+
  10. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  11. +----+-------------+-------------+------------+-------+---------------+-------------+---------+------+--------+----------+-------------+
  12. | 1 | PRIMARY | accessLog | NULL | index | NULL | loginuserId | 9 | NULL | 586090 | 100.00 | Using index |
  13. | 2 | UNION | access_test | NULL | ALL | NULL | NULL | NULL | NULL | 571023 | 100.00 | NULL |
  14. +----+-------------+-------------+------------+-------+---------------+-------------+---------+------+--------+----------+-------------+
  整个查询过程没有创建临时表,按照顺序,accessLog表的查询结果首先传输到客户端,然后access_test表的查询结果再传输到客户端。 注意:此项优化对union和在最外层用order by无效,如下:

点击(此处)折叠或打开

  1. mysql> select version();
  2. +------------+
  3. | version() |
  4. +------------+
  5. | 5.7.18-log |
  6. +------------+
  7. 1 row in set (0.00 sec)
  8. mysql> explain (select id from accessLog order by id) union all (select id from access_test order by id) order by id;
  9. +----+--------------+-------------+------------+-------+---------------+-------------+---------+------+--------+----------+---------------------------------+
  10. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  11. +----+--------------+-------------+------------+-------+---------------+-------------+---------+------+--------+----------+---------------------------------+
  12. | 1 | PRIMARY | accessLog | NULL | index | NULL | loginuserId | 9 | NULL | 586090 | 100.00 | Using index |
  13. | 2 | UNION | access_test | NULL | ALL | NULL | NULL | NULL | NULL | 571023 | 100.00 | NULL |
  14. | NULL | UNION RESULT | <union1,2> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary; Using filesort |
  15. +----+--------------+-------------+------------+-------+---------------+-------------+---------+------+--------+----------+---------------------------------+
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值