日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

unionall mysql_5分钟了解MySQL5.7union all用法的黑科技

發布時間:2023/12/10 数据库 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 unionall mysql_5分钟了解MySQL5.7union all用法的黑科技 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

wKiom1f8bNajxqWNAAA4eVx2Dz8965.jpg

wKioL1f8bNbCZ-bgAAA4pG6yXEQ597.jpg

MySQL5.7union 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 | | 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 '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版本中,執行結果如下圖所示:

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 '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中,執行結果如下圖所示:

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中則不具備這一特性。由于筆者的水平有限,編寫時間也很倉促,文中難免會出現一些錯誤或者不準確的地方,不妥之處懇請讀者批評指正。

?著作權歸作者所有:來自51CTO博客作者dbapower的原創作品,如需轉載,請注明出處,否則將追究法律責任

mysqlunionallMySQL

總結

以上是生活随笔為你收集整理的unionall mysql_5分钟了解MySQL5.7union all用法的黑科技的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。