SQL Tuning Overview

1,553 views
1,450 views

Published on

A brief overview of options for developers to have a better understanding of the art of tuning their SQL.

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,553
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
0
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

SQL Tuning Overview

  1. 1. By Kai Liu McDOUG 7/15/2009
  2. 2. Main tools for Developers <ul><li>Autotrace </li></ul><ul><li>SQL Trace </li></ul><ul><li>Explain Plan </li></ul><ul><li>Rule-based Optimization </li></ul><ul><li>How I Approach Tuning a SQL Statement </li></ul>
  3. 3. Autotrace <ul><li>Get a report on the execution path used by the SQL optimizer and the statement execution statistics </li></ul><ul><li>SET AUTOTRACE ON - The AUTOTRACE report includes both the optimizer execution path and the SQL statement execution statistics. </li></ul><ul><li>SET AUTOTRACE TRACE EXP – The AUTOTRACE report only includes the optimizer execution path. </li></ul><ul><li>$ORACLE_HOME/rdbms/admin/utlxpls.sql : script to format explain plans </li></ul>
  4. 4. Autotrace Continue <ul><li>SET AUTOTRACE OFF- No AUTOTRACE report is generated. This is the default.   </li></ul><ul><li>SET AUTOTRACE ON EXPLAIN - The AUTOTRACE report shows only the optimizer execution path.   </li></ul><ul><li>SET AUTOTRACE ON STATISTICS - The AUTOTRACE report shows only the SQL statement execution statistics.   </li></ul><ul><li>SET AUTOTRACE ON - The AUTOTRACE report includes both the optimizer execution path and the SQL statement execution statistics.   </li></ul><ul><li>SET AUTOTRACE TRACEONLY - Like SET AUTOTRACE ON, but suppresses the printing of the user's query output, if any. </li></ul>
  5. 5. SQL Trace <ul><li>alter session set sql_trace=true; 10g (sql_trace being deprecated) </li></ul><ul><li>execute dbms_monitor.session_trace_enable </li></ul><ul><li>Check /oracle/admin/dmsdev/udump </li></ul><ul><li>ls –lrt *.trc </li></ul><ul><li>execute dbms_monitor.session_trace_disable </li></ul>
  6. 6. Explain Plan <ul><li>Used to find out how the Oracle Optimizer processes a statement. The Optimizer is a program that examines the SQL statement as presented by a user and then devises an execution plan to execute the statement. </li></ul><ul><li>Oracle's Cost Based Optimizer comes up with an execution plan that is hopefully the most efficient way to resolve the query, but for many reasons it will often choose a sub-optimal plan. </li></ul>
  7. 7. Explain Plan Continue <ul><li>Syntax of the explain plan command: </li></ul><ul><ul><li>explain plan [set statement id = ’<name>’] for <statement>; </li></ul></ul><ul><li>Explain Plan set statement id = ‘<statement>’ for select ENAME, JOB, SAL, DNAME from EMP, DEPT </li></ul><ul><ul><li>where EMP.DEPTNO = DEPT.DEPTID </li></ul></ul><ul><ul><li>and not exists </li></ul></ul><ul><ul><li>(select from SALGRADE </li></ul></ul><ul><ul><li>where EMP.SAL between LOSAL and HISAL); </li></ul></ul>
  8. 8. Explain Plan Continue <ul><li>select statement to retrieve information about the (hierarchical) </li></ul><ul><li>execution plan determined by the query optimizer: </li></ul><ul><li>select lpad(’ ’, 2*(LEVEL-1)) “OPERATION”, </li></ul><ul><li>substr(OPTIONS,1,9) “OPTIONS”, OBJECT NAME, ID, PARENT ID </li></ul><ul><li>from PLAN TABLE </li></ul><ul><li>start with ID = 0 [and STATEMENT ID = ’<statement>’] </li></ul><ul><ul><li>connect by prior ID = PARENT ID </li></ul></ul><ul><li>[and STATEMENT ID = ’<statement>’;] </li></ul><ul><li>OPERATION OPTIONS OBJECT_NAME ID PARENT_ID </li></ul><ul><li>--------------------- -------- ----------- -- --------- </li></ul><ul><li>SELECT STATEMENT 0 </li></ul><ul><li>FILTER 1 0 </li></ul><ul><li>MERGE JOIN 2 1 </li></ul><ul><li> SORT JOIN 3 2 </li></ul><ul><li> TABLE ACCESS FULL DEPT 4 3 </li></ul><ul><li> SORT JOIN 5 2 </li></ul><ul><li>TABLE ACCESS FULL EMP 6 5 </li></ul><ul><li>TABLE ACCESS FULL SALGRADE 7 1 </li></ul>
  9. 9. Rule-based Optimization(RBO) Access Path <ul><li>Access row using Rowid (TID) </li></ul><ul><li>Access row using Cluster-Join </li></ul><ul><li>Access row Hash-Cluster-Key </li></ul><ul><li>Access row using primary key </li></ul><ul><li>Cluster-Join </li></ul><ul><li>Hash-Cluster-Key </li></ul><ul><li>Clusterkey with Index </li></ul><ul><li>Composed Index </li></ul><ul><li>Single-Row Index </li></ul><ul><li>Search Index range </li></ul><ul><li>Search Index without range </li></ul><ul><li>Merge Join </li></ul><ul><li>Use min or max on indexed column </li></ul><ul><li>Use order by on Index </li></ul><ul><li>Full Table Scan </li></ul>
  10. 10. How I Approach Tuning a SQL Statement <ul><li>Analyze the Statement </li></ul><ul><li>Compare Plans </li></ul><ul><li>Test it out </li></ul><ul><li>Tuning Hints and Tips </li></ul>

×