oracle10g_11g_繫結變數bind_與最優執行計劃explain plan
1,測試sql繫結變在捕獲且是不同的繫結變時值時,執行計劃的變化情況
小結:短速不會在檢視中記錄繫結變數的資料
既然繫結變數對於效能影響很大,如何處理此問題
小結:短速不會在檢視中記錄繫結變數的資料
既然繫結變數對於效能影響很大,如何處理此問題
SQL> create table t_bind_diff(bind_id int,bind_value varchar2(100));
表已建立。
SQL> insert into t_bind_diff select level,to_char(level)||'bind_value' from dual connect by level<=100000;
已建立100000行。
SQL> commit;
提交完成。
SQL> alter table t_bind_diff add constraint pk_bind_id primary key(bind_id)
2 ;
2 ;
表已更改。
SQL> select count(bind_id) from t_bind_diff where bind_id<=20;
COUNT(BIND_ID)
--------------
20
--------------
20
SQL> select * from table(dbms_xplan.display_cursor());
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID bdn9bh7krg288, child number 0
-------------------------------------
select count(bind_id) from t_bind_diff where bind_id<=20
Plan hash value: 2874089213
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
| 1 | SORT AGGREGATE | | 1 | 13 | | |
--------------------------------------------------------------------------------
SQL_ID bdn9bh7krg288, child number 0
-------------------------------------
select count(bind_id) from t_bind_diff where bind_id<=20
Plan hash value: 2874089213
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
| 1 | SORT AGGREGATE | | 1 | 13 | | |
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
|* 2 | INDEX RANGE SCAN| PK_BIND_ID | 20 | 260 | 2 (0)| 00:00:01 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("BIND_ID"<=20)
Note
-----
- dynamic sampling used for this statement (level=2)
--------------------------------------------------------------------------------
|* 2 | INDEX RANGE SCAN| PK_BIND_ID | 20 | 260 | 2 (0)| 00:00:01 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("BIND_ID"<=20)
Note
-----
- dynamic sampling used for this statement (level=2)
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
已選擇23行。
SQL> select count(bind_id) from t_bind_diff where bind_id<=1000000;
COUNT(BIND_ID)
--------------
100000
--------------
100000
SQL> select * from table(dbms_xplan.display_cursor());
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 64ac36k5gaurs, child number 0
-------------------------------------
select count(bind_id) from t_bind_diff where bind_id<=1000000
Plan hash value: 2806203814
--------------------------------------------------------------------------------
----
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
|
--------------------------------------------------------------------------------
SQL_ID 64ac36k5gaurs, child number 0
-------------------------------------
select count(bind_id) from t_bind_diff where bind_id<=1000000
Plan hash value: 2806203814
--------------------------------------------------------------------------------
----
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
|
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
----
| 0 | SELECT STATEMENT | | | | 63 (100)|
|
| 1 | SORT AGGREGATE | | 1 | 13 | |
|
|* 2 | INDEX FAST FULL SCAN| PK_BIND_ID | 107K| 1359K| 63 (2)| 00:00:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
----
| 0 | SELECT STATEMENT | | | | 63 (100)|
|
| 1 | SORT AGGREGATE | | 1 | 13 | |
|
|* 2 | INDEX FAST FULL SCAN| PK_BIND_ID | 107K| 1359K| 63 (2)| 00:00:
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
01 |
--------------------------------------------------------------------------------
----
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("BIND_ID"<=1000000)
--------------------------------------------------------------------------------
01 |
--------------------------------------------------------------------------------
----
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("BIND_ID"<=1000000)
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
--------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
已選擇23行。
SQL> select count(bind_id) from t_bind_diff;
COUNT(BIND_ID)
--------------
100000
--------------
100000
SQL> select * from table(dbms_xplan.display_cursor());
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 03hr7ruq506h2, child number 0
-------------------------------------
select count(bind_id) from t_bind_diff
Plan hash value: 2806203814
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | 62 (100)| |
| 1 | SORT AGGREGATE | | 1 | | |
--------------------------------------------------------------------------------
SQL_ID 03hr7ruq506h2, child number 0
-------------------------------------
select count(bind_id) from t_bind_diff
Plan hash value: 2806203814
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | 62 (100)| |
| 1 | SORT AGGREGATE | | 1 | | |
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
| 2 | INDEX FAST FULL SCAN| PK_BIND_ID | 107K| 62 (0)| 00:00:01 |
----------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
--------------------------------------------------------------------------------
| 2 | INDEX FAST FULL SCAN| PK_BIND_ID | 107K| 62 (0)| 00:00:01 |
----------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
已選擇18行。
--繼續測試繫結變數捕獲與執行計劃的問題
SQL> var a number;
SQL> var b number;
SQL> var b number;
SQL> create table t_capture(a number,b number);
表已建立。
SQL> insert into t_capture values(1,2);
已建立 1 行。
SQL> insert into t_capture values(2,4);
已建立 1 行。
SQL> commit;
提交完成。
SQL> select * from t_capture where a=:a and b=:b;
未選定行
SQL> var a number;
SQL> var b number;
SQL> var b number;
SQL> exec :a:=1;
PL/SQL 過程已成功完成。
SQL> exec :b:=2;
PL/SQL 過程已成功完成。
SQL> select * from t_capture where a=:a and b=:b;
A B
---------- ----------
1 2
---------- ----------
1 2
SQL> select * from table(dbms_xplan.display_cursor());
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID b5u4m059xkhbn, child number 0
-------------------------------------
select * from t_capture where a=:a and b=:b
Plan hash value: 1287275987
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
|* 1 | TABLE ACCESS FULL| T_CAPTURE | 1 | 26 | 2 (0)| 00:00:01 |
--------------------------------------------------------------------------------
SQL_ID b5u4m059xkhbn, child number 0
-------------------------------------
select * from t_capture where a=:a and b=:b
Plan hash value: 1287275987
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
|* 1 | TABLE ACCESS FULL| T_CAPTURE | 1 | 26 | 2 (0)| 00:00:01 |
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(("A"=:A AND "B"=:B))
Note
-----
- dynamic sampling used for this statement (level=2)
--------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(("A"=:A AND "B"=:B))
Note
-----
- dynamic sampling used for this statement (level=2)
已選擇22行。
SQL>
SQL>
SQL>
SQL>
SQL>
SQL> select address,hash_value,name,position,datatype,was_captured,to_char(last_captured,'yyyymmdd hh24:mi:ss'),value_string from v$sql_bind_capture where sql_id='b5u4m059xkhbn';
SQL> r
1* select address,hash_value,name,position,datatype,was_captured,to_char(last_captured,'yyyymmdd hh24:mi:ss'),value_string from v$sql_bind_capture where sql_id='b5u4m059xkhbn'
ADDRESS HASH_VALUE NAME POSITION DATATYPE WAS TO_CHAR(LAST_CAPT
-------- ---------- ---------- -------- ---------- --- -----------------
VALUE_STRI
----------
EBDDEC50 1406746996 :A 1 2 YES 20121126 18:45:39
NULL
EBDDEC50 1406746996 :B 2 2 YES 20121126 18:45:39
NULL
-------- ---------- ---------- -------- ---------- --- -----------------
VALUE_STRI
----------
EBDDEC50 1406746996 :A 1 2 YES 20121126 18:45:39
NULL
EBDDEC50 1406746996 :B 2 2 YES 20121126 18:45:39
NULL
SQL> exec :a:=3;
PL/SQL 過程已成功完成。
SQL> exec :b:=20;
PL/SQL 過程已成功完成。
SQL> select * from t_capture where a=:a and b=:b;
未選定行
SQL> select * from table(dbms_xplan.display_cursor());
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID b5u4m059xkhbn, child number 0
-------------------------------------
select * from t_capture where a=:a and b=:b
Plan hash value: 1287275987
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
|* 1 | TABLE ACCESS FULL| T_CAPTURE | 1 | 26 | 2 (0)| 00:00:01 |
--------------------------------------------------------------------------------
SQL_ID b5u4m059xkhbn, child number 0
-------------------------------------
select * from t_capture where a=:a and b=:b
Plan hash value: 1287275987
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
|* 1 | TABLE ACCESS FULL| T_CAPTURE | 1 | 26 | 2 (0)| 00:00:01 |
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(("A"=:A AND "B"=:B))
Note
-----
- dynamic sampling used for this statement (level=2)
小結:繫結變數執行多次sql,v$sql_bind_capture僅捕獲首次的繫結變數;如何讓智慧捕獲
這樣可能導致產生差的執行計劃
--------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(("A"=:A AND "B"=:B))
Note
-----
- dynamic sampling used for this statement (level=2)
小結:繫結變數執行多次sql,v$sql_bind_capture僅捕獲首次的繫結變數;如何讓智慧捕獲
這樣可能導致產生差的執行計劃
已選擇22行。
SQL> alter system flush buffer_cache;
系統已更改。
SQL> alter system flush shared_pool;
系統已更改。
SQL> exec :a:=3;
PL/SQL 過程已成功完成。
SQL> exec :b:=20;
PL/SQL 過程已成功完成。
SQL> select * from t_capture where a=:a and b=:b;
未選定行
SQL> select * from table(dbms_xplan.display_cursor('b5u4m059xkhbn',null,'advanced'));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID b5u4m059xkhbn, child number 0
-------------------------------------
select * from t_capture where a=:a and b=:b
Plan hash value: 1287275987
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
|* 1 | TABLE ACCESS FULL| T_CAPTURE | 1 | 26 | 2 (0)| 00:00:01 |
--------------------------------------------------------------------------------
SQL_ID b5u4m059xkhbn, child number 0
-------------------------------------
select * from t_capture where a=:a and b=:b
Plan hash value: 1287275987
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
|* 1 | TABLE ACCESS FULL| T_CAPTURE | 1 | 26 | 2 (0)| 00:00:01 |
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1 /
Outline Data
-------------
/*+
--------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1 /
Outline Data
-------------
/*+
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
BEGIN_OUTLINE_DATA
IGNORE_OPTIM_EMBEDDED_HINTS
OPTIMIZER_FEATURES_ENABLE('11.2.0.1')
DB_VERSION('11.2.0.1')
ALL_ROWS
OUTLINE_LEAF(@"SEL$1")
FULL(@"SEL$1" ")
END_OUTLINE_DATA
*/
Peeked Binds (identified by position):
--------------------------------------------------------------------------------
BEGIN_OUTLINE_DATA
IGNORE_OPTIM_EMBEDDED_HINTS
OPTIMIZER_FEATURES_ENABLE('11.2.0.1')
DB_VERSION('11.2.0.1')
ALL_ROWS
OUTLINE_LEAF(@"SEL$1")
FULL(@"SEL$1" ")
END_OUTLINE_DATA
*/
Peeked Binds (identified by position):
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
--------------------------------------
1 - :A (NUMBER): 3
2 - :B (NUMBER): 20
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(("A"=:A AND "B"=:B))
Column Projection Information (identified by operation id):
--------------------------------------------------------------------------------
--------------------------------------
1 - :A (NUMBER): 3
2 - :B (NUMBER): 20
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(("A"=:A AND "B"=:B))
Column Projection Information (identified by operation id):
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
-----------------------------------------------------------
1 - "A"[NUMBER,22], "B"[NUMBER,22]
Note
-----
- dynamic sampling used for this statement (level=2)
--------------------------------------------------------------------------------
-----------------------------------------------------------
1 - "A"[NUMBER,22], "B"[NUMBER,22]
Note
-----
- dynamic sampling used for this statement (level=2)
已選擇52行。
SQL> exec :a:=111
PL/SQL 過程已成功完成。
SQL> exec :b:=112
PL/SQL 過程已成功完成。
SQL> select * from t_capture where a=:a and b=:b;
未選定行
SQL> select * from table(dbms_xplan.display_cursor('b5u4m059xkhbn',null,'advanced'));
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID b5u4m059xkhbn, child number 0
-------------------------------------
select * from t_capture where a=:a and b=:b
Plan hash value: 1287275987
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
|* 1 | TABLE ACCESS FULL| T_CAPTURE | 1 | 26 | 2 (0)| 00:00:01 |
--------------------------------------------------------------------------------
SQL_ID b5u4m059xkhbn, child number 0
-------------------------------------
select * from t_capture where a=:a and b=:b
Plan hash value: 1287275987
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 2 (100)| |
|* 1 | TABLE ACCESS FULL| T_CAPTURE | 1 | 26 | 2 (0)| 00:00:01 |
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1 /
Outline Data
-------------
/*+
--------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SEL$1 /
Outline Data
-------------
/*+
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
BEGIN_OUTLINE_DATA
IGNORE_OPTIM_EMBEDDED_HINTS
OPTIMIZER_FEATURES_ENABLE('11.2.0.1')
DB_VERSION('11.2.0.1')
ALL_ROWS
OUTLINE_LEAF(@"SEL$1")
FULL(@"SEL$1" ")
END_OUTLINE_DATA
*/
Peeked Binds (identified by position):
--------------------------------------------------------------------------------
BEGIN_OUTLINE_DATA
IGNORE_OPTIM_EMBEDDED_HINTS
OPTIMIZER_FEATURES_ENABLE('11.2.0.1')
DB_VERSION('11.2.0.1')
ALL_ROWS
OUTLINE_LEAF(@"SEL$1")
FULL(@"SEL$1" ")
END_OUTLINE_DATA
*/
Peeked Binds (identified by position):
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
--------------------------------------
1 - :A (NUMBER): 3
2 - :B (NUMBER): 20
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(("A"=:A AND "B"=:B))
Column Projection Information (identified by operation id):
--------------------------------------------------------------------------------
--------------------------------------
1 - :A (NUMBER): 3
2 - :B (NUMBER): 20
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(("A"=:A AND "B"=:B))
Column Projection Information (identified by operation id):
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
-----------------------------------------------------------
1 - "A"[NUMBER,22], "B"[NUMBER,22]
Note
-----
- dynamic sampling used for this statement (level=2)
--------------------------------------------------------------------------------
-----------------------------------------------------------
1 - "A"[NUMBER,22], "B"[NUMBER,22]
Note
-----
- dynamic sampling used for this statement (level=2)
已選擇52行。
小結:
1,多次執行繫結變數sql,未多次捕獲
1,多次執行繫結變數sql,未多次捕獲
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9240380/viewspace-750033/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle執行計劃Explain Plan 如何使用OracleAI
- Oracle 執行計劃(Explain Plan) 說明OracleAI
- Oracle 執行計劃(Explain Plan) 說明OracleAI
- 使用EXPLAIN PLAN來檢視執行計劃AI
- TOAD中檢視執行計劃(Explain Plan)AI
- 配置oracle 解釋執行計劃--explain planOracleAI
- 【Explain Plan】檢視SQL的執行計劃AISQL
- 檢視執行計劃方法總結之一:explain plan命令AI
- 【執行計劃】格式化EXPLAIN PLAN的輸出結果AI
- 【最佳化】explain plan for 方式存取執行計劃AI
- 程式中使用繫結變數,執行計劃不正確變數
- 執行計劃繫結
- autotrace 和explain plan for可能導致執行計劃錯誤AI
- oracle explain plan for獲取執行計劃並不可靠.OracleAI
- mysql調優之——執行計劃explainMySqlAI
- [20231210]執行計劃與繫結變數.txt變數
- 【CURSOR】Oracle繫結變數、執行計劃對遊標的影響Oracle變數
- 11.2 繫結變數執行計劃怎麼這樣?求助!變數
- Event 10053 執行計劃 繫結變數 Bind peeking變數
- explain執行計劃分析AI
- mysql執行計劃explainMySqlAI
- mysql explain 執行計劃MySqlAI
- 使用 EXPLAIN PLAN 獲取SQL語句執行計劃 (R0.1)AISQL
- sqm執行計劃的繫結
- Oracle-繫結執行計劃Oracle
- 如何解決執行計劃繫結變數bind peeking重新硬解析變數
- Explain執行計劃詳解AI
- explain 查詢執行計劃AI
- MySQL執行計劃explain輸出列結果解析MySqlAI
- MySQL調優篇 | EXPLAIN執行計劃解讀(4)MySqlAI
- 【sql調優】繫結變數與CBOSQL變數
- MySQL Explain執行計劃 - 詳解MySqlAI
- 十六、Mysql之Explain執行計劃MySqlAI
- mysql explain 執行計劃詳解MySqlAI
- MySQL執行計劃EXPLAIN詳解MySqlAI
- ORACLE執行計劃 explain說明OracleAI
- oracle執行計劃的使用(EXPLAIN)OracleAI
- 使用spm繫結執行計劃來線上優化資料庫優化資料庫