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 】
相关推荐:
公司网站制作需要多少钱,找人做公司网站需要多少钱?
制作旅游网站html,怎样注册旅游网站?
Laravel事件和监听器如何实现_Laravel Events & Listeners解耦应用的实战教程
如何在HTML表单中获取用户输入并用JavaScript动态控制复利计算循环
如何在 Python 中将列表项按字母顺序编号(a.、b.、c. …)
在线ppt制作网站有哪些软件,如何把网页的内容做成ppt?
EditPlus中的正则表达式实战(5)
如何在浏览器中启用Flash_2025年继续使用Flash Player的方法【过时】
如何在沈阳梯子盘古建站优化SEO排名与功能模块?
轻松掌握MySQL函数中的last_insert_id()
如何彻底卸载建站之星软件?
湖南网站制作公司,湖南上善若水科技有限公司做什么的?
Python函数文档自动校验_规范解析【教程】
香港服务器网站卡顿?如何解决网络延迟与负载问题?
javascript事件捕获机制【深入分析IE和DOM中的事件模型】
LinuxCD持续部署教程_自动发布与回滚机制
Laravel如何实现URL美化Slug功能_Laravel使用eloquent-sluggable生成别名【方法】
微信小程序 五星评分(包括半颗星评分)实例代码
个人摄影网站制作流程,摄影爱好者都去什么网站?
phpredis提高消息队列的实时性方法(推荐)
JavaScript如何实现音频处理_Web Audio API如何工作?
Laravel怎么实现模型属性转换Casting_Laravel自动将JSON字段转为数组【技巧】
如何快速搭建高效WAP手机网站?
利用vue写todolist单页应用
悟空浏览器如何设置小说背景色_悟空浏览器背景色设置【方法】
Laravel如何安装Breeze扩展包_Laravel用户注册登录功能快速实现【流程】
Laravel如何实现数据库事务?(DB Facade示例)
如何在阿里云香港服务器快速搭建网站?
Laravel怎么实现前端Toast弹窗提示_Laravel Session闪存数据Flash传递给前端【方法】
香港代理服务器配置指南:高匿IP选择、跨境加速与SEO优化技巧
网站建设整体流程解析,建站其实很容易!
微信推文制作网站有哪些,怎么做微信推文,急?
Laravel如何将应用部署到生产服务器_Laravel生产环境部署流程
uc浏览器二维码扫描入口_uc浏览器扫码功能使用地址
如何获取PHP WAP自助建站系统源码?
Laravel Facade的原理是什么_深入理解Laravel门面及其工作机制
Laravel策略(Policy)如何控制权限_Laravel Gates与Policies实现用户授权
佐糖AI抠图怎样调整抠图精度_佐糖AI精度调整与放大细化操作【攻略】
清除minerd进程的简单方法
Laravel如何构建RESTful API_Laravel标准化API接口开发指南
Java类加载基本过程详细介绍
Laravel安装步骤详细教程_Laravel环境搭建指南
简历在线制作网站免费版,如何创建个人简历?
非常酷的网站设计制作软件,酷培ai教育官方网站?
如何在阿里云虚拟主机上快速搭建个人网站?
如何快速搭建二级域名独立网站?
Laravel如何实现API资源集合?(Resource Collection教程)
html如何与html链接_实现多个HTML页面互相链接【互相】
Laravel如何使用Gate和Policy进行权限控制_Laravel权限判定与策略规则配置
Laravel怎么创建自己的包(Package)_Laravel扩展包开发入门到发布
上一篇:《卡拉彼丘》最高段位介绍
下一篇:laravel prs4加载方法
上一篇:《卡拉彼丘》最高段位介绍
下一篇:laravel prs4加载方法

