1、創建基於cache的job
DECLARE
l_taskname VARCHAR2(30) := 'test_sql_access_task';
l_task_desc VARCHAR2(128) := 'Test SQL Access Task';
l_wkld_name VARCHAR2(30) := 'test_work_load';
l_saved_rows NUMBER := 0;
l_failed_rows NUMBER := 0;
l_num_found NUMBER;
BEGIN
-- Create an SQLAccess Advisor task.
DBMS_ADVISOR.create_task(
advisor_name => DBMS_ADVISOR.sqlaccess_advisor,
task_name => l_taskname,
task_desc => l_task_desc);
-- Reset the task.
DBMS_ADVISOR.reset_task(task_name => l_taskname);
-- Create a workload.
SELECT COUNT(*)
INTO l_num_found
FROM user_advisor_sqlw_sum
WHERE workload_name = l_wkld_name;
IF l_num_found = 0 THEN
DBMS_ADVISOR.create_sqlwkld(workload_name => l_wkld_name);
END IF;
-- Link the workload to the task.
SELECT count(*)
INTO l_num_found
FROM user_advisor_sqla_wk_map
WHERE task_name = l_taskname
AND workload_name = l_wkld_name;
IF l_num_found = 0 THEN
DBMS_ADVISOR.add_sqlwkld_ref(
task_name => l_taskname,
workload_name => l_wkld_name);
END IF;
-- Set workload parameters.
DBMS_ADVISOR.set_sqlwkld_parameter(l_wkld_name,
'ACTION_LIST',
DBMS_ADVISOR.ADVISOR_UNUSED);
DBMS_ADVISOR.set_sqlwkld_parameter(l_wkld_name,
'MODULE_LIST',
DBMS_ADVISOR.ADVISOR_UNUSED);
DBMS_ADVISOR.set_sqlwkld_parameter(l_wkld_name,
'SQL_LIMIT',
DBMS_ADVISOR.ADVISOR_UNLIMITED);
DBMS_ADVISOR.set_sqlwkld_parameter(l_wkld_name,
'ORDER_LIST',
'PRIORITY,OPTIMIZER_COST');
DBMS_ADVISOR.set_sqlwkld_parameter(l_wkld_name,
'USERNAME_LIST',
DBMS_ADVISOR.ADVISOR_UNUSED);
DBMS_ADVISOR.set_sqlwkld_parameter(l_wkld_name,
'VALID_TABLE_LIST',
DBMS_ADVISOR.ADVISOR_UNUSED);
--
DBMS_ADVISOR.import_sqlwkld_sqlcache(l_wkld_name,
'REPLACE',
2,
l_saved_rows,
l_failed_rows);
-- Set task parameters.
DBMS_ADVISOR.set_task_parameter(l_taskname,
'_MARK_IMPLEMENTATION',
'FALSE');
DBMS_ADVISOR.set_task_parameter(l_taskname,
'EXECUTION_TYPE',
'INDEX_ONLY');
DBMS_ADVISOR.set_task_parameter(l_taskname, 'MODE', 'COMPREHENSIVE');
DBMS_ADVISOR.set_task_parameter(l_taskname,
'STORAGE_CHANGE',
DBMS_ADVISOR.ADVISOR_UNLIMITED);
DBMS_ADVISOR.set_task_parameter(l_taskname, 'DML_VOLATILITY', 'TRUE');
DBMS_ADVISOR.set_task_parameter(l_taskname,
'ORDER_LIST',
'PRIORITY,OPTIMIZER_COST');
DBMS_ADVISOR.set_task_parameter(l_taskname, 'WORKLOAD_SCOPE', 'PARTIAL');
DBMS_ADVISOR.set_task_parameter(l_taskname,
'DEF_INDEX_TABLESPACE',
DBMS_ADVISOR.ADVISOR_UNUSED);
DBMS_ADVISOR.set_task_parameter(l_taskname,
'DEF_INDEX_OWNER',
DBMS_ADVISOR.ADVISOR_UNUSED);
DBMS_ADVISOR.set_task_parameter(l_taskname,
'DEF_MVIEW_TABLESPACE',
DBMS_ADVISOR.ADVISOR_UNUSED);
DBMS_ADVISOR.set_task_parameter(l_taskname,
'DEF_MVIEW_OWNER',
DBMS_ADVISOR.ADVISOR_UNUSED);
-- Execute the task.
DBMS_ADVISOR.execute_task(task_name => l_taskname);
END;
/
2、查看saa建議
SET LONG 100000
SET PAGESIZE 50000
SELECT DBMS_ADVISOR.get_task_script('test_sql_access_task') AS script FROM dual;
3、查看saa的建議的性能提高效果預估
SELECT sql_id,
precost 優化前cost,
postcost 優化後cost,
(precost / postcost) cost提升倍數,decode(PRIORITY, 1, '高', 2, '中', 3, '低') 重要性
FROM dba_advisor_sqla_wk_stmts
WHERE
task_name='test_sql_access_task';
4、反查sql語法
select SQL_TEXT,SQL_FULLTEXT,SQL_ID,LOADS,FIRST_LOAD_TIME,PLSQL_EXEC_TIME,JAVA_EXEC_TIME,ROWS_PROCESSED,COMMAND_TYPE,PARSING_USER_ID,PARSING_SCHEMA_ID,PARSING_SCHEMA_NAME,KEPT_VERSIONS,ADDRESS,SERVICE,MODULE,MODULE_HASH,ACTION,CPU_TIME,ELAPSED_TIME,OUTLINE_SID,CHILD_ADDRESS,SQLTYPE,REMOTE,OBJECT_STATUS,LITERAL_HASH_VALUE,LAST_LOAD_TIME,PROGRAM_ID,PROGRAM_LINE#,LAST_ACTIVE_TIME
from v$sql where sql_id in (
SELECT sql_id
FROM dba_advisor_sqla_wk_stmts
WHERE
task_name='test_sql_access_task' and (precost / postcost)>10 and postcost<>0)
select * from dba_hist_sqltext where sql_id in (
SELECT sql_id
FROM dba_advisor_sqla_wk_stmts
WHERE
task_name='test_sql_access_task' and (precost / postcost)>10 and postcost<>0)
select distinct substr(to_char(sql_text),1,20) from dba_hist_sqltext where sql_id in (
SELECT sql_id
FROM dba_advisor_sqla_wk_stmts
WHERE
task_name='test_sql_access_task' and (precost / postcost)>10 and postcost<>0)
select distinct to_char(sql_text) from dba_hist_sqltext where sql_id in (
SELECT sql_id
FROM dba_advisor_sqla_wk_stmts
WHERE
task_name='test_sql_access_task' and (precost / postcost)>10 and postcost<>0)
找那些提升明顯的sql進行優化