首页 > 数据库技术 > 详细

Oracle里的执行计划

时间:2018-12-26 19:40:28      阅读:48      评论:0      收藏:0      [点我收藏+]

标签:warn   group   one   ria   .com   可能   only   trace   cal   

1 执行计划的查看

 1 explan plan

 2 dbms_xplan包

 3 sqlplus中 set autot

 4 10046

 5 awr报告

 6一些现成的脚本

 7 10053

常见的就前4种

执行计划的补充信息

Note

-----

- dynamic sampling used for this statement

- cardinality feedback used for this statement

- sql profile sys_sqlprof_  used for this statement

技术分享图片

1 explain plan

 Plsql developer中F5,Toad中ctrl+e,

create table testyhq (id1 number,name1 varchar2(20),date1 date default sysdate);
create table testyhq2 (id1 number,name1 varchar2(20),date1 date default sysdate);

insert  into testyhq  (id1,name1) values (1,yhq);
insert  into testyhq2  (id1,name1) values (1,yhq);

explain plan for
select * from testyhq t1
where  exists (select 1 from testyhq2 t2 where t1.id1=t2.id1);

select * from table(dbms_xplan.display);

 

 技术分享图片

2 dbms_xplan

select * from table(dbms_xplan.display)
select * from table(dbms_xplan.display_cursor(null,null,runstats_last));-all,advanced
select * from table(dbms_xplan.display_cursor(null,null,allstats last));
select * from table(dbms_xplan.display_cursor(dwcqthpyuzbgm,null,typical -predicate -rows));

3 autot on

 set autot [off|on traceonly] [exp] [stat]

4 10046tkprof命令

 --详细见10046

2 如何得到真实的执行计划

  判断执行计划是否真实,要根据真正执行过

select count(*) from emp where ename=‘hongquan‘;

select * from v$sqlarea where sql_text like‘select count(*) from emp%‘

--得到 sql_id

select * from table(dbms_xplan.display_cursor(‘dg5qqawqhkbay‘,null,‘typical -predicate -rows‘));---advanced

-得到真实的执行计划

技术分享图片

 

   - Warning: basic plan statistics not available. These are only collected when:

       * hint ‘gather_plan_statistics‘ is used for the statement or

       * parameter ‘statistics_level‘ is set to ‘ALL‘, at session or system level

 

SQL> conn scott
Enter password: 
Connected.
SQL> set linesize 1000
SQL> set pagesize 1000
SQL> SELECT ename,dname,loc                                         
  2    FROM   emp e, dept d                                           
  3    WHERE  e.deptno = d.deptno                                     
  4    AND    e.empno  = 7788;     
ENAME      DNAME          LOC
---------- -------------- -------------
SCOTT      RESEARCH       DALLAS
SQL> select * from table(dbms_xplan.display_cursor(null,null));

PLAN_TABLE_OUTPUT
SQL_ID  bbms3v8t5j4zr, child number 0
-------------------------------------
SELECT ename,dname,loc   FROM   emp e, dept d   WHERE  e.deptno = d.deptno
AND    e.empno  = 7788

Plan hash value: 2385808155
----------------------------------------------------------------------------------------
| Id  | Operation                    | Name    | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |         |       |       |     2 (100)|          |
|   1 |  NESTED LOOPS                |         |     1 |    33 |     2   (0)| 00:00:01 |
|   2 |   TABLE ACCESS BY INDEX ROWID| EMP     |     1 |    13 |     1   (0)| 00:00:01 |
|*  3 |    INDEX UNIQUE SCAN         | PK_EMP  |     1 |       |     0   (0)|          |
|   4 |   TABLE ACCESS BY INDEX ROWID| DEPT    |     4 |    80 |     1   (0)| 00:00:01 |
|*  5 |    INDEX UNIQUE SCAN         | PK_DEPT |     1 |       |     0   (0)|          |
----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   3 - access("E"."EMPNO"=7788)
   5 - access("E"."DEPTNO"="D"."DEPTNO")
24 rows selected.

SQL> select sql_id,address,plan_hash_value,hash_value,child_number from v$sql                                           
  2   where sql_text like %SELECT ename% and sql_text not like %from v$sql%;   

SQL_ID        ADDRESS  PLAN_HASH_VALUE HASH_VALUE CHILD_NUMBER
------------- -------- --------------- ---------- ------------
d3kymurnb00vk 309E4398      2385808155 3903849330            0
bbms3v8t5j4zr 3094A978      2385808155  844665847            0

SQL> select * from table(dbms_xplan.display_cursor(bbms3v8t5j4zr,null,typical -predicate -rows));

PLAN_TABLE_OUTPUT
SQL_ID  bbms3v8t5j4zr, child number 0
-------------------------------------
SELECT ename,dname,loc   FROM   emp e, dept d   WHERE  e.deptno =
d.deptno   AND    e.empno  = 7788
Plan hash value: 2385808155
--------------------------------------------------------------------------------
| Id  | Operation                    | Name    | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |         |       |     2 (100)|          |
|   1 |  NESTED LOOPS                |         |    33 |     2   (0)| 00:00:01 |
|   2 |   TABLE ACCESS BY INDEX ROWID| EMP     |    13 |     1   (0)| 00:00:01 |
|   3 |    INDEX UNIQUE SCAN         | PK_EMP  |       |     0   (0)|          |
|   4 |   TABLE ACCESS BY INDEX ROWID| DEPT    |    80 |     1   (0)| 00:00:01 |
|   5 |    INDEX UNIQUE SCAN         | PK_DEPT |       |     0   (0)|          |
--------------------------------------------------------------------------------
18 rows selected.

SQL> show parameter statistics_le
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
statistics_level                     string      TYPICAL
SQL> alter session set statistics_level=all;
Session altered.
SQL> select e.ename,e.sal,s.grade                                                                                        
  2   from emp e                                                                                                          
  3   join salgrade s                                                                                                     
  4   on e.sal between losal and hisal                                                                                    
  5   and e.deptno = 20; 
ENAME             SAL      GRADE
---------- ---------- ----------
SCOTT            3000          4
FORD             3000          4
JONES            2975          4
ADAMS            1100          1
SMITH             800          1

SQL> select * from table(dbms_xplan.display_cursor(null,null,iostats last -predicate -note));
PLAN_TABLE_OUTPUT
SQL_ID  d75njv99u8m2n, child number 0
-------------------------------------
select e.ename,e.sal,s.grade  from emp e  join salgrade s  on e.sal between losal and hisal
 and e.deptno = 20

Plan hash value: 4204027666
----------------------------------------------------------------------------------------------------
| Id  | Operation            | Name     | Starts | E-Rows | A-Rows |   A-Time   | Buffers | Reads  |
--------------------------------------------------------------------------------------------
|   1 |  MERGE JOIN          |          |      1 |      1 |      5 |00:00:00.04 |      14 |     11 |
|   2 |   SORT JOIN          |          |      1 |      5 |      5 |00:00:00.03 |       7 |      5 |
|   3 |    TABLE ACCESS FULL | EMP      |      1 |      5 |      5 |00:00:00.03 |       7 |      5 |
|   4 |   FILTER             |          |      5 |        |      5 |00:00:00.01 |       7 |      6 |
|   5 |    SORT JOIN         |          |      5 |      5 |     14 |00:00:00.01 |       7 |      6 |
|   6 |     TABLE ACCESS FULL| SALGRADE |      1 |      5 |      5 |00:00:00.01 |       7 |      6 |
----------------------------------------------------------------------------------------------------

18 rows selected.
SQL> alter session set statistics_level=typical;
Session altered.
SQL>  SELECT /*+ gather_plan_statistics */ ename,dname,loc                                                                
  2   FROM   emp e, dept d                                                                                                
  3    WHERE  e.deptno = d.deptno                                                                                          
  4    AND    d.deptno=20 ORDER BY 1,2,3; 
ENAME      DNAME          LOC
---------- -------------- -------------
ADAMS      RESEARCH       DALLAS
FORD       RESEARCH       DALLAS
JONES      RESEARCH       DALLAS
SCOTT      RESEARCH       DALLAS
SMITH      RESEARCH       DALLAS

SQL> select * from table(dbms_xplan.display_cursor(null,null,allstats -rows));  
PLAN_TABLE_OUTPUT
SQL_ID  cdazxuhca6j88, child number 0
-------------------------------------
 SELECT /*+ gather_plan_statistics */ ename,dname,loc  FROM   emp e, dept d   WHERE  e.deptno = d.deptno
AND    d.deptno=20 ORDER BY 1,2,3

Plan hash value: 3339094711
| Id  | Operation                     | Name    | Starts | A-Rows |   A-Time   | Buffers |  OMem |  1Mem |  O/1/M   |
---------------------------------------------------------------------------------------------------------------------
|   1 |  SORT ORDER BY                |         |      1 |      5 |00:00:00.01 |       9 |  2048 |  2048 |     1/0/0|
|   2 |   NESTED LOOPS                |         |      1 |      5 |00:00:00.01 |       9 |       |       |          |
|   3 |    TABLE ACCESS BY INDEX ROWID| DEPT    |      1 |      1 |00:00:00.01 |       2 |       |       |          |
|*  4 |     INDEX UNIQUE SCAN         | PK_DEPT |      1 |      1 |00:00:00.01 |       1 |       |       |          |
|*  5 |    TABLE ACCESS FULL          | EMP     |      1 |      5 |00:00:00.01 |       7 |       |       |          |
--------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   4 - access("D"."DEPTNO"=20)
   5 - filter("E"."DEPTNO"=20)

23 rows selected.
SQL>

3 查看执行计划的执行顺序

  同级,最上,最右,

---- ----安装一个xplan

select * from table(xplan.display_cursor(null,null,‘runstats_last‘));

 

4 oracle里常见的执行计划

1 与表访问相关

 TABLE ACCESS FULL

 TABLE ACCESS BY USER ROWID

 TABLE ACCESS BY INDEX ROWID

2与b tree index 相关

 INDEX UNIQURE INDEX SCAN

 INDEX RANGE SCAN

 INDEX FULL SCAN

 INDEX FAST FULL SCAN

 INDEX SKIP SCAN

3与表连接相关

 SORT JOIN ,MERGE JOIN

 NESTED LOOPS,

HASH JOIN,

NESTED LOOPS ANTI,SEMI,MERGE JOIN

Anti:not in,not exists

Semi:in,exists----半连接会对连接结果进行必要的去重

--分区

--https://www.dbaxiaoyu.com/archives/1780

位图索引相关的执行计划

位图索引主要用于数据仓库或者dss系统,位图索引实现了快捷的按位运算

位图索引没有行锁的概念,要锁就锁索引行的整个位图段,多个数据行可能对应于同一个索引行的位图段

位图索引:如果索引行的distinct值比较少,那么与b树索引比较,会占用较少的存储空间,能够快速处理and,or的条件的sql

位图索引,会存储为null值的列(及时是单键值)

5 其他典型的执行计划

 1 and-equal(index merge)

 Where出了多个不同单列的等值条件,并且多列上有单键的index,则会根据相应的单个index去扫描这些index

SQL> set linesize 800
SQL> set pagesize 1000
SQL> set autot trace
SQL>  create index idx_empno on emp_t(empno);
Index created.
SQL>  create index idx_mgr on emp_t(mgr);
Index created.
SQL>  select /*+ and_equal( emp_t idx_empno idx_mgr)*/ empno,job,ename,mgr from emp_t where empno=7566 and mgr=7839;
Execution Plan
----------------------------------------------------------
Plan hash value: 3378047620
-----------------------------------------------------------------------------------------
| Id  | Operation                   | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |           |     1 |    22 |     2   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS BY INDEX ROWID| EMP_T     |     1 |    22 |     2   (0)| 00:00:01 |
|   2 |   AND-EQUAL                 |           |       |       |            |          |
|*  3 |    INDEX RANGE SCAN         | IDX_EMPNO |     1 |       |     1   (0)| 00:00:01 |
|*  4 |    INDEX RANGE SCAN         | IDX_MGR   |     2 |       |     1   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("EMPNO"=7566 AND "MGR"=7839)
   3 - access("EMPNO"=7566)
   4 - access("MGR"=7839)

2 index join

 针对单表上不同index 的连接

 3 view

 Oracle在处理包含视图的sql时,根据该视图是否能做视图合并(view merging)

 --如果可以合并,则oracle在执行该sql可以直接针对该视图的基表,此时sql 的执行计划很可能不会出现view关键字

 --如果不能合并view,则oracle把视图看着一个独立的整体来执行,会出现关键字view

create view emp_mgr_view as select * from emp where job=MANAGER

select empno,sal from emp_mgr_view where ename=CLARK
/*+ no_unset */
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |       |       |     2 (100)|          |
|*  1 |  TABLE ACCESS FULL| EMP  |     1 |    23 |     2   (0)| 00:00:01 |

4 fliter

 直接过滤,筛选,特殊的执行计划

 1得到一个驱动结果

 2 根据一定的过滤条件从上述驱动结果集中筛选掉不满足条件的记录

 3 结果集中的数据返回给最终

Filter类型实际上是一种改良的嵌套循环连接

5 sort

sort aggregate

sort unique

sort join

sort group by

sort order by

Buffer sort表示oracle会借用PGA并把扫描结果load进去

sort不一定需要排序,如Sort aggregate,Buffer sort不一定需要排序,

1 SQL> select sum(sal) from emp where job=MANAGER;
Elapsed: 00:00:00.01
Execution Plan
----------------------------------------------------------
Plan hash value: 2083865914
---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |    12 |     2   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE    |      |     1 |    12 |            |          |
|*  2 |   TABLE ACCESS FULL| EMP  |     3 |    36 |     2   (0)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - filter("JOB"=MANAGER)
2 SQL> select distinct ename from emp where job=MANAGER order by ename;
Elapsed: 00:00:00.00
Execution Plan
----------------------------------------------------------
Plan hash value: 596748738
---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     3 |    45 |     4  (50)| 00:00:01 |
|   1 |  SORT UNIQUE       |      |     3 |    45 |     3  (34)| 00:00:01 |
|*  2 |   TABLE ACCESS FULL| EMP  |     3 |    45 |     2   (0)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - filter("JOB"=MANAGER)
3 SQL> select  ename from emp where job=MANAGER group by ename order by ename;
Elapsed: 00:00:00.00
Execution Plan
----------------------------------------------------------
Plan hash value: 15469362
---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     3 |    45 |     3  (34)| 00:00:01 |
|   1 |  SORT GROUP BY     |      |     3 |    45 |     3  (34)| 00:00:01 |
|*  2 |   TABLE ACCESS FULL| EMP  |     3 |    45 |     2   (0)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - filter("JOB"=MANAGER)

6 union/union all

union ---合并 去重复

union all ---合并

SQL> select col1,col2 from t1
  2  union all
  3  select 1,col3 from t2;
6 rows selected.
Elapsed: 00:00:00.03
Execution Plan
----------------------------------------------------------
Plan hash value: 3002291744
---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     6 |    54 |     6  (50)| 00:00:01 |
|   1 |  UNION-ALL         |      |       |       |            |          |
|   2 |   TABLE ACCESS FULL| T1   |     3 |    45 |     3   (0)| 00:00:01 |
|   3 |   TABLE ACCESS FULL| T2   |     3 |     9 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------
SQL> select col1,col2 from t1
  2  union 
  3  select 1,col3 from t2;
6 rows selected.
Elapsed: 00:00:00.03
Execution Plan
----------------------------------------------------------
Plan hash value: 3008085330
----------------------------------------------------------------------------
| Id  | Operation           | Name | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |      |     6 |    54 |     8  (63)| 00:00:01 |
|   1 |  SORT UNIQUE        |      |     6 |    54 |     8  (63)| 00:00:01 |
|   2 |   UNION-ALL         |      |       |       |            |          |
|   3 |    TABLE ACCESS FULL| T1   |     3 |    45 |     3   (0)| 00:00:01 |
|   4 |    TABLE ACCESS FULL| T2   |     3 |     9 |     3   (0)| 00:00:01 |
----------------------------------------------------------------------------

7 concat (inlist iterator) (concatenation)

 concat就是in—list扩展或者 or 扩展

SQL> select empno,ename from emp where empno in (7654,7698,7782);-----oracle 10g
Elapsed: 00:00:00.01
Execution Plan
----------------------------------------------------------
Plan hash value: 3956160932
--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     3 |    33 |     2   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| EMP  |     3 |    33 |     2   (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("EMPNO"=7654 OR "EMPNO"=7698 OR "EMPNO"=7782)
/*+ use_concat*/

8 connect by

Oracle里面层次查询的关键字

SQL> select rownum from dual connect by rownum<=4;
Elapsed: 00:00:00.01
Execution Plan
----------------------------------------------------------
Plan hash value: 1731520519
------------------------------------------------------------------------------
| Id  | Operation                     | Name | Rows  | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT              |      |     1 |     2   (0)| 00:00:01 |
|   1 |  COUNT                        |      |       |            |          |
|   2 |   CONNECT BY WITHOUT FILTERING|      |       |            |          |
|   3 |    FAST DUAL                  |      |     1 |     2   (0)| 00:00:01 |
------------------------------------------------------------------------------
SQL> select empno,ename,mgr from emp 
  2  start with mgr =7839
  3  connect by prior empno=mgr;
13 rows selected.
Elapsed: 00:00:00.03
Execution Plan
----------------------------------------------------------
Plan hash value: 3364448299
----------------------------------------------------------------------------------
| Id  | Operation                 | Name | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT          |      |    14 |   462 |     2   (0)| 00:00:01 |
|*  1 |  CONNECT BY WITH FILTERING|      |       |       |            |          |
|*  2 |   FILTER                  |      |       |       |            |          |
|   3 |    TABLE ACCESS FULL      | EMP  |    14 |   462 |     2   (0)| 00:00:01 |
|*  4 |   HASH JOIN               |      |       |       |            |          |
|   5 |    CONNECT BY PUMP        |      |       |       |            |          |
|   6 |    TABLE ACCESS FULL      | EMP  |    14 |   462 |     2   (0)| 00:00:01 |
|   7 |   TABLE ACCESS FULL       | EMP  |    14 |   462 |     2   (0)| 00:00:01 |
----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("MGR"=7839)
   2 - filter("MGR"=7839)
   4 - access("MGR"=NULL)

CONNECT BY WITHOUT FILTERING (UNIQUE)

CONNECT BY WITHOUT FILTERING

hint /*+ connect_by_filtering */

Oracle里执行计划的稳定

10g sql profile来稳定执行计划

automatic ,manual

Manual 一堆hint的组合

11g用spm来稳定执行计划,主动的稳定执行计划的手段,保证只有经过验证的执行计划才会被启用

 

Oracle里的执行计划

标签:warn   group   one   ria   .com   可能   only   trace   cal   

原文:https://www.cnblogs.com/yhq1314/p/10180517.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 designnerd.net 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号