oracle里的查询转换的作用
Oracle里的查询转换,有称为查询改写,指oracle在执行目标sql时可能会做等价改写,目的是为了更高效的执行目标sql
在10g及其以后的版本中,oracle会对某些类型的查询转换(比如子查询展开、复杂视图合并等)计算成本,oracle会分别计算查询转换后的
等价改写的sql的成本和原始sql的成本,如果改写后的sql的成本低于原始sql的成本,oracle才会对目标sql执行查询转换。
子查询展开—in、exists。。
指优化器不在将子查询当做一个独立的单元来处理,而是将该子查询转为它自身和外部查询之间的等价的表连接,将子查询拆开,
将子查询中的表、视图从子查询中拿出来,然后后外部查询的表、视图做连接,或者会把该子查询转换为一个内嵌视图(inline view)然后再和外部查询的表、视图做连接(子查询展开)
Oracle子查询前的where条件是以下
single-row(=,>,<,<=,>=,<>)
exists
not exists
in
not in
any all
例1 子查询展开
SQL> select t1.col1,t1.col2 from t1 where t1.col2 in(select col2 from t2 where t2.col3='B1'); Elapsed: 00:00:00.36 Execution Plan ---------------------------------------------------------- Plan hash value: 2894408441 --------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 10 | 4 (0)| 00:00:01 | | 1 | NESTED LOOPS SEMI | | 1 | 10 | 4 (0)| 00:00:01 | | 2 | TABLE ACCESS FULL | T1 | 3 | 15 | 3 (0)| 00:00:01 | |* 3 | TABLE ACCESS BY INDEX ROWID| T2 | 1 | 5 | 1 (0)| 00:00:01 | |* 4 | INDEX RANGE SCAN | IDX_T2 | 1 | | 0 (0)| 00:00:01 | --------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 3 - filter("T2"."COL3"='B1') 4 - access("T1"."COL2"="COL2") 转换为对应的半连接,找到满足的记录,马上停止扫描
例2 不展开/*+ no_unnest */
SQL> select t1.col1,t1.col2 from t1 where t1.col2 in(select /*+ no_unnest */ col2 from t2 where t2.c ol3='B1'); Elapsed: 00:00:00.01 Execution Plan ---------------------------------------------------------- Plan hash value: 3615181094 --------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 5 | 6 (0)| 00:00:01 | |* 1 | FILTER | | | | | | | 2 | TABLE ACCESS FULL | T1 | 3 | 15 | 3 (0)| 00:00:01 | |* 3 | TABLE ACCESS BY INDEX ROWID| T2 | 1 | 5 | 2 (0)| 00:00:01 | |* 4 | INDEX RANGE SCAN | IDX_T2 | 1 | | 1 (0)| 00:00:01 | --------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter( EXISTS (SELECT /*+ NO_UNNEST */ 0 FROM "T2" "T2" WHERE "COL2"=:B1 AND "T2"."COL3"='B1')) 3 - filter("T2"."COL3"='B1') 4 - access("COL2"=:B1) 不能展开的子查询通常是目标sql的执行计划的最后一步才会被执行,并且会走filter SQL> select tab1.col1,tab1.col2 from tab1 where tab1.col2 not in(select col2 from tab2 where tab2.col3='B1' and tab1.col2=tab2.col2); 7 rows selected. Execution Plan ---------------------------------------------------------- Plan hash value: 3181642956 --------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 8 | 64 | 15 (0)| 00:00:01 | |* 1 | FILTER | | | | | | | 2 | TABLE ACCESS FULL| TAB1 | 10 | 80 | 3 (0)| 00:00:01 | |* 3 | TABLE ACCESS FULL| TAB2 | 1 | 6 | 3 (0)| 00:00:01 | --------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter( NOT EXISTS (SELECT /*+ */ 0 FROM "TAB2" "TAB2" WHERE "TAB2"."COL3"='B1' AND "TAB2"."COL2"=:B1 AND LNNVL("COL2"<>:B2))) 3 - filter("TAB2"."COL3"='B1' AND "TAB2"."COL2"=:B1 AND LNNVL("COL2"<>:B2)) SQL> select tab1.col1,tab1.col2 from tab1 where not exists(select 1 from tab2 where tab2.col3='B1' and tab1.col2=tab2.col2); Execution Plan ---------------------------------------------------------- Plan hash value: 949155623 --------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 8 | 112 | 7 (15)| 00:00:01 | |* 1 | HASH JOIN ANTI | | 8 | 112 | 7 (15)| 00:00:01 | | 2 | TABLE ACCESS FULL| TAB1 | 10 | 80 | 3 (0)| 00:00:01 | |* 3 | TABLE ACCESS FULL| TAB2 | 1 | 6 | 3 (0)| 00:00:01 | --------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("TAB1"."COL2"="TAB2"."COL2") 3 - filter("TAB2"."COL3"='B1')
例 3 子查询合并成一个视图VW_NSO_1
SQL> select area_id,area_name,country_code,iso_code 2 from provider_area where area_id in ( 3 select distinct area_id from PROVIDER_PREFIX p 4 where exists(select 1 from IDD_Channel i where 5 p.area_code = SUBSTR (I.Virtual_No, 1, LENGTH (p.area_code)) 6 AND I.Status = 'IDLE' 7 AND I.DDI_Owner = 'GLOBALROAM' 8 AND I.Reserved_Provider_ID = 'GLOBALROAM' 9 AND I.Provider_ID = 'GLOBALROAM')) 10 and provider_id= 'GLOBALROAM'; Elapsed: 00:00:00.03 Execution Plan ---------------------------------------------------------- Plan hash value: 3848479730 --------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 46 | 12 (9)| 00:00:01 | | 1 | NESTED LOOPS | | 1 | 46 | 12 (9)| 00:00:01 | | 2 | VIEW | VW_NSO_1 | 1 | 13 | 10 (0)| 00:00:01 | | 3 | HASH UNIQUE | | 1 | 47 | | | | 4 | NESTED LOOPS | | 1 | 47 | 10 (0)| 00:00:01 | |* 5 | TABLE ACCESS FULL | IDD_CHANNEL | 1 | 37 | 5 (0)| 00:00:01 | |* 6 | TABLE ACCESS FULL | PROVIDER_PREFIX | 1 | 10 | 5 (0)| 00:00:01 | | 7 | TABLE ACCESS BY INDEX ROWID| PROVIDER_AREA | 1 | 33 | 1 (0)| 00:00:01 | |* 8 | INDEX UNIQUE SCAN | PROVIDER_AREA_UK | 1 | | 0 (0)| 00:00:01 | --------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 5 - filter("I"."DDI_OWNER"='GLOBALROAM' AND "I"."RESERVED_PROVIDER_ID"='GLOBALROAM' AND "I"."PROVIDER_ID"='GLOBALROAM' AND "I"."STATUS"='IDLE') 6 - filter("P"."AREA_CODE"=SUBSTR("I"."VIRTUAL_NO",1,LENGTH("P"."AREA_CODE"))) 8 - access("PROVIDER_ID"='GLOBALROAM' AND "AREA_ID"="$nso_col_1")
能否做子查询展开取决如下2个条件:
1 子查询展开所对应的等价改写sql和原sql在语义上一定要是完全等价的。
2 对于不拆开的子查询会把它转换为一个内嵌视图的子查询展开。
对于第一种情况,Oracle 10g及以后的版本中,Oracle也不会考虑子查询展开的成本。及Oracle都会做子查询展开
SQL> select t1.ename,t1.deptno from emp t1 where t1.deptno in (select t2.deptno from dept t2 where t2.loc='hongquan'); no rows selected Execution Plan ---------------------------------------------------------- Plan hash value: 351108634 --------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ---------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 5 | 105 | 4 (0)| 00:00:01 | | 1 | NESTED LOOPS | | 5 | 105 | 4 (0)| 00:00:01 | |* 2 | TABLE ACCESS FULL | EMP | 14 | 140 | 3 (0)| 00:00:01 | |* 3 | TABLE ACCESS BY INDEX ROWID| DEPT | 1 | 11 | 1 (0)| 00:00:01 | |* 4 | INDEX UNIQUE SCAN | PK_DEPT | 1 | | 0 (0)| 00:00:01 | --------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter("T1"."DEPTNO" IS NOT NULL) 3 - filter("T2"."LOC"='hongquan') 4 - access("T1"."DEPTNO"="T2"."DEPTNO") 将表dept,emp做nl方式连接
3 视图合并view merging
不将视图作为单独的处理单元,将视图的基表拿出来与外部查询的表合并,不会再有视图的出现
1 简单的视图合并---没有出现view
SQL> select emp.ename,emp.empno from 2 emp, EMP_MGR_VIEW e 3 where emp.empno=e.empno; Elapsed: 00:00:00.01 Execution Plan ---------------------------------------------------------- Plan hash value: 1225002481 --------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 3 | 69 | 3 (0)| 00:00:01 | |* 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 12 | 1 (0)| 00:00:01 | | 2 | NESTED LOOPS | | 3 | 69 | 3 (0)| 00:00:01 | | 3 | TABLE ACCESS FULL | EMP | 14 | 154 | 2 (0)| 00:00:01 | |* 4 | INDEX RANGE SCAN | INDEX_EMP | 3 | | 0 (0)| 00:00:01 | --------------------------------------------------------------------- 1 - filter("EMP"."EMPNO"="EMPNO") 4 - access("JOB"='MANAGER') 2 不进行视图合并/*+ no_merge(e)*/ SQL> select /*+ no_merge(e)*/emp.ename,emp.empno from 2 emp, EMP_MGR_VIEW e 3 where emp.empno=e.empno; Elapsed: 00:00:00.01 Execution Plan ---------------------------------------------------------- Plan hash value: 3750149094 --------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 3 | 72 | 5 (20)| 00:00:01 | |* 1 | HASH JOIN | | 3 | 72 | 5 (20)| 00:00:01 | | 2 | VIEW | EMP_MGR_VIEW | 3 | 39 | 2 (0)| 00:00:01 | |* 3 | TABLE ACCESS FULL| EMP | 3 | 36 | 2 (0)| 00:00:01 | | 4 | TABLE ACCESS FULL | EMP | 14 | 154 | 2 (0)| 00:00:01 | --------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("EMP"."EMPNO"="E"."EMPNO") 3 - filter("JOB"='MANAGER') /*+ MERGE(VIEW) */ CARDINALITY(T1 100) 进行简单的视图合并,该视图的视图定义不能包含以下 集合运算符(union,union all,minus,intersect) connect by字句 rownum
2外连接视图合并
Outer join view merging,针对那些使用了外连接,以及所带视图的视图定义sql语句中不含distinct,group by 等聚合函数的目标sql的视图合并
外连接视图合并的通用限制,该视图被作为外连接的驱动表,或者该视图定义的sql语句中只包含一个表
例
SQL> select emp.ename,emp.empno from 2 emp, EMP_MGR_VIEW e 3 where emp.empno(+)=e.empno; Elapsed: 00:00:00.01 Execution Plan ---------------------------------------------------------- Plan hash value: 2341341676 --------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 3 | 69 | 5 (20)| 00:00:01 | |* 1 | HASH JOIN OUTER | | 3 | 69 | 5 (20)| 00:00:01 | |* 2 | TABLE ACCESS FULL| EMP | 3 | 36 | 2 (0)| 00:00:01 | | 3 | TABLE ACCESS FULL| EMP | 14 | 154 | 2 (0)| 00:00:01 | --------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("EMP"."EMPNO"(+)="EMPNO") 2 - filter("JOB"='MANAGER') SQL> select emp.ename,emp.empno from 2 emp, EMP_MGR_VIEW e 3 where emp.empno=e.empno(+); 14 rows selected. Elapsed: 00:00:00.03 Execution Plan ---------------------------------------------------------- Plan hash value: 1264059372 ------------------------------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | 14 | 322 | 3 (0)| 00:00:01 | | 1 | NESTED LOOPS OUTER | | 14 | 322 | 3 (0)| 00:00:01 | | 2 | TABLE ACCESS FULL | EMP | 14 | 154 | 2 (0)| 00:00:01 | |* 3 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 12 | 1 (0)| 00:00:01 | |* 4 | INDEX RANGE SCAN | INDEX_EMP | 3 | | 0 (0)| 00:00:01 | --------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 3 - filter("EMP"."EMPNO"="EMPNO"(+)) 4 - access("JOB"(+)='MANAGER')
3复杂视图的合并
复杂视图的合并会推迟group by,distinct操作,所以不一定能带来效率的提升
4 连接谓词推入---视图
Join predicate pushdown,优化处理带视图的另一个优化手段,会把视图当做一个独立的单元,但优化器会把处于该视图外部查询中和该视图的连接条件推入到该视图的定义的sql语句内部,为了能使用上基表的index
Oracle仅仅支持如下类型的视图做谓词推入
视图定义sql语句中包含union all、union
视图定义sql包含distinct
视图定义sql包含group by视图
和外部查询之间的连接类型是外连接
和外部查询之间的连接类型是反连接
和外部查询之间的连接类型是半连接
例
create table emp1 as select * from emp create table emp2 as select * from emp create index emp1_idx on emp1(empno) create index emp2_idx on emp2(empno) create view emp_view as select emp1.empno as empno1 from emp1; create view emp_view_union as select emp1.empno as empno1 from emp1 union all select emp2.empno as empno1 from emp2; SQL> select /*+ no_merge(emp_view)*/ emp.empno 2 from emp,emp_view 3 where emp.empno=emp_view.empno1(+) 4 and emp.ename='FORD'; Elapsed: 00:00:00.01 Execution Plan ---------------------------------------------------------- Plan hash value: 1890544608 --------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 19 | 3 (0)| 00:00:01 | | 1 | NESTED LOOPS OUTER | | 1 | 19 | 3 (0)| 00:00:01 | |* 2 | TABLE ACCESS FULL | EMP | 1 | 15 | 2 (0)| 00:00:01 | | 3 | VIEW PUSHED PREDICATE | EMP_VIEW | 1 | 4 | 1 (0)| 00:00:01 | |* 4 | INDEX RANGE SCAN | EMP1_IDX | 1 | 26 | 1 (0)| 00:00:01 | --------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter("EMP"."ENAME"='FORD') 4 - access("EMP1"."EMPNO"="EMP"."EMPNO") Note ----- - dynamic sampling used for this statement 2 没有做谓词推入 SQL> select emp.empno 2 from emp,emp_view_union 3 where emp.empno=emp_view_union.empno1 4 and emp.ename='FORD'; Elapsed: 00:00:00.01 Execution Plan ---------------------------------------------------------- Plan hash value: 2081391209 --------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 24 | 3 (0)| 00:00:01 | |* 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 11 | 1 (0)| 00:00:01 | | 2 | NESTED LOOPS | | 1 | 24 | 3 (0)| 00:00:01 | | 3 | VIEW | EMP_VIEW_UNION | 28 | 364 | 2 (0)| 00:00:01 | | 4 | UNION-ALL | | | | | | | 5 | INDEX FULL SCAN | EMP1_IDX | 14 | 182 | 1 (0)| 00:00:01 | | 6 | INDEX FULL SCAN | EMP2_IDX | 14 | 182 | 1 (0)| 00:00:01 | |* 7 | INDEX RANGE SCAN | EMP_IDX | 1 | | 0 (0)| 00:00:01 | --------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("EMP"."EMPNO"="EMP_VIEW_UNION"."EMPNO1") 7 - access("EMP"."ENAME"='FORD') 3 做了谓词推入 SQL> select /*+ push_pred(emp_view_union) */emp.empno 2 from emp,emp_view_union 3 where emp.empno=emp_view_union.empno1 4 and emp.ename='FORD'; Elapsed: 00:00:00.03 Execution Plan ---------------------------------------------------------- Plan hash value: 765440681 --------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | 1 | 31 | 4 (0)| 00:00:01 | | 1 | NESTED LOOPS | | 1 | 31 | 4 (0)| 00:00:01 | |* 2 | TABLE ACCESS FULL | EMP | 1 | 23 | 2 (0)| 00:00:01 | | 3 | VIEW | EMP_VIEW_UNION | 1 | 8 | 2 (0)| 00:00:01 | | 4 | UNION ALL PUSHED PREDICATE | | | | | | |* 5 | INDEX RANGE SCAN | EMP1_IDX | 1 | 26 | 1 (0)| 00:00:01 | |* 6 | INDEX RANGE SCAN | EMP2_IDX | 1 | 26 | 1 (0)| 00:00:01 | Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter("EMP"."ENAME"='FORD') 5 - access("EMP1"."EMPNO"="EMP"."EMPNO") 6 - access("EMP2"."EMPNO"="EMP"."EMPNO")
5 连接因式分解—union all
11gr2被引入,优化器处理带union-all的目标的sql 的一种优化手段,把多个union的相同结果提取出来为一个结果集,然后再和原union all中剩下的部分表做连接
6表扩展—partition table
优化器针对分区表的目标sql的一种优化手段,当指定目标sql中的分区表的某个局部分区索引由于某种原因在某些分区上变得不可用(unusable),
oracle将目标sql等级的改写成按分区union all的形式,不可用index union all可用index
11g r2引入
对同一个目标sql而言,oracle可能会采用不止一种的查询转换手段
7 表移除
是优化器处理带多表连接的目标的sql的一种优化手段,使用表与表之间通过外键相连的情形,还使用于表与表之间外连接的情况
SQL> select ename from emp,dept 2 where emp.deptno=dept.deptno; ------执行计划没有选择dept表,列deptno的属性 is not null 该列有一个fk 14 rows selected. Execution Plan ---------------------------------------------------------- Plan hash value: 3956160932 -------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 14 | 98 | 3 (0)| 00:00:01 | |* 1 | TABLE ACCESS FULL| EMP | 14 | 98 | 3 (0)| 00:00:01 | -------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("EMP"."DEPTNO" IS NOT NULL)
8 oracle如何处理sql语句中的in
优化器在处理带in字句的sql时,会将其转换为or,2者等价
优化器在处理带in的sql时,通常会采用以下4中方法
Inlist lterator
Inlist expansion
inlist filter
对in做子查询展开,或者既展开又做视图合并
1 inlist lterator
-----针对in后面是常量集合的一种处理方法
需要注意的地方:1 inlist lterator是oracle针对in的首选处理方法
2 来处理in的前提条件是in所在的列一定要有index
3 不能让oracle强制走inlist lterator,没有相关的hint
SQL> select ename,empno from emp where empno in (7521,7566); Execution Plan ---------------------------------------------------------- Plan hash value: 1899965127 --------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 2 | 16 | 2 (0)| 00:00:01 | | 1 | INLIST ITERATOR | | | | | | | 2 | TABLE ACCESS BY INDEX ROWID| EMP | 2 | 16 | 2 (0)| 00:00:01 | |* 3 | INDEX RANGE SCAN | PK_EMP | 2 | | 1 (0)| 00:00:01 | ---------------------------------------------------------------------Predicate Information (identified by operation id): --------------------------------------------------- 3 - access("EMPNO"=7521 OR "EMPNO"=7566) 2 inlist expansion| or expansion 针对in后面是常量集合的另一种处理方法 把in后面的每一个常量都用union all来展开,有多少个常量,就会产生union all 多少次 改为union all后,各个分支就可以走自己的index,分区裁剪,表连接等相关的执行计划而不互相干扰 create index idx_emp_depto on emp(deptno) select /*+ use_concat*/* from emp where deptno in (10,20,30); 也还是走的INLIST ITERATOR
3 inlist filter
---优化器把in后面的子查询所对应的结果集当做过滤条件,并且走filter执行计划
目标sql的in后面子查询不是常量的集合
Oracle未对目标sql进行子查询展开
4对in子查询展开/视图合并
In 后面不实常量
In后面的子查询可以展开