zoukankan      html  css  js  c++  java
  • 优化器的查询转换

    1.视图合并

    SQL> create view emp_vm as 
      2  select empno,ename,job,sal,comm,deptno
      3  from emp
      4  where deptno=30;
    
    View created.
    
    SQL> select empno
      2  from emp_vm
      3  where empno > 150;
    
         EMPNO
    ----------
          7499
          7521
          7654
          7698
          7844
          7900
    
    6 rows selected.
    
    SQL> select * from table(dbms_xplan.display_cursor(null, null, 'advanced'));
    
    PLAN_TABLE_OUTPUT
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    SQL_ID  997u1v1b1k5sc, child number 0
    -------------------------------------
    select empno from emp_vm where empno > 150
    
    Plan hash value: 169057108
    
    --------------------------------------------------------------------------------------
    | Id  | Operation                   | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
    --------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT            |        |       |       |     2 (100)|          |
    |*  1 |  TABLE ACCESS BY INDEX ROWID| EMP    |     5 |    35 |     2   (0)| 00:00:01 |
    |*  2 |   INDEX RANGE SCAN          | PK_EMP |    14 |       |     1   (0)| 00:00:01 |
    --------------------------------------------------------------------------------------
    
    Query Block Name / Object Alias (identified by operation id):
    -------------------------------------------------------------
    
       1 - SEL$F5BB74E1 / EMP@SEL$2
       2 - SEL$F5BB74E1 / EMP@SEL$2
    
    Outline Data
    -------------
    
      /*+
          BEGIN_OUTLINE_DATA
          IGNORE_OPTIM_EMBEDDED_HINTS
          OPTIMIZER_FEATURES_ENABLE('11.2.0.4')
          DB_VERSION('11.2.0.4')
          ALL_ROWS
          OUTLINE_LEAF(@"SEL$F5BB74E1")
          MERGE(@"SEL$2")
          OUTLINE(@"SEL$1")
          OUTLINE(@"SEL$2")
          INDEX_RS_ASC(@"SEL$F5BB74E1" "EMP"@"SEL$2" ("EMP"."EMPNO"))
          END_OUTLINE_DATA
      */
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
       1 - filter("DEPTNO"=30)
       2 - access("EMPNO">150)
    
    Column Projection Information (identified by operation id):
    -----------------------------------------------------------
    
       1 - "EMPNO"[NUMBER,22]
       2 - "EMP".ROWID[ROWID,10], "EMPNO"[NUMBER,22]
    
    
    49 rows selected.
    
    SQL> 
    

    2.谓词推进

    优化器将查询块的相关的谓词推进到视图查询块。

    a.定义一个视图

    create view all_emp_vw as
    	( select empno, ename, job, comm, deptno from emp )
    	union
    	( select empno, ename, job, comm, deptno from contract_workers );
    

     b.执行查询

    select ename
    from all_emp_vw
    where empno = 50;
    

    c.谓词推进(优化器真正执行的sql)

    select ename
    from ( select empno, ename, job, comm, deptno from emp where empno=50
    union
    select empno, ename, job, comm, deptno from contract_workers where empno=50 );
    

    3.非嵌套子查询:将子查询转换成等价的join连接

    4.使用物化视图进行查询重写

  • 相关阅读:
    查看eclipse web项目中jsp编译后的servlet源文件【转】【JSP】
    综合实战--文件上传系统【JDBC&IO&Socket】
    002、使用webpack的各种loader处理文件
    001、node & webpack工程手动搭建
    000、GO之特别语法糖
    000、GO之深刻理解拷贝
    000、常见算法解析
    003、GO之并发
    002、GO之反射
    001、GO之指针转换
  • 原文地址:https://www.cnblogs.com/abclife/p/5179478.html
Copyright © 2011-2022 走看看