全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

5分钟了解MySQL5.7中union all用法的黑科技

union all在MySQL5.6下的表现

Part1:MySQL5.6.25

[root@HE1 ~]# MySQL -uroot -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.25-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select version();
+------------+
| version() |
+------------+
| 5.6.25-log |
+------------+
1 row in set (0.26 sec)
  
mysql> explain (select id from helei order by id) union all (select id from t where id=0 order by id);
+----+--------------+------------+-------+---------------+--------+---------+------+------+-----------------+
| id | select_type | table   | type | possible_keys | key  | key_len | ref | rows | Extra      |
+----+--------------+------------+-------+---------------+--------+---------+------+------+-----------------+
| 1 | PRIMARY   | helei   | index | NULL     | idx_c1 | 4    | NULL | 5219 | Using index   |
| 2 | UNION    | t     | ALL  | NULL     | NULL  | NULL  | NULL |  1 | Using where   |
| NULL | UNION RESULT | <union1,2> | ALL  | NULL     | NULL  | NULL  | NULL | NULL | Using temporary |
+----+--------------+------------+-------+---------------+--------+---------+------+------+-----------------+
3 rows in set (0.00 sec)

可以看出,在MySQL5.6版本中,执行结果如下图所示:

从执行计划来看,是把helei表的查询结果和t表的查询结果合并在了一张临时表里,然后输出给客户端。

union all在MySQL5.7/MariaDB10.1下的表现

Part1:MySQL5.7.15

[root@HE1 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.15-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select version();
+------------+
| version() |
+------------+
| 5.7.15-log |
+------------+
1 row in set (0.00 sec)、
mysql> explain (select id from helei order by id) union all (select id from t where id=0 order by id);
+----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref | rows | filtered | Extra    |
+----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+-------------+
| 1 | PRIMARY   | helei | NULL    | index | NULL     | idx_c1 | 4    | NULL | 5212 |  100.00 | Using index |
| 2 | UNION    | t   | NULL    | ALL  | NULL     | NULL  | NULL  | NULL |  1 |  100.00 | Using where |
+----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+-------------+
2 rows in set, 1 warning (0.00 sec)

可以看出,在MySQL5.7版本中,执行结果如下图所示:

Part2:MariaDB10.1.16

[root@HE3 ~]# /usr/local/mariadb/bin/mysql -uroot -S /tmp/mariadb.sock 
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 10.1.16-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
MariaDB [helei]> explain (select id from helei order by id) union all (select id from t where id=0 order by id);
+------+-------------+-------+-------+---------------+--------+---------+------+------+-------------+
| id  | select_type | table | type | possible_keys | key  | key_len | ref | rows | Extra    |
+------+-------------+-------+-------+---------------+--------+---------+------+------+-------------+
|  1 | PRIMARY   | helei | index | NULL     | idx_c1 | 4    | NULL | 5198 | Using index |
|  2 | UNION    | t   | ALL  | NULL     | NULL  | NULL  | NULL |  1 | Using where |
+------+-------------+-------+-------+---------------+--------+---------+------+------+-------------+
2 rows in set (0.00 sec)

可以看出在MariaDB10.1中,执行结果如下图所示:

从执行结果看,无论是MySQL5.7还是MariaDB10.1,都没有创建临时表,按照顺序,helei表的查询结果首先输出到客户端,然后t表的查询结果再输出到客户端。

本文中的优化只针对union all,对union和在最外层使用order by无效。如下图是所示: 


——总结——

在MySQL5.7/MariaDB10.1中,union all不再创建临时表,这样在联合查询时会减少I/O开销,在MySQL5.5/5.6中则不具备这一特性。

以上所述是小编给大家介绍的5分钟了解MySQL5.7中union all用法的黑科技,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!


# mysql  # union  # all用法  # MySQL中关键字UNION和UNION ALL的区别  # MySQL之union和union all的使用及区别说明  # 简单聊一聊SQL中的union和union all  # 带例子详解Sql中Union和Union ALL的区别  # MySQL系列理解运用union(all)与limit及exists关键字教程  # 简单了解MySQL union all与union的区别  # MySQL中UNION与UNION ALL的基本使用方法  # 浅析mysql union和union all  # SQL语句之Union和Union All的用法  # SQL中UNION与UNION ALL的区别小结  # 所示  # 查询结果  # 如下图  # 可以看出  # 客户端  # 小编  # 这一  # 在此  # 并在  # 给大家  # 不具备  # 所述  # 给我留言  # 感谢大家  # 疑问请  # 有任何  # 最外层  # registered  # names  # Corporation 


相关文章: 建站主机与服务器功能差异如何区分?  如何快速上传建站程序避免常见错误?  网站专业制作公司有哪些,做一个公司网站要多少钱?  建站主机选哪种环境更利于SEO优化?  建站之星微信建站一键生成小程序+多端营销系统  网站制作哪家好,cc、.co、.cm哪个域名更适合做网站?  唐山网站制作公司有哪些,唐山找工作哪个网站最靠谱?  清除minerd进程的简单方法  自助网站制作软件,个人如何自助建网站?  兔展官网 在线制作,怎样制作微信请帖?  如何通过VPS建站实现广告与增值服务盈利?  制作营销网站公司,淘特是干什么用的?  c# 在高并发场景下,委托和接口调用的性能对比  高防服务器:AI智能防御DDoS攻击与数据安全保障  香港服务器网站推广:SEO优化与外贸独立站搭建策略  陕西网站制作公司有哪些,陕西凌云电器有限公司官网?  如何通过虚拟机搭建网站?详细步骤解析  如何在腾讯云免费申请建站?  手机怎么制作网站教程步骤,手机怎么做自己的网页链接?  Swift开发中switch语句值绑定模式  如何高效搭建专业期货交易平台网站?  网站制作公司广州有几家,广州尚艺美发学校网站是多少?  如何通过虚拟主机空间快速建站?  如何在云指建站中生成FTP站点?  深圳网站制作的公司有哪些,dido官方网站?  建站之星上传入口如何快速找到?  如何快速生成凡客建站的专业级图册?  制作电商网页,电商供应链怎么做?  建站之星如何助力网站排名飙升?揭秘高效技巧  微课制作网站有哪些,微课网怎么进?  简单实现Android文件上传  电商网站制作多少钱一个,电子商务公司的网站制作费用计入什么科目?  建站之星如何助力企业快速打造五合一网站?  建站主机选择指南:服务器配置与SEO优化实战技巧  代刷网站制作软件,别人代刷火车票靠谱吗?  官网自助建站平台指南:在线制作、快速建站与模板选择全解析  三星网站视频制作教程下载,三星w23网页如何全屏?  如何将凡科建站内容保存为本地文件?  湖南网站制作公司,湖南上善若水科技有限公司做什么的?  如何用已有域名快速搭建网站?  香港服务器租用费用高吗?如何避免常见误区?  如何实现建站之星域名转发设置?  学生网站制作软件,一个12岁的学生写小说,应该去什么样的网站?  制作ppt免费网站有哪些,有哪些比较好的ppt模板下载网站?  车管所网站制作流程,交警当场开简易程序处罚决定书,在交警网站查询不到怎么办?  如何获取开源自助建站系统免费下载链接?  如何快速搭建FTP站点实现文件共享?  如何用手机制作网站和网页,手机移动端的网站能制作成中英双语的吗?  教学论文网站制作软件有哪些,写论文用什么软件 ?  如何破解联通资金短缺导致的基站建设难题? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。