where 子句中的顺序并不重要。ANSI JOIN 语法提高了可读性,但不影响性能。为了证明这一点:
create table t1 (a number, x number);
create table t2 (a number, b number);
insert into t1 (a, x) select level, mod(level,100) from dual connect by level <= 100000;
insert into t2 (a, b) select level, mod(level,10) from dual connect by level <= 100000;
exec dbms_stats.gather_table_stats(user,'t1');
exec dbms_stats.gather_table_stats(user,'t2');
set autotrace trace explain
所有四个查询
select t1.a, t2.b from t1, t2 where t1.a=t2.b and t1.x > 5 and t2.b > 5;
select t1.a, t2.b from t1, t2 where t1.a=t2.b and t2.b > 5 and t1.x > 5;
select t1.a, t2.b from t1, t2 where t1.x > 5 and t2.b > 5 and t1.a=t2.b;
select t1.a, t2.b from t1 join t2 on t1.a=t2.b where t1.x > 5 and t2.b > 5;
产生完全相同的查询计划
Plan hash value: 282751716
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 44444 | 434K| 88 (7)| 00:00:02 |
|* 1 | HASH JOIN | | 44444 | 434K| 88 (7)| 00:00:02 |
|* 2 | TABLE ACCESS FULL| T2 | 44444 | 130K| 42 (5)| 00:00:01 |
|* 3 | TABLE ACCESS FULL| T1 | 94946 | 649K| 44 (5)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("T1"."A"="T2"."B")
2 - filter("T2"."B">5)
3 - filter("T1"."X">5 AND "T1"."A">5)