【指令碼】通過hash_value直接獲得sql語句的執行計劃(9i-->10g過度)
1.先看一下我在9i環境下通過hash值獲取sql語句執行計劃的常用sql指令碼
ora10g@linux5 /home/oracle/sql$ cat plan.sql
set pagesize 0
set serveroutput on size 10000
col plan_table_output format a125
undefine hash_value
set verify off feedback off
var hash_value varchar2(20)
begin
:hash_value := '&hash_value';
end;
/
insert into plan_table
(statement_id,timestamp,operation,options,object_node,object_owner,object_name,
optimizer,search_columns,id,parent_id,position,cost,cardinality,bytes,other_tag,
partition_start,partition_stop,partition_id,other,distribution,
cpu_cost,io_cost,temp_space,access_predicates,filter_predicates
)
select distinct hash_value,sysdate,operation,options,object_node,object_owner,object_name,
optimizer,search_columns,id,parent_id,position,cost,cardinality,bytes,other_tag,
partition_start,partition_stop,partition_id,other,distribution,
cpu_cost,io_cost,temp_space,access_predicates,filter_predicates
from v$sql_plan
where hash_value = :hash_value
/
col piece noprint
select distinct piece,sql_text from v$sqltext where hash_value = :hash_value order by piece
/
@?/rdbms/admin/utlxplp.sql
set verify on feedback on pagesize 1000
2.因為10g資料庫的執行計劃表與9i的表結構存在差異,因此需要調整10g plan_table表結構到9i模式
sys@ora10g> create table PLAN_TABLE_FOR_9I (
statement_id varchar2(30),
timestamp date,
remarks varchar2(80),
operation varchar2(30),
options varchar2(255),
object_node varchar2(128),
object_owner varchar2(30),
object_name varchar2(30),
object_instance numeric,
object_type varchar2(30),
optimizer varchar2(255),
search_columns number,
id numeric,
parent_id numeric,
position numeric,
cost numeric,
cardinality numeric,
bytes numeric,
other_tag varchar2(255),
partition_start varchar2(255),
partition_stop varchar2(255),
partition_id numeric,
other long,
distribution varchar2(30),
cpu_cost numeric,
io_cost numeric,
temp_space numeric,
access_predicates varchar2(4000),
filter_predicates varchar2(4000));
Table created.
sys@ora10g> create view plan_table as select * from plan_table_for_9i;
View created.
sys@ora10g> drop PUBLIC SYNONYM plan_table;
Synonym dropped.
sys@ora10g> create public synonym plan_table for sys.plan_table;
Synonym created.
sys@ora10g> GRANT ALL ON sys.plan_table TO public;
Grant succeeded.
sys@ora10g> select object_name,object_type,owner from dba_objects where object_name='PLAN_TABLE';
OBJECT_NAME OBJECT_TYPE OWNER
------------------------------ ------------------- ------------------------------
PLAN_TABLE SYNONYM PUBLIC
3.使用方法
sec@ora10g> select HASH_VALUE,sql_text from v$sql where sql_text like 'select * from emp%';
HASH_VALUE SQL_TEXT
---------- ----------------------------------------------------------------------
1893626834 select * from emp where empno=2000
1745700775 select * from emp
2 rows selected.
sec@ora10g> @plan
Enter value for hash_value: 1893626834
select * from emp where empno=2000
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 1 (100)|
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 37 | 1 (0)|
|* 2 | INDEX UNIQUE SCAN | PK_EMP | 1 | | 0 (0)|
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("EMPNO"=2000)
Note
-----
- 'PLAN_TABLE' is old version
sec@ora10g> @plan
Enter value for hash_value: 1745700775
select * from emp
---------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
---------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 3 (100)|
| 1 | TABLE ACCESS FULL| EMP | 14 | 518 | 3 (0)|
---------------------------------------------------------------
Note
-----
- 'PLAN_TABLE' is old version
-- The End --
ora10g@linux5 /home/oracle/sql$ cat plan.sql
set pagesize 0
set serveroutput on size 10000
col plan_table_output format a125
undefine hash_value
set verify off feedback off
var hash_value varchar2(20)
begin
:hash_value := '&hash_value';
end;
/
insert into plan_table
(statement_id,timestamp,operation,options,object_node,object_owner,object_name,
optimizer,search_columns,id,parent_id,position,cost,cardinality,bytes,other_tag,
partition_start,partition_stop,partition_id,other,distribution,
cpu_cost,io_cost,temp_space,access_predicates,filter_predicates
)
select distinct hash_value,sysdate,operation,options,object_node,object_owner,object_name,
optimizer,search_columns,id,parent_id,position,cost,cardinality,bytes,other_tag,
partition_start,partition_stop,partition_id,other,distribution,
cpu_cost,io_cost,temp_space,access_predicates,filter_predicates
from v$sql_plan
where hash_value = :hash_value
/
col piece noprint
select distinct piece,sql_text from v$sqltext where hash_value = :hash_value order by piece
/
@?/rdbms/admin/utlxplp.sql
set verify on feedback on pagesize 1000
2.因為10g資料庫的執行計劃表與9i的表結構存在差異,因此需要調整10g plan_table表結構到9i模式
sys@ora10g> create table PLAN_TABLE_FOR_9I (
statement_id varchar2(30),
timestamp date,
remarks varchar2(80),
operation varchar2(30),
options varchar2(255),
object_node varchar2(128),
object_owner varchar2(30),
object_name varchar2(30),
object_instance numeric,
object_type varchar2(30),
optimizer varchar2(255),
search_columns number,
id numeric,
parent_id numeric,
position numeric,
cost numeric,
cardinality numeric,
bytes numeric,
other_tag varchar2(255),
partition_start varchar2(255),
partition_stop varchar2(255),
partition_id numeric,
other long,
distribution varchar2(30),
cpu_cost numeric,
io_cost numeric,
temp_space numeric,
access_predicates varchar2(4000),
filter_predicates varchar2(4000));
Table created.
sys@ora10g> create view plan_table as select * from plan_table_for_9i;
View created.
sys@ora10g> drop PUBLIC SYNONYM plan_table;
Synonym dropped.
sys@ora10g> create public synonym plan_table for sys.plan_table;
Synonym created.
sys@ora10g> GRANT ALL ON sys.plan_table TO public;
Grant succeeded.
sys@ora10g> select object_name,object_type,owner from dba_objects where object_name='PLAN_TABLE';
OBJECT_NAME OBJECT_TYPE OWNER
------------------------------ ------------------- ------------------------------
PLAN_TABLE SYNONYM PUBLIC
3.使用方法
sec@ora10g> select HASH_VALUE,sql_text from v$sql where sql_text like 'select * from emp%';
HASH_VALUE SQL_TEXT
---------- ----------------------------------------------------------------------
1893626834 select * from emp where empno=2000
1745700775 select * from emp
2 rows selected.
sec@ora10g> @plan
Enter value for hash_value: 1893626834
select * from emp where empno=2000
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 1 (100)|
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 37 | 1 (0)|
|* 2 | INDEX UNIQUE SCAN | PK_EMP | 1 | | 0 (0)|
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("EMPNO"=2000)
Note
-----
- 'PLAN_TABLE' is old version
sec@ora10g> @plan
Enter value for hash_value: 1745700775
select * from emp
---------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)|
---------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 3 (100)|
| 1 | TABLE ACCESS FULL| EMP | 14 | 518 | 3 (0)|
---------------------------------------------------------------
Note
-----
- 'PLAN_TABLE' is old version
-- The End --
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/519536/viewspace-563050/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL——通過EXPLAIN分析SQL的執行計劃MySqlAI
- mysql執行sql語句過程MySql
- 獲取oracle sql語句詳細些執行計劃OracleSQL
- 一條sql語句的執行過程SQL
- Mybatis原始碼分析(五)探究SQL語句的執行過程MyBatis原始碼SQL
- [20191101]通過zsh計算sql語句的sql_id.txtSQL
- [20191011]通過bash計算sql語句的sql_id.txtSQL
- Laravel 獲取執行的sql語句LaravelSQL
- Oracle資料庫SQL語句執行過程Oracle資料庫SQL
- GaussDB SQL查詢語句執行過程解析SQL
- 通過 Redis 定時執行指令碼Redis指令碼
- 9i and 10g 透過SQL_ADDRESS 或sql_id查詢執行計劃SQL
- 分析執行計劃優化SQLSQL語句處理的過程(轉)優化SQL
- spark sql語句效能最佳化及執行計劃SparkSQL
- 通過鎖定表的統計資訊來穩定sql的執行計劃SQL
- MySQL探祕(二):SQL語句執行過程詳解MySql
- [zebra原始碼]分片語句ShardPreparedStatement執行過程原始碼
- MySQL系列之一條SQL查詢語句的執行過程MySql
- Oracle 通過註釋改變執行計劃Oracle
- PostgreSQL的insert語句執行過程分析SQL
- MySQL_通過binlog檢視原始SQL語句MySql
- sql語句如何執行的SQL
- [求指導] 如何通過程式碼分析一個查詢語句的執行效率
- Mybatis原始碼解析之執行SQL語句MyBatis原始碼SQL
- mysql的sql語句執行流程MySql
- SQL 語句的執行順序SQL
- 指令的執行過程
- 【SQL】Oracle程式設計藝術指令碼學習之runsat(語句執行消耗對比)SQLOracle程式設計指令碼
- SQL語句執行順序SQL
- Oracle利用coe_load_sql_profile指令碼繫結執行計劃OracleSQL指令碼
- 執行計劃-1:獲取執行計劃
- MySQL 查詢語句執行過程淺析MySql
- mybatis執行sql指令碼MyBatisSQL指令碼
- PostgreSQL 原始碼解讀(15)- Insert語句(執行過程跟蹤)SQL原始碼
- MySQL全文索引原始碼剖析之Insert語句執行過程MySql索引原始碼
- sql語句執行緩慢分析SQL
- 後臺執行SQL語句(oracle)SQLOracle
- Mybatis 動態執行SQL語句MyBatisSQL
- 查詢Oracle正在執行的sql語句及執行該語句的使用者OracleSQL