Oracle+高效能SQL引擎剖析:SQL優化與調優機制詳解-筆記之執行計劃(一)
筆記所在章節:第2 章 解讀執行計劃
以上操作我可以成功執行,但是我發現我的執行計劃與書中的不同,
SQL> exec sql_explain('select o.owner, o.object_name, o.object_id from t_users u, t_objects o where u.username=o.owner and o.object_name like :A and u.user_id=:B','BASIC PREDICATE');
Plan hash value: 1644402662
--------------------------------------------------------
| Id | Operation | Name |
--------------------------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | NESTED LOOPS | |
| 2 | TABLE ACCESS BY INDEX ROWID | T_USERS |
|* 3 | INDEX UNIQUE SCAN | T_USERS_PK |
|* 4 | TABLE ACCESS BY INDEX ROWID | T_OBJECTS |
| 5 | BITMAP CONVERSION TO ROWIDS| |
|* 6 | BITMAP INDEX SINGLE VALUE | T_OBJECTS_IDX4 |
--------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("U"."USER_ID"=TO_NUMBER(:B))
4 - filter("O"."OBJECT_NAME" LIKE :A)
6 - access("U"."USERNAME"="O"."OWNER")
PL/SQL procedure successfully completed
書中的第四步是:
|* 4 | TABLE ACCESS FULL | T_OBJECTS |
---------------------------------------------------
但是我將sql_explain裡最後一個引數設為false:
exec sql_explain('select o.owner, o.object_name, o.object_id from t_users u, t_objects o where u.username=o.owner and o.object_name like :A and u.user_id=:B','BASIC PREDICATE', FALSE);
原文:
2.1 執行計劃的基本資料
我們用程式碼清單2-1 中的查詢計劃為例,解釋計劃訪問中基本資料的含義。
程式碼清單2-1 執行計劃查詢
HELLODBA.COM>exec sql_explain('select o.owner, o.object_name, o.object_id from t_users u, t_objects o
where u.username=o.owner and o.object_name like :A and u.user_id=:B','BASIC PREDICATE');
我們用程式碼清單2-1 中的查詢計劃為例,解釋計劃訪問中基本資料的含義。
程式碼清單2-1 執行計劃查詢
HELLODBA.COM>exec sql_explain('select o.owner, o.object_name, o.object_id from t_users u, t_objects o
where u.username=o.owner and o.object_name like :A and u.user_id=:B','BASIC PREDICATE');
以上操作我可以成功執行,但是我發現我的執行計劃與書中的不同,
SQL> exec sql_explain('select o.owner, o.object_name, o.object_id from t_users u, t_objects o where u.username=o.owner and o.object_name like :A and u.user_id=:B','BASIC PREDICATE');
Plan hash value: 1644402662
--------------------------------------------------------
| Id | Operation | Name |
--------------------------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | NESTED LOOPS | |
| 2 | TABLE ACCESS BY INDEX ROWID | T_USERS |
|* 3 | INDEX UNIQUE SCAN | T_USERS_PK |
|* 4 | TABLE ACCESS BY INDEX ROWID | T_OBJECTS |
| 5 | BITMAP CONVERSION TO ROWIDS| |
|* 6 | BITMAP INDEX SINGLE VALUE | T_OBJECTS_IDX4 |
--------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("U"."USER_ID"=TO_NUMBER(:B))
4 - filter("O"."OBJECT_NAME" LIKE :A)
6 - access("U"."USERNAME"="O"."OWNER")
PL/SQL procedure successfully completed
書中的第四步是:
|* 4 | TABLE ACCESS FULL | T_OBJECTS |
---------------------------------------------------
但是我將sql_explain裡最後一個引數設為false:
exec sql_explain('select o.owner, o.object_name, o.object_id from t_users u, t_objects o where u.username=o.owner and o.object_name like :A and u.user_id=:B','BASIC PREDICATE', FALSE);
會執行報錯:
ORA-01008: 並非所有變數都已繫結
ORA-06512: 在 "SYS.DBMS_SQL", line 1587
ORA-06512: 在 "SYS.SQL_EXPLAIN", line 32
ORA-06512: 在 line 2
ORA-06512: 在 "SYS.DBMS_SQL", line 1587
ORA-06512: 在 "SYS.SQL_EXPLAIN", line 32
ORA-06512: 在 line 2
我在sqlplus中嘗試過
var A varchar2(20);
exec :A := 'T_USERS';
var B number;
exec :B := 94;
exec sql_explain('select o.owner, o.object_name, o.object_id from t_users u, t_objects o where u.username=o.owner and o.object_name like :A and u.user_id=to_number(:B)','BASIC PREDICATE', FALSE);
還是會報錯,這是什麼原因?
SQL> select * from v$version where rownum=1;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
經與作者聯絡得到的答覆:
你好!
我就重新做了實驗:
SQL>
declare
A varchar2(10) := 'T_USERS';
B varchar2(2) := '94';
begin
sql_explain('select o.owner, o.object_name, o.object_id from t_users u, t_objects o where u.username=o.owner and o.object_name like '''|| A || ''' and u.user_id=' || B,'BASIC PREDICATE',FALSE);
end;
/
before parse: select o.owner, o.object_name, o.object_id from t_users u, t_objects o where u.username=o.owner and o.object_name like 'T_USERS' and u.user_id=94
EXPLAINED SQL STATEMENT:
------------------------
select o.owner, o.object_name, o.object_id from t_users u, t_objects o
where u.username=o.owner and o.object_name like 'T_USERS' and
u.user_id=94
Plan hash value: 1644402662
--------------------------------------------------------
| Id | Operation | Name |
--------------------------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | NESTED LOOPS | |
| 2 | TABLE ACCESS BY INDEX ROWID | T_USERS |
|* 3 | INDEX UNIQUE SCAN | T_USERS_PK |
|* 4 | TABLE ACCESS BY INDEX ROWID | T_OBJECTS |
| 5 | BITMAP CONVERSION TO ROWIDS| |
|* 6 | BITMAP INDEX SINGLE VALUE | T_OBJECTS_IDX4 |
--------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("U"."USER_ID"=94)
4 - filter("O"."OBJECT_NAME" LIKE 'T_USERS')
6 - access("U"."USERNAME"="O"."OWNER")
PL/SQL procedure successfully completed
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
經與作者聯絡得到的答覆:
你好!
第三個引數指定了FASLE,這表示該過程將會實際執行該語句。所以你需要在語句中使用實際值,而不是使用繫結變數。
執行計劃不同是因為在演示這個計劃是,索引T_OBJECTS_IDX4還未建立。你可以使用提示/*+full(o)*/來重現該計劃
致,
黃瑋
你好!
你是指在sqlplus中用var命令來定義變數嗎?這樣是不行的,因為這是一個儲存過程,不會從sqlplus中讀取變數的。
致,
黃瑋
-----------------------------------------------------------------------------------------------------------------------------我就重新做了實驗:
我在一個匿名塊裡定義變數,在裡面呼叫這個SP就可以:
declare
A varchar2(10) := 'T_USERS';
B varchar2(2) := '94';
begin
sql_explain('select o.owner, o.object_name, o.object_id from t_users u, t_objects o where u.username=o.owner and o.object_name like '''|| A || ''' and u.user_id=' || B,'BASIC PREDICATE',FALSE);
end;
/
就可以成功執行:A varchar2(10) := 'T_USERS';
B varchar2(2) := '94';
begin
sql_explain('select o.owner, o.object_name, o.object_id from t_users u, t_objects o where u.username=o.owner and o.object_name like '''|| A || ''' and u.user_id=' || B,'BASIC PREDICATE',FALSE);
end;
/
SQL>
declare
A varchar2(10) := 'T_USERS';
B varchar2(2) := '94';
begin
sql_explain('select o.owner, o.object_name, o.object_id from t_users u, t_objects o where u.username=o.owner and o.object_name like '''|| A || ''' and u.user_id=' || B,'BASIC PREDICATE',FALSE);
end;
/
before parse: select o.owner, o.object_name, o.object_id from t_users u, t_objects o where u.username=o.owner and o.object_name like 'T_USERS' and u.user_id=94
EXPLAINED SQL STATEMENT:
------------------------
select o.owner, o.object_name, o.object_id from t_users u, t_objects o
where u.username=o.owner and o.object_name like 'T_USERS' and
u.user_id=94
Plan hash value: 1644402662
--------------------------------------------------------
| Id | Operation | Name |
--------------------------------------------------------
| 0 | SELECT STATEMENT | |
| 1 | NESTED LOOPS | |
| 2 | TABLE ACCESS BY INDEX ROWID | T_USERS |
|* 3 | INDEX UNIQUE SCAN | T_USERS_PK |
|* 4 | TABLE ACCESS BY INDEX ROWID | T_OBJECTS |
| 5 | BITMAP CONVERSION TO ROWIDS| |
|* 6 | BITMAP INDEX SINGLE VALUE | T_OBJECTS_IDX4 |
--------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("U"."USER_ID"=94)
4 - filter("O"."OBJECT_NAME" LIKE 'T_USERS')
6 - access("U"."USERNAME"="O"."OWNER")
PL/SQL procedure successfully completed
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26521853/viewspace-1313761/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle+高效能SQL引擎剖析:SQL優化與調優機制詳解-筆記之執行計劃(二)OracleSQL優化筆記
- Oracle 高效能SQL引擎剖析--SQL優化與調優機制詳解OracleSQL優化
- 【sql調優之執行計劃】獲取執行計劃SQL
- 【sql調優之執行計劃】sort operationsSQL
- 【sql調優之執行計劃】estimator iSQL
- 【sql調優之執行計劃】hash joinSQL
- 【sql調優之執行計劃】query transformerSQLORM
- 【sql調優之執行計劃】temp table transformationSQLORM
- 【sql調優之執行計劃】in相關的operationSQL
- 【sql調優之執行計劃】merge sort joinSQL
- 【sql調優之執行計劃】使用hint(五)Hint for parallelSQLParallel
- 【sql調優之執行計劃】使用hint(四)Hints for JoinSQL
- oracle筆記整理13——效能調優之SQL優化Oracle筆記SQL優化
- 【sql調優之執行計劃】使用hint(一)Hints for Optimization Approaches and GoalsSQLAPPGo
- 【sql調優之執行計劃】使用hint(三)Hints for Query TransformationsSQLORM
- 【sql調優之執行計劃】使用hint(二)Hints for Access PathsSQL
- MySQL之SQL優化詳解(一)MySql優化
- 【sql調優之執行計劃】使用hint(六) append and noappendSQLAPP
- 【sql調優之執行計劃】merge join cartesian and buffer sortSQL
- oracle筆記整理14——效能調優之oracle執行計劃Oracle筆記
- 使用leading(,)優化sql執行計劃優化SQL
- SQL優化案例-從執行計劃定位SQL問題(三)SQL優化
- 剖析SQL Server執行計劃SQLServer
- 【sql調優之執行計劃】merge semi join and merge anti joinSQL
- 【sql調優之執行計劃】nested loops join and nested loop join outerSQLOOP
- 通過分析SQL語句的執行計劃優化SQL 二SQL優化
- SQL優化筆記SQL優化筆記
- sql的執行計劃 詳解SQL
- mysql調優之——執行計劃explainMySqlAI
- Oracle中SQL調優(SQL TUNING)之最權威獲取SQL執行計劃大全OracleSQL
- MySQL之SQL優化詳解(二)MySql優化
- MySQL之SQL優化詳解(三)MySql優化
- 通過分析SQL語句的執行計劃優化SQL(總結)SQL優化
- 通過分析SQL語句的執行計劃優化SQL語句SQL優化
- 轉摘_使用leading(,)優化sql執行計劃優化SQL
- Oracle Sql優化筆記OracleSQL優化筆記
- Oracle調優之看懂Oracle執行計劃Oracle
- ORACLE SQL調優之執行計劃與隱藏引數_complex_view_mergingOracleSQLView