oracle數據庫sql中的where條件執行順序

採用自下而上的順序解析where子句

|
|--(根據這個原理)
|--1.表之間的連接必須寫在其他where條件之前
|--2.那些可以過濾掉最大數量記錄的條件必須寫在where子句的末尾

引發效率問題舉例

|
|--(低效)|---select … from emp e where 
|        |   	   sal > 50000     
|        |   and job = 'manager'
|        |   and 25 < (select count(*) from emp where mgr=e.empno);  
|
|--(高效)|---select … from emp e where         
|        |       25 < (select count(*) from emp where mgr=e.empno)
|        |   and sal > 50000     
|        |   and job = 'manager'; 

引發正確性問題舉例

|
|\
|  \______table:demo_________________
|  |  id   id01      id02     id03   |
|  |  1     55       55       md     |
|  |  2     66       abced    eeeee  |
|  |__3_____300______200______rt_____|
|
|--報錯語句|---update demo a set id01 = 100 where
|          |   	a.id03 in ('md','rt')     
|          |   and to_number(a.id02) < 100;
|          --------------------------------------------------(to_number(a.id02),轉換異常)
| 
|--正確語句|---update demo a set id01 = 100 where
|          |   	to_number(a.id02) < 100     
|          |   and a.id03 in ('md','rt');
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章