MySQL5.7中union all用法的黑科技的图文代码介绍
本文带领大家通过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中则不具备这一特性。
以上是MySQL5.7中union all用法的黑科技的图文代码介绍的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

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

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

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

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

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

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

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

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