MySQL查詢語句執行過程圖解

好比有以下的sql查詢語句,其中的執行順序是什麼樣子的呢?sql

select * from help_topic as t join help_relation  as r on t.help_topic_id = r.help_topic_id
where t.help_topic_id < 200
having r.help_topic_id<150
order by t.help_topic_id asc;

簡單來講,上述語句是咱們所寫的SQL語句,這個是人理解的順序,固然對於初學者,這個順序是有點繞的,機器執行的順序和咱們手寫SQL的順序是不一致的!其執行順序以下:code

1. FROM  <left_table>
2. ON <join_codition>
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>

整體來講,MYSQL的查詢語句,能夠分爲三個部分: 整體來講,MYSQL的查詢語句,能夠分爲三個部分:blog

  • 一個是表之間的各類鏈接,這部分是join ...on....造成了一個新的表(能夠這麼理解);
    • select * from help_topic as t join help_relation as r on t.help_topic_id = r.help_topic_id;
  • 第二是按照條件去篩選,此處使用的是where條件,也能夠理解成造成了一個新的表;
    • select * from help_topic as t join help_relation as r on t.help_topic_id = r.help_topic_id where t.help_topic_id < 200;
  • 第三是按照上述造成的新的臨時表,而後去經過分組(group by),篩選(having),排列(order by),限定行數(limit)等操做。
    • select * from help_topic as t join help_relation as r on t.help_topic_id = r.help_topic_id where t.help_topic_id < 200 having r.help_topic_id < 150 order by t.help_topic_id asc;

正如上述描述,SQL查詢就能夠分紅這麼三個步驟。形象的解釋以下圖所示: MySQL查詢語句執行過程圖解it

相關文章
相關標籤/搜索