接下來再走一步,讓咱們看看一條SQL語句的前世此生。mysql
首先看一下示例語句: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 分組以後纔會執行SELECT 8 DISTINCT <select_list> 9 ORDER BY <order_by_condition> 10 LIMIT <limit_number>前9步都是SQL92標準語法。limit是MySQL的獨有語法。
雖然本身沒想到是這樣的,不過一看仍是很天然和諧的,從哪裏獲取,不斷的過濾條件,要選擇同樣或不同的,排好序,那才知道要取前幾條呢。ide
既然如此了,那就讓咱們一步步來看看其中的細節吧。函數
create database testQuery
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;
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);
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解析之旅吧!測試
對FROM的左邊的表和右邊的表計算笛卡爾積(CROSS JOIN)。產生虛表VT1優化
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 | +-----+------+-----+------+ rows in set (0.00 sec)
對虛表VT1進行ON篩選,只有那些符合 <join-condition> 的行纔會被記錄在虛表VT2中。ui
注意:這裏由於語法限制,使用了WHERE代替,從中讀者也能夠感覺到二者之間微妙的關係;code
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 | +-----+------+-----+------+ rows in set (0.00 sec)
若是指定了OUTER JOIN(好比left join、 right join),那麼保留表中未匹配的行就會做爲外部行添加到虛擬表VT2中,產生虛擬表VT3。blog
若是FROM子句中包含兩個以上的表的話,那麼就會對上一個join鏈接產生的結果VT3和下一個表重複執行步驟1~3這三個步驟,一直處處理完全部的表爲止。
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 | +-----+------+------+------+ rows in set (0.00 sec)
對虛擬表VT3進行WHERE條件過濾。只有符合<where-condition>的記錄纔會被插入到虛擬表VT4中。
注意:此時由於分組,不能使用聚合運算;也不能使用SELECT中建立的別名;
與ON的區別:
若是有外部列,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 | +-----+------+------+------+ rows in set (0.00 sec)
根據group by子句中的列,對VT4中的記錄進行分組操做,產生虛擬表VT5。
注意:其後處理過程的語句,如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 | +-----+------+------+------+ rows in set (0.00 sec)
對虛擬表VT5應用having過濾,只有符合<having-condition>的記錄纔會被 插入到虛擬表VT6中。
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 | +-----+------+------+------+ rows in set (0.00 sec)
這個子句對SELECT子句中的元素進行處理,生成VT5表。
(5-J1)計算表達式 計算SELECT 子句中的表達式,生成VT5-J1
尋找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 | +-----+-------+ rows in set (0.00 sec)
從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 | +-----+-------+ rows in set (0.00 sec)
LIMIT子句從上一步獲得的VT6虛擬表中選出從指定位置開始的指定行數據。
注意:offset和rows的正負帶來的影響;
當偏移量很大時效率是很低的,能夠這麼作:
採用子查詢的方式優化,在子查詢裏先從索引獲取到最大id,而後倒序排,再取N行結果集
mysql> SELEC -> 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 | +-----+-------+ row in set (0.00 sec)
FROM(將最近的兩張表,進行笛卡爾積)---VT1
ON(將VT1按照它的條件進行過濾)---VT2
LEFT JOIN(保留左表的記錄)---VT3
WHERE(過濾VT3中的記錄)--VT4…VTn
GROUP BY(對VT4的記錄進行分組)---VT5
HAVING(對VT5中的記錄進行過濾)---VT6
SELECT(對VT6中的記錄,選取指定的列)--VT7
ORDER BY(對VT7的記錄進行排序)--VT8
單表查詢:根據WHERE條件過濾表中的記錄,造成中間表(這個中間表對用戶是不可見的);而後根據SELECT的選擇列選擇相應的列進行返回最終結果。
兩錶鏈接查詢:對兩表求積(笛卡爾積)並用ON條件和鏈接鏈接類型進行過濾造成中間表;而後根據WHERE條件過濾中間表的記錄,並根據SELECT指定的列返回查詢結果。
笛卡爾積:行相乘、列相加。
多表鏈接查詢:先對第一個和第二個表按照兩錶鏈接作查詢,而後用查詢結果和第三個表作鏈接查詢,以此類推,直到全部的表都鏈接上爲止,最終造成一箇中間的結果表,而後根據WHERE條件過濾中間表的記錄,並根據SELECT指定的列返回查詢結果。
WHERE條件解析順序
MySQL:從左往右去執行WHERE條件的。
寫WHERE條件的時候,優先級高的部分要去編寫過濾力度最大的條件語句。