MySQL5.6中,使用union all相当于创建一张临时表,这在执行大的联合查询时候会增加I/O开销,降低查询速度。
例如执行以下SQL语句:
(select id from accessLog order by id) union all (select id from access_test order by id);
在MySQL5.6环境:
-
mysql> select version();
-
+------------+
-
| version() |
-
+------------+
-
| 5.6.14-log |
-
+------------+
-
1 row in set (0.00 sec)
-
-
mysql> explain (select id from accessLog order by id) union all (select id from access_test order by id);
-
+----+--------------+-------------+-------+---------------+--------------+---------+------+--------+-----------------+
-
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
-
+----+--------------+-------------+-------+---------------+--------------+---------+------+--------+-----------------+
-
| 1 | PRIMARY | accessLog | index | NULL | loginuserId | 9 | NULL | 535513 | Using index |
-
| 2 | UNION | access_test | index | NULL | idx_loginuid | 9 | NULL | 477248 | Using index |
-
| NULL | UNION RESULT | <union1,2> | ALL | NULL | NULL | NULL | NULL | NULL | Using temporary |
-
+----+--------------+-------------+-------+---------------+--------------+---------+------+--------+-----------------+
可以看到执行计划中提现到了创建的临时表。
在MySQL5.7环境:
-
mysql> select version();
-
+------------+
-
| version() |
-
+------------+
-
| 5.7.18-log |
-
+------------+
-
1 row in set (0.00 sec)
-
-
mysql> explain (select id from accessLog order by id) union all (select id from access_test order by id);
-
+----+-------------+-------------+------------+-------+---------------+-------------+---------+------+--------+----------+-------------+
-
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
-
+----+-------------+-------------+------------+-------+---------------+-------------+---------+------+--------+----------+-------------+
-
| 1 | PRIMARY | accessLog | NULL | index | NULL | loginuserId | 9 | NULL | 586090 | 100.00 | Using index |
-
| 2 | UNION | access_test | NULL | ALL | NULL | NULL | NULL | NULL | 571023 | 100.00 | NULL |
-
+----+-------------+-------------+------------+-------+---------------+-------------+---------+------+--------+----------+-------------+
整个查询过程没有创建临时表,按照顺序,accessLog表的查询结果首先传输到客户端,然后access_test表的查询结果再传输到客户端。
注意:此项优化对union和在最外层用order by无效,如下:
-
mysql> select version();
-
+------------+
-
| version() |
-
+------------+
-
| 5.7.18-log |
-
+------------+
-
1 row in set (0.00 sec)
-
-
mysql> explain (select id from accessLog order by id) union all (select id from access_test order by id) order by id;
-
+----+--------------+-------------+------------+-------+---------------+-------------+---------+------+--------+----------+---------------------------------+
-
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
-
+----+--------------+-------------+------------+-------+---------------+-------------+---------+------+--------+----------+---------------------------------+
-
| 1 | PRIMARY | accessLog | NULL | index | NULL | loginuserId | 9 | NULL | 586090 | 100.00 | Using index |
-
| 2 | UNION | access_test | NULL | ALL | NULL | NULL | NULL | NULL | 571023 | 100.00 | NULL |
-
| NULL | UNION RESULT | <union1,2> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | NULL | Using temporary; Using filesort |
-
+----+--------------+-------------+------------+-------+---------------+-------------+---------+------+--------+----------+---------------------------------+
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30135314/viewspace-2144685/
,如需转载,请注明出处,否则将追究法律责任。