SQL的where執行順序

2021-09-07 13:23:13 字數 633 閱讀 5171

1 mysql 從左到右.

乙個原則,排除越多的條件放到第乙個

例子:抄的。

select … where p.languages_id = 1 and m.languages_id = 1 and c.languages_id = 1 and t.languages_id = 1 and p.products_id in (472,474)

這樣查詢需要20多秒,雖然在各個欄位上都建立了索引。用分析explain sql一分析,發現在第一次分析過程中就返回了幾萬條資料:

where p.languages_id = 1 ,然後再依次根據條件,縮小範圍。

而我改變一下where 欄位的位置之後,速度就有了明顯地提高:

where p.products_id in (472,474) and

p.languages_id = 1 and m.languages_id = 1 and c.languages_id = 1 and t.languages_id = 1

這樣,第一次的條件是p.products_id in (472,474),它返回的結果只有不到10條,接下來還要根據其它的條件來過濾,自然在速度上有了較大的提公升。

2 orcal

從右到左

SQL 的where 執行順序

乙個原則,排除越多的條件放到第乙個例子 抄的。select where p.languages id 1 and m.languages id 1 and c.languages id 1 and t.languages id 1 and p.products id in 472,474 這樣查詢需...

sql語句中的where條件執行順序

語句1 select from work order list where head corp id 1 and work order state in 1,2 and receiving id is null or receiving id 27 語句2 select from work orde...

where 子句的執行順序

看 複製 如下 set statistics io on set statistics time on go set statistics profile on go use pubs select from authors where au fname like s or au fname lik...