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

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

 更新時間:2017年04月14日 14:05:03   作者:樂搏學院Learnbo  
本文帶領大家通過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í)行結果如下圖所示:

wKioL1f8bZvhzEMaAAFulp6pefo997.jpg

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

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í)行結果如下圖所示:

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í)行結果如下圖所示:

wKioL1f8bmmwi9GLAAFbMJCN0uU554.jpg

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

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

wKiom1f8boazPx35AAKnKQS1Ig4776.jpg

——總結——

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

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

相關文章

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

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

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

    mysql備份表的幾種方法總結

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

    Mysql的Explain使用方式及索引總結

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

    mysql登錄遇到ERROR 1045問題解決方法

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

    MySQL中的EXPLAIN用法及解讀

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

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

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

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

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

    php+mysql prepare 與普通查詢的性能對比實例講解

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

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

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

    詳解mysql 中的鎖結構

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

最新評論

永城市| 安新县| 朝阳县| 宝应县| SHOW| 勐海县| 武强县| 巴中市| 阳原县| 农安县| 高碑店市| 日喀则市| 若尔盖县| 宁海县| 浦江县| 陇川县| 中阳县| 富阳市| 石林| 讷河市| 咸宁市| 西乌珠穆沁旗| 青河县| 西昌市| 西和县| 新巴尔虎左旗| 柳林县| 筠连县| 漳平市| 永靖县| 巴林右旗| 兴文县| 浏阳市| 枝江市| 常熟市| 乐安县| 泰来县| 互助| 镇雄县| 大余县| 页游|