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

发布时间 - 2026-01-11 00:39:50    点击率:

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 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: 如何利用DOS批处理实现定时关机操作详解  Laravel如何使用Guzzle调用外部接口_Laravel发起HTTP请求与JSON数据解析【详解】  如何自己制作一个网站链接,如何制作一个企业网站,建设网站的基本步骤有哪些?  Laravel Asset编译怎么配置_Laravel Vite前端构建工具使用  移动端脚本框架Hammer.js  简历在线制作网站免费版,如何创建个人简历?  如何快速使用云服务器搭建个人网站?  Win11怎么查看显卡温度 Win11任务管理器查看GPU温度【技巧】  实现点击下箭头变上箭头来回切换的两种方法【推荐】  韩国网站服务器搭建指南:VPS选购、域名解析与DNS配置推荐  如何快速搭建高效WAP手机网站?  大连网站制作费用,大连新青年网站,五年四班里的视频怎样下载啊?  如何登录建站主机?访问步骤全解析  高端云建站费用究竟需要多少预算?  网站制作怎么样才能赚钱,用自己的电脑做服务器架设网站有什么利弊,能赚钱吗?  Laravel如何处理表单验证?(Requests代码示例)  Win11怎么关闭资讯和兴趣_Windows11任务栏设置隐藏小组件  Laravel如何实现多对多模型关联?(Eloquent教程)  Android滚轮选择时间控件使用详解  如何用PHP快速搭建高效网站?分步指南  Linux系统命令中tree命令详解  Win11怎么更改系统语言为中文_Windows11安装语言包并设为显示语言  Laravel 419 page expired怎么解决_Laravel CSRF令牌过期处理  html5的keygen标签为什么废弃_替代方案说明【解答】  活动邀请函制作网站有哪些,活动邀请函文案?  如何为不同团队 ID 动态生成多个独立按钮  如何自定义建站之星模板颜色并下载新样式?  三星、SK海力士获美批准:可向中国出口芯片制造设备  Laravel如何发送邮件和通知_Laravel邮件与通知系统发送步骤  如何在万网开始建站?分步指南解析  广州网站制作公司哪家好一点,广州欧莱雅百库网络科技有限公司官网?  如何在服务器上配置二级域名建站?  如何在沈阳梯子盘古建站优化SEO排名与功能模块?  轻松掌握MySQL函数中的last_insert_id()  Laravel N+1查询问题如何解决_Eloquent预加载(Eager Loading)优化数据库查询  Bootstrap整体框架之JavaScript插件架构  Laravel如何使用Socialite实现第三方登录?(微信/GitHub示例)  laravel怎么实现图片的压缩和裁剪_laravel图片压缩与裁剪方法  装修招标网站设计制作流程,装修招标流程?  JavaScript模板引擎Template.js使用详解  利用python获取某年中每个月的第一天和最后一天  Mybatis 中的insertOrUpdate操作  利用JavaScript实现拖拽改变元素大小  宙斯浏览器文件分类查看教程 快速筛选视频文档与图片方法  Linux系统命令中screen命令详解  详解Android中Activity的四大启动模式实验简述  Laravel怎么设置路由分组Prefix_Laravel多级路由嵌套与命名空间隔离【步骤】  黑客如何通过漏洞一步步攻陷网站服务器?  ai格式如何转html_将AI设计稿转换为HTML页面流程【页面】  再谈Python中的字符串与字符编码(推荐)