explain plan,autotrace,tkprof,执行计划和静态统计信息的解读
1、 执行计划
执行计划的设定
conn sys/pwd@tiwen` as sysdba;
IDENTIFIED BY tool
DEFAULT TABLESPACE EXAMPLE
TEMPORARY TABLESPACE TEMP
PROFILE DEFAULT
-- 2 Rolesfor TOOL
GRANT CONNECT TO TOOL;
ALTER USER TOOL DEFAULT ROLE NONE;
-- 3 SystemPrivileges for TOOL
GRANT CREATE SESSION TO TOOL;
GRANT CREATE TABLE TO TOOL;
GRANT UNLIMITED TABLESPACE TO TOOL;
CREATE GLOBAL TEMPORARY TABLE tool.PLAN_TABLE
(
STATEMENT_ID VARCHAR2(30 BYTE),
PLAN_ID NUMBER,
REMARKS VARCHAR2(4000 BYTE),
OPERATION VARCHAR2(30 BYTE),
OPTIONS VARCHAR2(255 BYTE),
OBJECT_NODE VARCHAR2(128 BYTE),
OBJECT_OWNER VARCHAR2(30 BYTE),
OBJECT_NAME VARCHAR2(30 BYTE),
OBJECT_ALIAS VARCHAR2(65 BYTE),
OBJECT_TYPE VARCHAR2(30 BYTE),
OPTIMIZER VARCHAR2(255 BYTE),
SEARCH_COLUMNS NUMBER,
ID INTEGER,
PARENT_ID INTEGER,
DEPTH INTEGER,
POSITION INTEGER,
COST INTEGER,
CARDINALITY INTEGER,
BYTES INTEGER,
PARTITION_START VARCHAR2(255 BYTE),
PARTITION_STOP VARCHAR2(255 BYTE),
PARTITION_ID INTEGER,
OTHER LONG,
DISTRIBUTION VARCHAR2(30 BYTE),
CPU_COST INTEGER,
IO_COST INTEGER,
ACCESS_PREDICATES VARCHAR2(4000 BYTE),
FILTER_PREDICATES VARCHAR2(4000 BYTE),
PROJECTION VARCHAR2(4000 BYTE),
TIME INTEGER,
QBLOCK_NAME VARCHAR2(30 BYTE)
)
grant all on TOOL.PLAN_TABLE to public;
CREATE PUBLIC SYNONYM PLAN_TABLE FOR TOOL.PLAN_TABLE;
使用方法:
truncate table PLAN_TABL;
explain plan select * from emp;
select plan_table_output from table(dbms_xplan.display('plan_table',null,'serial'));
演示:
conn scott/tiger
SQL> explain plan for select * from dept where deptno=10;
Explained
SQL> select plan_table_output fromtable(dbms_xplan.display('plan_table',null,'serial'));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
--------------------------------------------------------------------------
| Id |Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECTSTATEMENT | | 1 | 16 | 4 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| DEPT | 1 | 16 | 4 (0)| 00:00:01|
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 -filter("DEPTNO"=10)
13 rows selected
执行计划解读:--估算表
表v$sql_plan
cost概念
cardinality
create table e
as select * from emp
create table d
as
select * from dept
Explain plan for
selectename,dname from d,e where e.deptno=d.deptno
select * from table(dbms_xplan.display());
Plan hash value: 1127375450
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 15 | 630 | 7 (15)| 00:00:01 |
|* 1 | HASH JOIN | | 15 | 630 | 7 (15)| 00:00:01 |
| 2 | TABLE ACCESS FULL| D | 4 | 88 | 3 (0)| 00:00:01 |
| 3 | TABLE ACCESS FULL| E | 15 | 300 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified byoperation id):
---------------------------------------------------
1 - access("E"."DEPTNO"="D"."DEPTNO")
Note
-----
- dynamic sampling used for this statement
Explain plan for
select ename,dnamefrom d, (select ename,deptno from e where rownum<2) e where e.deptno=d.deptno
select * from table(dbms_xplan.display());
Plan hash value: 1791846393
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 42 | 5 (0)| 00:00:01 |
| 1 | NESTEDLOOPS | | 1 | 42 | 5 (0)| 00:00:01 |
| 2 | VIEW | | 1 | 20 | 2 (0)| 00:00:01 |
|* 3 | COUNTSTOPKEY | | | | | |
| 4 | TABLE ACCESS FULL| E | 15 | 300 | 2 (0)| 00:00:01 |
|* 5 | TABLE ACCESS FULL | D | 1 | 22 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------
Predicate Information (identified byoperation id):
---------------------------------------------------
3 - filter(ROWNUM<2)
5 -filter("E"."DEPTNO"="D"."DEPTNO")
Note
-----
- dynamic sampling used for this statement
驱动表概念
估算树
从左到右 从下到上
autotrace
conn sys/pwd@tiwen as sysdba;
drop role plustrace;
create role plustrace;
grant select on v_$sesstat to plustrace;
grant select on v_$statname to plustrace;
grant select on v_$mystat to plustrace;
grant plustrace to dba with admin option;
grant plustrace to public;
grant select on v_$sesstat to public;
grant select on v_$statname to public;
grant select on v_$mystat to public;
grant plustrace to dba with admin option;
grant plustrace to public;
grant alter session to public;
使用命令
set autotrace on
set autotrace off
set autotrace on explain
set autotrace on statistics
set autotrace traceonly
recursive calls:执行语句时、调用的oracle内部语句(如分析所用的sql)和其他语句(如触发器)。
测试举例:
举例1
conn scott/tiger@tiwen
set autotrace on
alter system flush shared_pool
set autotrace on
select * from emp
select * from emp
举例2
create table exchage_table
(
bill_code number(10),
exchage_rate number(16,3)
)
BILL_CODE
EXCHAGE_RATE
100
4.678
200
5.235
300
5.430
2.654
create or replace functiontoday_exchage(p_code in number) return number is
v_exange number(16,3);
begin
selectexchage_rate into v_exange from exchage_table
wherebill_code=p_code;
returnv_exange;
end;
create table affair
(
trans_id number(10),
bill_code number(10),
balance number(16,2)
)
TRANS_ID
BILL_CODE
BALANCE
1000
100
1234.00
2000
200
4324.32
3000
300
65464.23
较好的写法为
select
trans_id,
(select exchage_rate
fromexchage_table
wherebill_code=affair.bill_code )*balance
from affair
举例3
drop table tppp purge
create table tppp(p integer)
create or replace trigger t_trigger
begin
raise_application_error(-20001,'bbbbbbbb');
end if;
end t_trigger;
统计信息
----------------------------------------------------------
29 recursive calls
54 consistent gets
0 physical reads
676 bytes sent via SQL*Net to client
627 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
9 rows processed
在一次运行
统计信息
---------------------------------------------------------
29 recursive calls
0 db block gets
117 consistent gets
1 physical reads
0 redo size
483 bytes sent via SQL*Net to client
416 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
9 rows processed
drop trigger t_trigger;
统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
108 consistent gets
0 physical reads
0 redo size
483 bytes sent via SQL*Net to client
416 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
9 rows processed
解决方法为
1、 编写高效的trigger
2、 用过程代替trigger