最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

5分鐘了解MySQL5.7中union all用法的黑科技

 更新時(shí)間:2017年04月14日 14:05:03   作者:樂(lè)搏學(xué)院Learnbo  
本文帶領(lǐng)大家通過(guò)5分鐘了解MySQL5.7中union all用法的黑科技,需要的朋友可以參考下

union all在MySQL5.6下的表現(xiàn)

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版本中,執(zhí)行結(jié)果如下圖所示:

wKioL1f8bZvhzEMaAAFulp6pefo997.jpg

從執(zhí)行計(jì)劃來(lái)看,是把helei表的查詢結(jié)果和t表的查詢結(jié)果合并在了一張臨時(shí)表里,然后輸出給客戶端。

union all在MySQL5.7/MariaDB10.1下的表現(xiàn)

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版本中,執(zhí)行結(jié)果如下圖所示:

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中,執(zhí)行結(jié)果如下圖所示:

wKioL1f8bmmwi9GLAAFbMJCN0uU554.jpg

從執(zhí)行結(jié)果看,無(wú)論是MySQL5.7還是MariaDB10.1,都沒(méi)有創(chuàng)建臨時(shí)表,按照順序,helei表的查詢結(jié)果首先輸出到客戶端,然后t表的查詢結(jié)果再輸出到客戶端。

本文中的優(yōu)化只針對(duì)union all,對(duì)union和在最外層使用order by無(wú)效。如下圖是所示: 

wKiom1f8boazPx35AAKnKQS1Ig4776.jpg

——總結(jié)——

在MySQL5.7/MariaDB10.1中,union all不再創(chuàng)建臨時(shí)表,這樣在聯(lián)合查詢時(shí)會(huì)減少I/O開銷,在MySQL5.5/5.6中則不具備這一特性。

以上所述是小編給大家介紹的5分鐘了解MySQL5.7中union all用法的黑科技,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • MySQL表和列、增刪改查語(yǔ)句及數(shù)據(jù)類型約束示例詳解

    MySQL表和列、增刪改查語(yǔ)句及數(shù)據(jù)類型約束示例詳解

    MySQL數(shù)據(jù)庫(kù)的增刪改查操作,這可是每個(gè)軟件測(cè)試工程師的必備技能,下面這篇文章主要介紹了MySQL表和列、增刪改查語(yǔ)句及數(shù)據(jù)類型約束的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2026-02-02
  • mysql備份表的幾種方法總結(jié)

    mysql備份表的幾種方法總結(jié)

    這篇文章主要介紹了mysql的備份表的幾種方法總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-03-03
  • Mysql的Explain使用方式及索引總結(jié)

    Mysql的Explain使用方式及索引總結(jié)

    這篇文章主要介紹了Mysql的Explain使用方式及索引總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • mysql登錄遇到ERROR 1045問(wèn)題解決方法

    mysql登錄遇到ERROR 1045問(wèn)題解決方法

    mysql登錄時(shí)出現(xiàn)了錯(cuò)誤:ERROR 1045: Access denied for user,究竟是什么原因呢?接下來(lái)為你詳細(xì)介紹下,感興趣的你可以參考下哈,或許可以幫助到你
    2013-03-03
  • MySQL中的EXPLAIN用法及解讀

    MySQL中的EXPLAIN用法及解讀

    文章講解了MySQL中EXPLAIN關(guān)鍵字的作用,用于分析SQL執(zhí)行計(jì)劃,查看表讀取順序、訪問(wèn)類型、索引使用情況等,幫助優(yōu)化查詢性能,重點(diǎn)包括字段含義、連接類型評(píng)估及覆蓋索引、臨時(shí)表等優(yōu)化技巧
    2025-07-07
  • mysql in語(yǔ)句子查詢效率慢的優(yōu)化技巧示例

    mysql in語(yǔ)句子查詢效率慢的優(yōu)化技巧示例

    本文介紹主要介紹在mysql中使用in語(yǔ)句時(shí),查詢效率非常慢,這里分享下我的解決方法,供朋友們參考。
    2017-10-10
  • MAC下MySQL忘記初始密碼怎么辦

    MAC下MySQL忘記初始密碼怎么辦

    MySQL初始密碼忘記怎么辦,這篇文章主要介紹了MAC下MySQL忘記初始密碼的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • php+mysql prepare 與普通查詢的性能對(duì)比實(shí)例講解

    php+mysql prepare 與普通查詢的性能對(duì)比實(shí)例講解

    prepare可以解決大訪問(wèn)量的網(wǎng)站給數(shù)據(jù)庫(kù)服務(wù)器所帶來(lái)的負(fù)載和開銷,本文章通過(guò)實(shí)例向大家介紹預(yù)查詢prepare與普通查詢的性能對(duì)比,需要的朋友可以參考一下
    2016-11-11
  • Mysql5.6忘記root密碼修改root密碼的方法

    Mysql5.6忘記root密碼修改root密碼的方法

    這篇文章主要介紹了Mysql5.6忘記root密碼修改root密碼的方法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • 詳解mysql 中的鎖結(jié)構(gòu)

    詳解mysql 中的鎖結(jié)構(gòu)

    這篇文章主要介紹了mysql 中的鎖結(jié)構(gòu)的相關(guān)資料,幫助大家更好的理解和使用數(shù)據(jù)庫(kù),感興趣的朋友可以了解下
    2020-10-10

最新評(píng)論

宁波市| 漳州市| 郴州市| 龙岩市| 九龙县| 尚义县| 吐鲁番市| 霞浦县| 长治县| 黎川县| 莱阳市| 东阳市| 晋州市| 贵阳市| 金坛市| 白河县| 蕲春县| 攀枝花市| 成都市| 阜阳市| 昌江| 阿拉善左旗| 高清| 新安县| 攀枝花市| 库伦旗| 潜山县| 蓬莱市| 嫩江县| 郯城县| 大庆市| 石家庄市| 蒙城县| 漯河市| 浦江县| 宁国市| 建瓯市| 萨嘎县| 宜昌市| 西乌珠穆沁旗| 扎囊县|