首頁 資料庫 mysql教程 MySQL5.7中union all用法的黑科技的圖文程式碼介紹

MySQL5.7中union all用法的黑科技的圖文程式碼介紹

Apr 15, 2017 am 09:31 AM

本文帶領大家透過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版本中,執行結果如下圖所示:

MySQL5.7中union all用法的黑科技的圖文程式碼介紹

從執行計劃來看,是把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 &#39;help;&#39; or &#39;\h&#39; for help. Type &#39;\c&#39; 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版本中,執行結果如下圖:

MySQL5.7中union all用法的黑科技的圖文程式碼介紹

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 &#39;help;&#39; or &#39;\h&#39; for help. Type &#39;\c&#39; 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中union all用法的黑科技的圖文程式碼介紹

從執行結果來看,無論是MySQL5.7或MariaDB10.1,都沒有建立臨時表,依照順序,helei表的查詢結果先輸出到客戶端,然後t表的查詢結果再輸出到客戶端。

本文中的最佳化只針對union all,對union和在最外層使用order by無效。如下圖所示: 

MySQL5.7中union all用法的黑科技的圖文程式碼介紹

—總結—

在MySQL5.7/MariaDB10.1中,union all不再建立臨時表,這樣在聯合查詢時會減少I/O開銷,在MySQL5.5/5.6中則不具備此特性。

以上是MySQL5.7中union all用法的黑科技的圖文程式碼介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1665
14
CakePHP 教程
1423
52
Laravel 教程
1321
25
PHP教程
1269
29
C# 教程
1249
24
c語言union的用法 c語言union的用法 Sep 27, 2023 am 11:00 AM

c語言union的用法是一種特殊的資料類型,它允許在相同的記憶體位置儲存不同的資料類型,union的使用可以幫助我們節省記憶體空間,並且可以方便地在不同的資料類型之間進行轉換。使用union時需要注意對應的成員是有效的,並且只能同時存取一個成員。

MySQL中如何使用FULL OUTER JOIN函數取得兩個表的並集 MySQL中如何使用FULL OUTER JOIN函數取得兩個表的並集 Jul 26, 2023 pm 05:45 PM

MySQL中如何使用FULLOUTERJOIN函數取得兩個表的並集在MySQL中,FULLOUTERJOIN函數是一種融合內連接和外連接的功能強大的連接操作。它可以用來取得兩個表的並集,即將兩個表中的所有資料合併為一個結果集。本文將介紹FULLOUTERJOIN函數的用法,並提供一些範例程式碼以幫助讀者更好地理解。 FULLOUTERJOIN函數

java如何定義Union類別實作資料體的共存 java如何定義Union類別實作資料體的共存 May 14, 2023 pm 03:34 PM

定義Union類別實作資料體的共存在C/C++語言中,聯合體(union),又稱共用體,類似結構體(struct)的一種資料結構。聯合體(union)和結構體(struct)一樣,可以包含很多種資料類型和變量,兩者區別如下:結構體(struct)中所有變數是「共存」的,同時所有變數都生效,各個變數佔據不同的記憶體空間;聯合體(union)中是各變數是「互斥」的,同時只有一個變數生效,所有變數佔據同一塊記憶體空間。當多個資料需要共享記憶體或多個資料每次只取其一時,可以採用聯合體(union)。在Java

怎麼用mysql union實作全外連線查詢 怎麼用mysql union實作全外連線查詢 May 30, 2023 pm 06:49 PM

1、union不是多表格連接查詢的一種方式,將多個查詢句子的查詢結果合併成一個結果,去除重複資料。 2.全外連接查詢左表和右表的數據,然後根據連接條件連接。實例#用左外的Aunion右外的BSELECT*FROMt_categorycLEFTOUTERJOINt_productpONc.cid=p.cnounionSELECT*FROMt_categorycRIGHTOUTERJOINt_productpONc.cid=p.cno

如何透過MySQL對UNION最佳化來提高效能 如何透過MySQL對UNION最佳化來提高效能 May 11, 2023 pm 05:40 PM

在許多資料庫應用程式中,我們都會面臨需要整合來自多個資料來源的資料的情況。 MySQL的UNION語句就是一種用來解決這種情況的方式,它允許我們將兩個或多個SELECT語句的結果集合併為一個。雖然這是一個非常方便的功能,但如果不加以最佳化,UNION語句也可能會對系統產生效能問題。本文將探討如何透過MySQL對UNION最佳化來提升效能。使用UNIONALL在使用U

MySQL中怎麼用Union優化Like語句 MySQL中怎麼用Union優化Like語句 May 31, 2023 pm 03:55 PM

用Union優化Like語句1)有時候,你可能需要在查詢中使用or操作符來比較。當or關鍵字在where子句中使用頻率過高的時候,它可能會使MySQL優化器錯誤的選擇全表掃描來檢索記錄。 union子句可以是查詢執行的更快,尤其是當其中一個查詢有一個最佳化索引,而另一個查詢也有一個最佳化索引的時候。例如,在first_name和last_name上分別存在索引的情況下,執行下列查詢語句:mysql>select*fromstudentswherefirst_namelike'A

mysql使用union的注意點是什麼 mysql使用union的注意點是什麼 Jun 03, 2023 pm 08:04 PM

1.union運算子用於連接兩個以上的select語句的結果組合到一個結果集合中。多個select語句會刪除重複的資料。 2.使用時union合併結果集時,要求兩個結果集的列數相同。實例selectplayerno,townfromPLAYERSwheretown='Inglewood'unionselectplayerno,townfromPLAYERSwheretown='Plymouth';

MySQL中union和unionall差別是什麼 MySQL中union和unionall差別是什麼 May 30, 2023 am 08:04 AM

union:對多個結果集進行並集操作,不包括重複行,同時進行排序。 unionall:對多個結果集進行並集操作,包括重複行,不進行排序。查詢部門小於30號的員工信息,和部門大於20小於40號的員工資訊。 ①.先查詢部門小於30號的員工資料。 SELECTemployees_id,last_name,salary,department_idFROMemployeesWHEREdepartment_id

See all articles