2017-04-04 54 views
0

這是問題很簡單,我知道,但我想有人解釋我看看我是否正確。MYSQL引擎如何工作查詢

這個簡單的查詢是否MYSQL總是執行從左到右的語義?

SELECT c03,c04,c05,count(*) as c FROM table 
where status='Active' and c04 is not null and c05 is not null 
group by c03,c04,c05 
having c>1 order by c desc limit 10; 

發動機開始從

* Status later comparing c04 and later c05 
* Later groups the results 
* later filters again the result applying the c>1 filter 
* Later sorts and fetch the first 10 results and disposing the others 

左至右過濾每一條記錄或者有一些其他的優化假設不使用索引...

回答

1

我想Status later comparing c04 and later c05取決於可用的指標和列的cardinality,並不總是按照該順序進行評估。 MySQL將嘗試首先評估約束條件,這將有助於以最低成本減少結果集的數量,例如使用沒有索引的列的約束最後可能會被評估,因爲評估很昂貴。

其餘的訂單留下的空間不大,但有些步驟可能會從適當的索引獲益。

+0

但過濾器後的下一步是這樣嗎?後來分組結果 *稍後再次過濾應用c> 1過濾器的結果 *稍後分類並獲取前10個結果並處置其他結果 – chiperortiz

+0

是的,我想是的(這是我的第二段提到的)。至少我認爲沒有可能以不同的順序去做。過濾必須在分組後進行。在過濾之前進行排序沒有任何意義,因爲過濾的複雜性是'O(n)',而排序需要'O(n log n)',所以優化器應該在排序之前減少結果集。挑選前10行只有在排序後纔可能。 – GhostGambler