本文帶領大家透過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中文網其他相關文章!

MySQL函數可用於數據處理和計算。 1.基本用法包括字符串處理、日期計算和數學運算。 2.高級用法涉及結合多個函數實現複雜操作。 3.性能優化需避免在WHERE子句中使用函數,並使用GROUPBY和臨時表。

MySQL批量插入数据的高效方法包括:1.使用INSERTINTO...VALUES语法,2.利用LOADDATAINFILE命令,3.使用事务处理,4.调整批量大小,5.禁用索引,6.使用INSERTIGNORE或INSERT...ONDUPLICATEKEYUPDATE,这些方法能显著提升数据库操作效率。

在MySQL中,添加字段使用ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column,刪除字段使用ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop。添加字段時,需指定位置以優化查詢性能和數據結構;刪除字段前需確認操作不可逆;使用在線DDL、備份數據、測試環境和低負載時間段修改表結構是性能優化和最佳實踐。

使用EXPLAIN命令可以分析MySQL查詢的執行計劃。 1.EXPLAIN命令顯示查詢的執行計劃,幫助找出性能瓶頸。 2.執行計劃包括id、select_type、table、type、possible_keys、key、key_len、ref、rows和Extra等字段。 3.根據執行計劃,可以通過添加索引、避免全表掃描、優化JOIN操作和使用覆蓋索引來優化查詢。

子查詢可以提升MySQL查詢效率。 1)子查詢簡化複雜查詢邏輯,如篩選數據和計算聚合值。 2)MySQL優化器可能將子查詢轉換為JOIN操作以提高性能。 3)使用EXISTS代替IN可避免多行返回錯誤。 4)優化策略包括避免相關子查詢、使用EXISTS、索引優化和避免子查詢嵌套。

在MySQL中配置字符集和排序規則的方法包括:1.設置服務器級別的字符集和排序規則:SETNAMES'utf8';SETCHARACTERSETutf8;SETCOLLATION_CONNECTION='utf8_general_ci';2.創建使用特定字符集和排序規則的數據庫:CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci;3.創建表時指定字符集和排序規則:CREATETABLEexample_table(idINT

要安全、徹底地卸載MySQL並清理所有殘留文件,需遵循以下步驟:1.停止MySQL服務;2.卸載MySQL軟件包;3.清理配置文件和數據目錄;4.驗證卸載是否徹底。

MySQL中重命名數據庫需要通過間接方法實現。步驟如下:1.創建新數據庫;2.使用mysqldump導出舊數據庫;3.將數據導入新數據庫;4.刪除舊數據庫。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

禪工作室 13.0.1
強大的PHP整合開發環境

Atom編輯器mac版下載
最受歡迎的的開源編輯器

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),