MySql解析順序
阿新 • • 發佈:2018-12-26
前言:
一直是想知道一條SQL語句是怎麼被執行的,它執行的順序是怎樣的,然後檢視總結各方資料,就有了下面這一篇博文了。 本文將從MySQL總體架構--->查詢執行流程--->語句執行順序來探討一下其中的知識。 一、MySQL架構總覽: 架構最好看圖,再配上必要的說明文字。 下圖根據參考書籍中一圖為原本,再在其上新增上了自己的理解。三、SQL解析順序 接下來再走一步,讓我們看看一條SQL語句的前世今生。 首先看一下示例語句
SELECT DISTINCT < select_list > FROM < left_table > < join_type > JOIN < right_table > ON < join_condition > WHERE < where_condition > GROUP BY < group_by_list > HAVING < having_condition > ORDER BY < order_by_condition > LIMIT < limit_number >然而它的執行順序是這樣的
1 FROM <left_table> 2 ON <join_condition> 3 <join_type> JOIN <right_table> 4 WHERE <where_condition> 5 GROUP BY <group_by_list> 6 HAVING <having_condition> 7 SELECT 8 DISTINCT <select_list> 9 ORDER BY <order_by_condition> 10 LIMIT <limit_number>雖然自己沒想到是這樣的,不過一看還是很自然和諧的,從哪裡獲取,不斷的過濾條件,要選擇一樣或不一樣的,排好序,那才知道要取前幾條呢。 既然如此了,那就讓我們一步步來看看其中的細節吧。 準備工作 1.建立測試資料庫
create database testQuery2.建立測試表
CREATE TABLE table1 ( uid VARCHAR(10) NOT NULL, name VARCHAR(10) NOT NULL, PRIMARY KEY(uid) )ENGINE=INNODB DEFAULT CHARSET=UTF8; CREATE TABLE table2 ( oid INT NOT NULL auto_increment, uid VARCHAR(10), PRIMARY KEY(oid) )ENGINE=INNODB DEFAULT CHARSET=UTF8;3.插入資料
INSERT INTO table1(uid,name) VALUES('aaa','mike'),('bbb','jack'),('ccc','mike'),('ddd','mike'); INSERT INTO table2(uid) VALUES('aaa'),('aaa'),('bbb'),('bbb'),('bbb'),('ccc'),(NULL);4.最後想要的結果
SELECT a.uid, count(b.oid) AS total FROM table1 AS a LEFT JOIN table2 AS b ON a.uid = b.uid WHERE a. NAME = 'mike' GROUP BY a.uid HAVING count(b.oid) < 2 ORDER BY total DESC LIMIT 1;
!現在開始SQL解析之旅吧! 1. FROM 當涉及多個表的時候,左邊表的輸出會作為右邊表的輸入,之後會生成一個虛擬表VT1。 (1-J1)笛卡爾積 計算兩個相關聯表的笛卡爾積(CROSS JOIN) ,生成虛擬表VT1-J1。
mysql> select * from table1,table2; +-----+------+-----+------+ | uid | name | oid | uid | +-----+------+-----+------+ | aaa | mike | 1 | aaa | | bbb | jack | 1 | aaa | | ccc | mike | 1 | aaa | | ddd | mike | 1 | aaa | | aaa | mike | 2 | aaa | | bbb | jack | 2 | aaa | | ccc | mike | 2 | aaa | | ddd | mike | 2 | aaa | | aaa | mike | 3 | bbb | | bbb | jack | 3 | bbb | | ccc | mike | 3 | bbb | | ddd | mike | 3 | bbb | | aaa | mike | 4 | bbb | | bbb | jack | 4 | bbb | | ccc | mike | 4 | bbb | | ddd | mike | 4 | bbb | | aaa | mike | 5 | bbb | | bbb | jack | 5 | bbb | | ccc | mike | 5 | bbb | | ddd | mike | 5 | bbb | | aaa | mike | 6 | ccc | | bbb | jack | 6 | ccc | | ccc | mike | 6 | ccc | | ddd | mike | 6 | ccc | | aaa | mike | 7 | NULL | | bbb | jack | 7 | NULL | | ccc | mike | 7 | NULL | | ddd | mike | 7 | NULL | +-----+------+-----+------+ 28 rows in set (0.00 sec)
(1-J2)ON過濾 基於虛擬表VT1-J1這一個虛擬表進行過濾,過濾出所有滿足ON 謂詞條件的列,生成虛擬表VT1-J2。 注意:這裡因為語法限制,使用了'WHERE'代替,從中讀者也可以感受到兩者之間微妙的關係;
mysql> SELECT -> * -> FROM -> table1, -> table2 -> WHERE -> table1.uid = table2.uid -> ; +-----+------+-----+------+ | uid | name | oid | uid | +-----+------+-----+------+ | aaa | mike | 1 | aaa | | aaa | mike | 2 | aaa | | bbb | jack | 3 | bbb | | bbb | jack | 4 | bbb | | bbb | jack | 5 | bbb | | ccc | mike | 6 | ccc | +-----+------+-----+------+ 6 rows in set (0.00 sec)
(1-J3)新增外部列 如果使用了外連線(LEFT,RIGHT,FULL),主表(保留表)中的不符合ON條件的列也會被加入到VT1-J2中,作為外部行,生成虛擬表VT1-J3。
mysql> SELECT -> * -> FROM -> table1 AS a -> LEFT OUTER JOIN table2 AS b ON a.uid = b.uid; +-----+------+------+------+ | uid | name | oid | uid | +-----+------+------+------+ | aaa | mike | 1 | aaa | | aaa | mike | 2 | aaa | | bbb | jack | 3 | bbb | | bbb | jack | 4 | bbb | | bbb | jack | 5 | bbb | | ccc | mike | 6 | ccc | | ddd | mike | NULL | NULL | +-----+------+------+------+ 7 rows in set (0.00 sec)
下面從網上找到一張很形象的關於‘SQL JOINS'的解釋圖,如若侵犯了你的權益,請勞煩告知刪除,謝謝。
2. WHERE 對VT1過程中生成的臨時表進行過濾,滿足WHERE子句的列被插入到VT2表中。 注意: 此時因為分組,不能使用聚合運算;也不能使用SELECT中建立的別名; 與ON的區別: 如果有外部列,ON針對過濾的是關聯表,主表(保留表)會返回所有的列; 如果沒有新增外部列,兩者的效果是一樣的; 應用: 對主表的過濾應該放在WHERE; 對於關聯表,先條件查詢後連線則用ON,先連線後條件查詢則用WHERE;
mysql> SELECT -> * -> FROM -> table1 AS a -> LEFT OUTER JOIN table2 AS b ON a.uid = b.uid -> WHERE -> a. NAME = 'mike'; +-----+------+------+------+ | uid | name | oid | uid | +-----+------+------+------+ | aaa | mike | 1 | aaa | | aaa | mike | 2 | aaa | | ccc | mike | 6 | ccc | | ddd | mike | NULL | NULL | +-----+------+------+------+ 4 rows in set (0.00 sec)
3. GROUP BY 這個子句會把VT2中生成的表按照GROUP BY中的列進行分組。生成VT3表。 注意: 其後處理過程的語句,如SELECT,HAVING,所用到的列必須包含在GROUP BY中,對於沒有出現的,得用聚合函式; 原因: GROUP BY改變了對錶的引用,將其轉換為新的引用方式,能夠對其進行下一級邏輯操作的列會減少; 我的理解是: 根據分組欄位,將具有相同分組欄位的記錄歸併成一條記錄,因為每一個分組只能返回一條記錄,除非是被過濾掉了,而不在分組欄位裡面的欄位可能會有多個值,多個值是無法放進一條記錄的,所以必須通過聚合函式將這些具有多值的列轉換成單值;
mysql> SELECT -> * -> FROM -> table1 AS a -> LEFT OUTER JOIN table2 AS b ON a.uid = b.uid -> WHERE -> a. NAME = 'mike' -> GROUP BY -> a.uid; +-----+------+------+------+ | uid | name | oid | uid | +-----+------+------+------+ | aaa | mike | 1 | aaa | | ccc | mike | 6 | ccc | | ddd | mike | NULL | NULL | +-----+------+------+------+ 3 rows in set (0.00 sec)
4. HAVING 這個子句對VT3表中的不同的組進行過濾,只作用於分組後的資料,滿足HAVING條件的子句被加入到VT4表中。
mysql> SELECT -> * -> FROM -> table1 AS a -> LEFT OUTER JOIN table2 AS b ON a.uid = b.uid -> WHERE -> a. NAME = 'mike' -> GROUP BY -> a.uid -> HAVING -> count(b.oid) < 2; +-----+------+------+------+ | uid | name | oid | uid | +-----+------+------+------+ | ccc | mike | 6 | ccc | | ddd | mike | NULL | NULL | +-----+------+------+------+ 2 rows in set (0.00 sec)
5. SELECT 這個子句對SELECT子句中的元素進行處理,生成VT5表。 (5-J1)計算表示式 計算SELECT 子句中的表示式,生成VT5-J1 (5-J2)DISTINCT 尋找VT5-1中的重複列,並刪掉,生成VT5-J2 如果在查詢中指定了DISTINCT子句,則會建立一張記憶體臨時表(如果記憶體放不下,就需要存放在硬碟了)。這張臨時表的表結構和上一步產生的虛擬表VT5是一樣的,不同的是對進行DISTINCT操作的列增加了一個唯一索引,以此來除重複資料。
mysql> SELECT -> a.uid, -> count(b.oid) AS total -> FROM -> table1 AS a -> LEFT OUTER JOIN table2 AS b ON a.uid = b.uid -> WHERE -> a. NAME = 'mike' -> GROUP BY -> a.uid -> HAVING -> count(b.oid) < 2; +-----+-------+ | uid | total | +-----+-------+ | ccc | 1 | | ddd | 0 | +-----+-------+ 2 rows in set (0.00 sec)
6.ORDER BY 從VT5-J2中的表中,根據ORDER BY 子句的條件對結果進行排序,生成VT6表。 注意: 唯一可使用SELECT中別名的地方;
mysql> SELECT -> a.uid, -> count(b.oid) AS total -> FROM -> table1 AS a -> LEFT OUTER JOIN table2 AS b ON a.uid = b.uid -> WHERE -> a. NAME = 'mike' -> GROUP BY -> a.uid -> HAVING -> count(b.oid) < 2 -> ORDER BY -> total DESC; +-----+-------+ | uid | total | +-----+-------+ | ccc | 1 | | ddd | 0 | +-----+-------+ 2 rows in set (0.00 sec)
7.LIMIT LIMIT子句從上一步得到的VT6虛擬表中選出從指定位置開始的指定行資料。 注意: offset和rows的正負帶來的影響; 當偏移量很大時效率是很低的,可以這麼做: 採用子查詢的方式優化,在子查詢裡先從索引獲取到最大id,然後倒序排,再取N行結果集 採用INNER JOIN優化,JOIN子句裡也優先從索引獲取ID列表,然後直接關聯查詢獲得最終結果
mysql> SELECT -> a.uid, -> count(b.oid) AS total -> FROM -> table1 AS a -> LEFT JOIN table2 AS b ON a.uid = b.uid -> WHERE -> a. NAME = 'mike' -> GROUP BY -> a.uid -> HAVING -> count(b.oid) < 2 -> ORDER BY -> total DESC -> LIMIT 1; +-----+-------+ | uid | total | +-----+-------+ | ccc | 1 | +-----+-------+ 1 row in set (0.00 sec)
至此SQL的解析之旅就結束了,上圖總結一下:
參考書籍: 《MySQL效能調優與架構實踐》 《MySQL技術內幕:SQL程式設計》 尾聲: 嗯,到這裡這一次的深入瞭解之旅就差不多真的結束了,雖然也不是很深入,只是一些東西將其東拼西湊在一起而已,參考了一些以前看過的書籍,大師之筆果然不一樣。而且在這過程中也是get到了蠻多東西的,最重要的是更進一步意識到,計算機軟體世界的巨集大呀~ 轉自:http://www.cnblogs.com/annsshadow/p/5037667.html