SQL Access Advisor的 DBMS_ADVISOR.QUICK_TUNE 使用
SQL Access Advisor是伴隨著10G出現的一個優化工具,提供對錶分割槽,物化檢視,索引,物化檢視日誌優化建議.下邊通過對單個SQL的執行,檢視Oracle給出的建議.
1.建表並生成測試資料
SQL> connect scott/scott
Connected.
SQL> drop table tb_test;
SQL> create table tb_test(id number not null,name varchar2(30));
Table created.
SQL> create index idx_tb_test on tb_test(id);
Index created.
SQL> declare
begin
for i in 1 .. 100000 loop
insert into tb_test values (i, 'test');
commit;
end loop;
end;
Connected.
SQL> drop table tb_test;
SQL> create table tb_test(id number not null,name varchar2(30));
Table created.
SQL> create index idx_tb_test on tb_test(id);
Index created.
SQL> declare
begin
for i in 1 .. 100000 loop
insert into tb_test values (i, 'test');
commit;
end loop;
end;
2.分析表
connect / as sysdba
begin
dbms_stats.gather_table_stats(ownname => 'SCOTT', tabname => 'TB_TEST',cascade => true);
end;
dbms_stats.gather_table_stats(ownname => 'SCOTT', tabname => 'TB_TEST',cascade => true);
end;
3.使用dbms_advisor.quick_tune生成優化建議
connect / as sysdba
declare
l_task_name VARCHAR2(255);
l_sql_stmt VARCHAR2(4000);
begin
l_sql_stmt := 'select /*+ full(t) */ * from scott.tb_test t where t.id = :1';
l_task_name := 'MY_FULL_ACCESS_TEST';
DBMS_ADVISOR.QUICK_TUNE(DBMS_ADVISOR.SQLACCESS_ADVISOR,
l_task_name,
l_sql_stmt);
exception when others then dbms_output.put_line(sqlerrm);
end;
l_task_name VARCHAR2(255);
l_sql_stmt VARCHAR2(4000);
begin
l_sql_stmt := 'select /*+ full(t) */ * from scott.tb_test t where t.id = :1';
l_task_name := 'MY_FULL_ACCESS_TEST';
DBMS_ADVISOR.QUICK_TUNE(DBMS_ADVISOR.SQLACCESS_ADVISOR,
l_task_name,
l_sql_stmt);
exception when others then dbms_output.put_line(sqlerrm);
end;
我們這裡造一個走全表掃描的索引,很明顯走索引才是正確的,這裡的目的主要是看oracle會給出什麼樣的建議.
4.檢視優化建議
SQL> set serveroutput on;
SQL> set long 999999999;
SQL> begin
show_recm('MY_FULL_ACCESS_TEST');
end;
SQL> set long 999999999;
SQL> begin
show_recm('MY_FULL_ACCESS_TEST');
end;
=========================================
Task_name = MY_FULL_ACCESS_TEST
Action ID: 1
Command : RETAIN INDEX
Attr1 (name) : "SCOTT"."IDX_TB_TEST_N1"
Attr2 (tablespace):
Attr3 : "SCOTT"."TB_TEST"
Attr4 : BTREE
Attr5 :
----------------------------------------
=========END RECOMMENDATIONS============
Task_name = MY_FULL_ACCESS_TEST
Action ID: 1
Command : RETAIN INDEX
Attr1 (name) : "SCOTT"."IDX_TB_TEST_N1"
Attr2 (tablespace):
Attr3 : "SCOTT"."TB_TEST"
Attr4 : BTREE
Attr5 :
----------------------------------------
=========END RECOMMENDATIONS============
PL/SQL procedure successfully completed.
從以上輸出可以看出oracle給出的建議是走索引.
show_recm過程如下:
CREATE OR REPLACE PROCEDURE show_recm (in_task_name IN VARCHAR2) IS
CURSOR curs IS
SELECT DISTINCT action_id, command, attr1, attr2, attr3, attr4
FROM dba_advisor_actions
WHERE task_name = in_task_name
ORDER BY action_id;
v_action number;
v_command VARCHAR2(32);
v_attr1 VARCHAR2(4000);
v_attr2 VARCHAR2(4000);
v_attr3 VARCHAR2(4000);
v_attr4 VARCHAR2(4000);
v_attr5 VARCHAR2(4000);
BEGIN
OPEN curs;
DBMS_OUTPUT.PUT_LINE('=========================================');
DBMS_OUTPUT.PUT_LINE('Task_name = ' || in_task_name);
LOOP
FETCH curs INTO
v_action, v_command, v_attr1, v_attr2, v_attr3, v_attr4 ;
EXIT when curs%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Action ID: ' || v_action);
DBMS_OUTPUT.PUT_LINE('Command : ' || v_command);
DBMS_OUTPUT.PUT_LINE('Attr1 (name) : ' || SUBSTR(v_attr1,1,30));
DBMS_OUTPUT.PUT_LINE('Attr2 (tablespace): ' || SUBSTR(v_attr2,1,30));
DBMS_OUTPUT.PUT_LINE('Attr3 : ' || SUBSTR(v_attr3,1,30));
DBMS_OUTPUT.PUT_LINE('Attr4 : ' || v_attr4);
DBMS_OUTPUT.PUT_LINE('Attr5 : ' || v_attr5);
DBMS_OUTPUT.PUT_LINE('----------------------------------------');
END LOOP;
CLOSE curs;
DBMS_OUTPUT.PUT_LINE('=========END RECOMMENDATIONS============');
END show_recm;
CURSOR curs IS
SELECT DISTINCT action_id, command, attr1, attr2, attr3, attr4
FROM dba_advisor_actions
WHERE task_name = in_task_name
ORDER BY action_id;
v_action number;
v_command VARCHAR2(32);
v_attr1 VARCHAR2(4000);
v_attr2 VARCHAR2(4000);
v_attr3 VARCHAR2(4000);
v_attr4 VARCHAR2(4000);
v_attr5 VARCHAR2(4000);
BEGIN
OPEN curs;
DBMS_OUTPUT.PUT_LINE('=========================================');
DBMS_OUTPUT.PUT_LINE('Task_name = ' || in_task_name);
LOOP
FETCH curs INTO
v_action, v_command, v_attr1, v_attr2, v_attr3, v_attr4 ;
EXIT when curs%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Action ID: ' || v_action);
DBMS_OUTPUT.PUT_LINE('Command : ' || v_command);
DBMS_OUTPUT.PUT_LINE('Attr1 (name) : ' || SUBSTR(v_attr1,1,30));
DBMS_OUTPUT.PUT_LINE('Attr2 (tablespace): ' || SUBSTR(v_attr2,1,30));
DBMS_OUTPUT.PUT_LINE('Attr3 : ' || SUBSTR(v_attr3,1,30));
DBMS_OUTPUT.PUT_LINE('Attr4 : ' || v_attr4);
DBMS_OUTPUT.PUT_LINE('Attr5 : ' || v_attr5);
DBMS_OUTPUT.PUT_LINE('----------------------------------------');
END LOOP;
CLOSE curs;
DBMS_OUTPUT.PUT_LINE('=========END RECOMMENDATIONS============');
END show_recm;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29196873/viewspace-1103534/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SQL Access Advisor!SQL
- SQL Access Advisor(zt)SQL
- SQL Access Advisor 概要SQL
- SQL Access Advisor 與SQL Tuning AdvisorSQL
- 使用dbms_advisor來執行sql access advisorSQL
- SQL Access Advisor、SQL Tuning Advisor 測試SQL
- sql tuning advisor和sql access advisor區別SQL
- 手工執行sql tuning advisor和sql access advisorSQL
- 在EM中使用SQL Access Advisor(SQL訪問建議)優化SQLSQL優化
- 透過shell定製dbms_advisor.quick_tuneUI
- 通過shell定製dbms_advisor.quick_tuneUI
- Oracle 11 sql tuning advisor sql access advisor關閉以及job檢視與停止OracleSQL
- Oracle OCP IZ0-053 Q232(sql access advisor)OracleSQL
- 使用sql tuning advisor最佳化sqlSQL
- Oracle OCP 1Z0 053 Q40(DBMS_ADVISOR.QUICK_TUNE)OracleUI
- Oracle OCP 1Z0 053 Q123(SQL Access Advisor)OracleSQL
- Sql Tuning Advisor 使用方法SQL
- SQL Tuning Advisor使用例項SQL
- ORACLE SQL TUNING ADVISOR 使用方法OracleSQL
- SQL Repair AdvisorSQLAI
- SQL TUNING ADVISORSQL
- 使用SQL Profile及SQL Tuning Advisor固定執行計劃SQL
- [原創]ORACLE SQL TUNING ADVISOR 使用方法OracleSQL
- 在EM中使用SQL Tuning Advisor(SQL優化建議)優化SQLSQL優化
- SQL Tuning Advisor簡介SQL
- Oracle SQL優化之sql tuning advisorOracleSQL優化
- oracle實用sql(3)--sql tuning advisorOracleSQL
- access 轉 sql 之一SQL
- sql tuning advisor(STA) 建議 建立sql profileSQL
- Oracle SQL優化之sql tuning advisor(STA)OracleSQL優化
- sql tuning set/sql tuning advisor(待完善)SQL
- 深入瞭解SQL Tuning AdvisorSQL
- SQL Tuning Advisor : dbms_sqltuneSQL
- oracle實用sql(4)--undo advisorOracleSQL
- oracle實用sql(2)--segment advisorOracleSQL
- Oracle自帶工具sql優化集-SQL Tuning Advisor (使用心得體會)OracleSQL優化
- Sql Tuning Advisor的大致過程測試!SQL
- oracle SQL最佳化器SQL Tuning Advisor (STA)OracleSQL