Oracle SQL優化之sql tuning advisor
有時在分析複雜SQL效能問題時比較耗時,當沒有頭緒時,可以通過sql tuning advisor看一下oracle的建議,
但是最終是否接受建議,需要DBA根據綜合情況自己判斷,算是增加了一種調優的方式。
一:建立測試資料
SQL> create user cjc identified by cjc; User created. SQL> grant connect,resource,dba to cjc; Grant succeeded. SQL> conn cjc/cjc Connected. SQL> create table t1 as select * from dba_tables; Table created. SQL> create table t2 as select * from dba_users; Table created. SQL> create table t3 as select * from dba_objects; Table created. SQL> create table t5 as select * from dba_data_files; Table created. SQL> create view v1 as select t1.table_name,t1.owner,t1.TABLESPACE_NAME,t1.NUM_ROWS,t3.CREATED,t3.LAST_DDL_TIME from t1,t3 where t1.table_name=t3.object_name and t1.owner=t3.owner; View created.
根據實際情況,按使用者、庫或表級別收集統計資訊
---SQL> exec dbms_stats.gather_schema_stats('CJC'); ---SQL> exec DBMS_STATS.GATHER_DATABASE_STATS (estimate_percent=>100,Degree=>8, Cascade=>TRUE, Granularity=>'ALL');
二:模擬效能差的SQL
SQL> set line 300 col table_name for a20 col owner for a20 col TABLESPACE_NAME for a20 select * from v1 where table_name='SDO_FEATURE_USAGE'; TABLE_NAME OWNER TABLESPACE_NAME NUM_ROWS CREATED LAST_DDL_ -------------------- -------------------- -------------------- ---------- --------- --------- SDO_FEATURE_USAGE MDSYS SYSAUX 20 07-NOV-20 07-NOV-20
三:執行sql tuning advisor
檢視耗時sql對應sql_id
SQL> select sql_id,sql_text from v$sql where sql_text like '%SDO_FEATURE_USAGE%'; 20vymfjk9wqw8 select * from v1 where table_name='SDO_FEATURE_USAGE'
生成並執行TUNING TASK
SQL> DECLARE my_task_name VARCHAR2(30); BEGIN my_task_name := DBMS_SQLTUNE.CREATE_TUNING_TASK(sql_id => '20vymfjk9wqw8', scope => 'COMPREHENSIVE', time_limit => 3600, task_name => 'cjc_sql_tuing_task', description => 'SQL TUNE TEST'); DBMS_SQLTUNE.EXECUTE_TUNING_TASK(task_name => 'cjc_sql_tuing_task'); END; / PL/SQL procedure successfully completed.
檢視是否生成TUNING TASK
SQL> SELECT status FROM DBA_ADVISOR_TASKS WHERE task_name = 'cjc_sql_tuing_task'; STATUS ----------- COMPLETED
檢視建議
根據給出的建議可知,可以建立如下索引:
create index CJC.IDX$$_00970001 on CJC.T3("OBJECT_NAME","OWNER");
效能預計提升98.58%。
SQL> SET LONG 10000 SET LONGCHUNKSIZE 1000 SET LINESIZE 300 SELECT DBMS_SQLTUNE.REPORT_TUNING_TASK('cjc_sql_tuing_task') FROM DUAL;
具體建議如下:
DBMS_SQLTUNE.REPORT_TUNING_TASK('CJC_SQL_TUING_TASK') ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ GENERAL INFORMATION SECTION ------------------------------------------------------------------------------- Tuning Task Name : cjc_sql_tuing_task Tuning Task Owner : SYS Workload Type : Single SQL Statement Scope : COMPREHENSIVE Time Limit(seconds): 3600 Completion Status : COMPLETED Started at : 01/31/2021 19:18:31 Completed at : 01/31/2021 19:18:31 DBMS_SQLTUNE.REPORT_TUNING_TASK('CJC_SQL_TUING_TASK') ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------- Schema Name: CJC SQL ID : 20vymfjk9wqw8 SQL Text : select * from v1 where table_name='SDO_FEATURE_USAGE' ------------------------------------------------------------------------------- FINDINGS SECTION (1 finding) ------------------------------------------------------------------------------- 1- Index Finding (see explain plans section below) -------------------------------------------------- DBMS_SQLTUNE.REPORT_TUNING_TASK('CJC_SQL_TUING_TASK') ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ The execution plan of this statement can be improved by creating one or more indices. Recommendation (estimated benefit: 98.58%) ------------------------------------------ - Consider running the Access Advisor to improve the physical schema design or creating the recommended index. create index CJC.IDX$$_00970001 on CJC.T3("OBJECT_NAME","OWNER"); - Consider running the Access Advisor to improve the physical schema design or creating the recommended index. DBMS_SQLTUNE.REPORT_TUNING_TASK('CJC_SQL_TUING_TASK') ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ create index CJC.IDX$$_00970002 on CJC.T1("TABLE_NAME","OWNER"); Rationale --------- Creating the recommended indices significantly improves the execution plan of this statement. However, it might be preferable to run "Access Advisor" using a representative SQL workload as opposed to a single statement. This will allow to get comprehensive index recommendations which takes into account index maintenance overhead and additional space consumption. ------------------------------------------------------------------------------- DBMS_SQLTUNE.REPORT_TUNING_TASK('CJC_SQL_TUING_TASK') ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ EXPLAIN PLANS SECTION ------------------------------------------------------------------------------- 1- Original ----------- Plan hash value: 3101620303 --------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | |1 | 89 | 425 (1)| 00:00:01 | DBMS_SQLTUNE.REPORT_TUNING_TASK('CJC_SQL_TUING_TASK') ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |* 1 | HASH JOIN | |1 | 89 | 425 (1)| 00:00:01 | |* 2 | TABLE ACCESS FULL| T1 |1 | 33 | 33 (0)| 00:00:01 | |* 3 | TABLE ACCESS FULL| T3 |1 | 56 | 392 (1)| 00:00:01 | --------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - access("T1"."TABLE_NAME"="T3"."OBJECT_NAME" AND "T1"."OWNER"="T3"."OWNER") 2 - filter("T1"."TABLE_NAME"='SDO_FEATURE_USAGE') DBMS_SQLTUNE.REPORT_TUNING_TASK('CJC_SQL_TUING_TASK') ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 3 - filter("T3"."OBJECT_NAME"='SDO_FEATURE_USAGE') 2- Using New Indices -------------------- Plan hash value: 976976346 -------------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 89 | 6 (0)| 00:00:01 | | 1 | NESTED LOOPS | | 1 | 89 | 6 (0)| 00:00:01 | DBMS_SQLTUNE.REPORT_TUNING_TASK('CJC_SQL_TUING_TASK') ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | 2 | NESTED LOOPS | | 1 | 89 | 6 (0)| 00:00:01 | | 3 | TABLE ACCESS BY INDEX ROWID BATCHED| T3 | 1 | 56 | 4 (0)| 00:00:01 | |* 4 | INDEX RANGE SCAN | IDX$$_00970001 | 1 | | 3 (0)| 00:00:01 | |* 5 | INDEX RANGE SCAN | IDX$$_00970002 | 1 | | 1 (0)| 00:00:01 | | 6 | TABLE ACCESS BY INDEX ROWID | T1 | 1 | 33 | 2 (0)| 00:00:01 | -------------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 4 - access("T3"."OBJECT_NAME"='SDO_FEATURE_USAGE') DBMS_SQLTUNE.REPORT_TUNING_TASK('CJC_SQL_TUING_TASK') ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 5 - access("T1"."TABLE_NAME"='SDO_FEATURE_USAGE' AND "T1"."OWNER"="T3"."OWNER") -------------------------------------------------------------------------------
###chenjuchao 2021-01-31 19:35###
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29785807/viewspace-2754886/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- oracle SQL最佳化器SQL Tuning Advisor (STA)OracleSQL
- Oracle優化案例-關閉auto space advisor和sql tuning advisor(十九)Oracle優化SQL
- 使用sql tuning advisor最佳化sqlSQL
- SQL Access Advisor、SQL Tuning Advisor 測試SQL
- Oracle自帶工具sql優化集-SQL Tuning Advisor (使用心得體會)OracleSQL優化
- Oracle 11 sql tuning advisor sql access advisor關閉以及job檢視與停止OracleSQL
- Oracle SQL調優系列之SQL Monitor ReportOracleSQL
- SQL Access Advisor(zt)SQL
- Oracle效能優化-SQL優化(案例一)Oracle優化SQL
- Oracle效能優化-SQL優化(案例二)Oracle優化SQL
- Oracle效能優化-SQL優化(案例三)Oracle優化SQL
- Oracle效能優化-SQL優化(案例四)Oracle優化SQL
- sql優化之邏輯優化SQL優化
- SQL優化之limit 1SQL優化MIT
- MySQL之SQL優化技巧MySql優化
- MySQL之SQL語句優化MySql優化
- Oracle某行系統SQL優化案例(三)OracleSQL優化
- Oracle某行系統SQL優化(案例五)OracleSQL優化
- Oracle某行系統SQL優化案例(二)OracleSQL優化
- Oracle 某行系統SQL優化案例(一)OracleSQL優化
- Oracle 高效能SQL引擎剖析--SQL優化與調優機制詳解OracleSQL優化
- 神奇的 SQL 之效能優化 → 讓 SQL 飛起來SQL優化
- 十七、Mysql之SQL優化查詢MySql優化
- MySQL之SQL優化詳解(二)MySql優化
- MySQL之SQL優化詳解(三)MySql優化
- MySQL之SQL優化詳解(一)MySql優化
- Oracle SQL效能優化的40條軍規OracleSQL優化
- SQL SERVER優化SQLServer優化
- SQL優化指南SQL優化
- [20191112]SQL Tuning by adding column alias (2).txtSQL
- Oracle 效能調優工具:SQL MonitorOracleSQL
- 如何調優 Oracle SQL系列文章:查詢優化器介紹OracleSQL優化
- 資料庫優化 - SQL優化資料庫優化SQL
- SQL優化參考SQL優化
- sql優化專題SQL優化
- SQL語句優化SQL優化
- SQL效能優化技巧SQL優化
- MySQL-SQL優化MySql優化