首页 数据库 mysql教程 MySQL5.7中union all用法的黑科技的图文代码介绍

MySQL5.7中union all用法的黑科技的图文代码介绍

Apr 15, 2017 am 09:31 AM

本文带领大家通过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版本中,执行结果如下图所示:

wKioL1f8bZvhzEMaAAFulp6pefo997.jpg

从执行计划来看,是把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 &#39;help;&#39; or &#39;\h&#39; for help. Type &#39;\c&#39; 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版本中,执行结果如下图所示:

wKiom1f8bijj3fJiAAF48HG3WPQ918.jpg

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 &#39;help;&#39; or &#39;\h&#39; for help. Type &#39;\c&#39; 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中,执行结果如下图所示:

wKioL1f8bmmwi9GLAAFbMJCN0uU554.jpg

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

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

wKiom1f8boazPx35AAKnKQS1Ig4776.jpg

——总结——

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

以上是MySQL5.7中union all用法的黑科技的图文代码介绍的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1655
14
CakePHP 教程
1413
52
Laravel 教程
1306
25
PHP教程
1252
29
C# 教程
1226
24
c语言union的用法 c语言union的用法 Sep 27, 2023 am 11:00 AM

c语言union的用法是一种特殊的数据类型,它允许在相同的内存位置存储不同的数据类型,union的使用可以帮助我们节省内存空间,并且可以方便地在不同的数据类型之间进行转换。使用union时需要注意对应的成员是有效的,并且只能同时访问一个成员。

MySQL中如何使用FULL OUTER JOIN函数获取两个表的并集 MySQL中如何使用FULL OUTER JOIN函数获取两个表的并集 Jul 26, 2023 pm 05:45 PM

MySQL中如何使用FULLOUTERJOIN函数获取两个表的并集在MySQL中,FULLOUTERJOIN函数是一种融合内连接和外连接的功能强大的连接操作。它可以用于获取两个表的并集,即将两个表中的所有数据合并为一个结果集。本文将介绍FULLOUTERJOIN函数的用法,并提供一些示例代码以帮助读者更好地理解。FULLOUTERJOIN函数

java如何定义Union类实现数据体的共存 java如何定义Union类实现数据体的共存 May 14, 2023 pm 03:34 PM

定义Union类实现数据体的共存在C/C++语言中,联合体(union),又称共用体,类似结构体(struct)的一种数据结构。联合体(union)和结构体(struct)一样,可以包含很多种数据类型和变量,两者区别如下:结构体(struct)中所有变量是“共存”的,同时所有变量都生效,各个变量占据不同的内存空间;联合体(union)中是各变量是“互斥”的,同时只有一个变量生效,所有变量占据同一块内存空间。当多个数据需要共享内存或者多个数据每次只取其一时,可以采用联合体(union)。在Java

怎么用mysql union实现全外连接查询 怎么用mysql union实现全外连接查询 May 30, 2023 pm 06:49 PM

1、union不是多表连接查询的一种方式,将多个查询句子的查询结果合并成一个结果,去除重复数据。2、全外连接查询左表和右表的数据,然后根据连接条件连接。实例#用左外的Aunion右外的BSELECT*FROMt_categorycLEFTOUTERJOINt_productpONc.cid=p.cnounionSELECT*FROMt_categorycRIGHTOUTERJOINt_productpONc.cid=p.cno

如何通过MySQL对UNION优化来提高性能 如何通过MySQL对UNION优化来提高性能 May 11, 2023 pm 05:40 PM

在许多数据库应用程序中,我们都会面临需要整合来自多个数据源的数据的情况。MySQL的UNION语句就是一种用来解决这种情况的方式,它允许我们将两个或多个SELECT语句的结果集合并为一个。虽然这是一个非常方便的功能,但如果不加以优化,UNION语句也可能对系统产生性能问题。本文将探讨如何通过MySQL对UNION优化来提高性能。使用UNIONALL在使用U

MySQL中怎么用Union优化Like语句 MySQL中怎么用Union优化Like语句 May 31, 2023 pm 03:55 PM

用Union优化Like语句1)有时候,你可能需要在查询中使用or操作符进行比较。当or关键字在where子句中使用频率过高的时候,它可能会使MySQL优化器错误的选择全表扫描来检索记录。union子句可以是查询执行的更快,尤其是当其中一个查询有一个优化索引,而另一个查询也有一个优化索引的时候。比如,在first_name和last_name上分别存在索引的情况下,执行如下查询语句:mysql>select*fromstudentswherefirst_namelike'A

mysql使用union的注意点是什么 mysql使用union的注意点是什么 Jun 03, 2023 pm 08:04 PM

1、union操作符用于连接两个以上的select语句的结果组合到一个结果集合中。多个select语句会删除重复的数据。2、在使用时union合并结果集时,要求两个结果集的列数相同。实例selectplayerno,townfromPLAYERSwheretown='Inglewood'unionselectplayerno,townfromPLAYERSwheretown='Plymouth';

MySQL中union和unionall区别是什么 MySQL中union和unionall区别是什么 May 30, 2023 am 08:04 AM

union:对多个结果集进行并集操作,不包括重复行,同时进行排序。unionall:对多个结果集进行并集操作,包括重复行,不进行排序。查询部门小于30号的员工信息,和部门大于20小于40号的员工信息。①.先查询部门小于30号的员工信息。SELECTemployees_id,last_name,salary,department_idFROMemployeesWHEREdepartment_id

See all articles