Sierra using sqltxplain to diagnose sql statements performing poorly v2

  • 1,205 views
Uploaded on

There are many SQL Performance Tuning tools for Oracle Database spread out in the market. SQLTXPLAIN, shortly referred by SQLT is one that is becoming increasingly popular these days. This is a native …

There are many SQL Performance Tuning tools for Oracle Database spread out in the market. SQLTXPLAIN, shortly referred by SQLT is one that is becoming increasingly popular these days. This is a native performance tuning tool of Oracle, and can help to provide adequate diagnostic information for a SQL Query and help to increase the Query Performance by refining it.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,205
On Slideshare
0
From Embeds
0
Number of Embeds
24

Actions

Shares
Downloads
0
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. 1 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 2. Using SQLTXPLAIN todiagnose SQL statementsperforming poorlyCarlos SierraConsulting Technical AdvisorCenter of Expertise (CoE)2 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 3. Carlos Sierra Consulting Technical Advisor - Center of Expertise (CoE)  Job/Hobby: SQL Tuning frequent practitioner  Experience: Oracle since 1996  Home: Orlando, Florida  Besides SQL Tuning: Spinning, Biking, Racquetball, Parenting!  Author of: SQLTXPLAIN (SQLT), TRCANLZR (TRCA), SQLHC, etc.3 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 4. Agenda Using SQLTXPLAIN to diagnose SQL statements performing poorly  Some SQL Tuning concepts  SQLTXPLAIN (SQLT) introduction  SQLT installation  SQLT main methods  SQLT main diagnostics report  Health Checks (HC) related to a SQL statement  SQL Test Case (TC) introduction4 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 5. Some SQL Tuning Concepts5 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 6. What is SQL Tuning? Art, Science or Black Magic?  Process to make a SQL statement execute faster  Help the optimizer produce an optimal execution plan  Associate a “good” execution plan to a SQL statement  Fix the performance of a SQL that used to run faster  A combination of the above… and maybe some others  Improve SQL performance to achieve a business target – While minimizing resources utilization6 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 7. What is Query Optimization? A function of many relational database management systems  Core function of the optimizer  Take a SQL statement and produce an “optimal” execution plan  Evaluation of many possible execution plans  Selection of an optimal execution plan  Complex and resource intensive computer task7 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 8. Cost-based Optimizer (CBO) Inputs a SQL statement and outputs an optimal execution plan  Inputs some knowledge about data structure, volume and distribution  Accuracy depends on reliable knowledge about the data  Needs to consider plan stability and plan flexibility policies  Lots of configuration parameters  Has to be fast! (sub second in most cases)  Not every execution plan is the most optimal plan8 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 9. CBO Statistics are important! Model to represent data volume and distribution  Schema object statistics  System statistics  Data dictionary statistics  Fixed objects statistics  Extended statistics  Dynamic sampling9 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 10. Some reasons for “bad” Execution Plans Quality of CBO output is a function of the quality of its input  CBO statistics – Small sample sizes, inconsistent gathering, missing, etc.  Non-default setup of CBO parameters  Lack of appropriate indexes  Cursor sharing strategy and implementation  SQL complexity and coding practices  Limitations and bugs (CBO, statistics model, etc.)  Data model design and application design10 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 11. SQL Tuning heads-up Results of a quick sanity-check may raise a red flag  Nested loops and index access for large row sources  Many distinct plans in AWR  Peeked binds not representative  Poor coding practices – T1.C1 = DECODE(:b1, :b2, T1.C1, :b3, T2.C2)  Questionable quality of CBO statistics  Overuse of CBO parameters to “tweak” the CBO  Starts * E-Rows ≈ A-Rows?11 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 12. Validating Estimated Cardinality Starts * E-Rows ≈ A-Rows? --------------------------------------------------------------------------------------------- | Id | Operation | Name | Starts | E-Rows | Cost | A-Rows | Buffers | --------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | | 1158 | 10 | 1634 | | 1 | SORT ORDER BY | | 1 | 175 | 1158 | 10 | 1634 | |* 2 | FILTER | | 1 | | | 10 | 1634 | | 3 | SORT GROUP BY | | 1 | 175 | 1158 | 308 | 1634 | |* 4 | HASH JOIN | | 1 | 3481 | 1155 | 351 | 1634 | | 5 | NESTED LOOPS | | 1 | 3481 | 77 | 352 | 587 | |* 6 | HASH JOIN | | 1 | 3481 | 74 | 352 | 233 | |* 7 | TABLE ACCESS FULL | CUSTOMER | 1 | 2150 | 26 | 1380 | 86 | |* 8 | TABLE ACCESS FULL | SALES_ORDER | 1 | 13521 | 47 | 2747 | 147 | |* 9 | INDEX UNIQUE SCAN | CUSTOMER_PK | 352 | 1 | 0 | 352 | 354 | | 10 | VIEW | | 1 | 33860 | 1077 | 33860 | 1047 | | 11 | SORT GROUP BY | | 1 | 33860 | 1077 | 33860 | 1047 | |* 12 | HASH JOIN | | 1 | 237K| 293 | 237K| 1047 | | 13 | TABLE ACCESS FULL| PART | 1 | 21396 | 46 | 21396 | 154 | | 14 | TABLE ACCESS FULL| ORDER_LINE | 1 | 237K| 242 | 237K| 893 | ---------------------------------------------------------------------------------------------12 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 13. LAST/ALL Over/Under Estimate Estim Card * LAST Starts ≈ LAST Output Rows?13 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 14. SQL Tuning Challenges Dependencies, dynamism, complexity and expertise  Selection of execution plan has many complex dependencies – Statistics, binds, parameters, features, etc.  Dependencies are both static and dynamic – Understanding prior plans requires understanding prior dependencies  Relevant expertise is a must to succeed in a reasonable time  SQL Tuning is a complex task!14 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 15. How do we approach SQL Tuning? Some techniques for SQL Tuning  Cross your fingers and hope for the issue to go away  Hire a 3rd party knowledgeable in SQL tuning  Enhance your team’s DBA skills  Look for some SQL Tuning “silver bullets”  A combination of some of the above(?)  Consider best practices and apply a sound methodology15 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 16. About SQL Tuning Silver Bullets Are there any robust silver bullets for SQL Tuning?  Null  Nil  Zero  No  Nope  Nada!16 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 17. SQL Tuning “Best Practices” Not a recipe but more of a systematic approach  Validate business performance targets  Identify “bad” SQL (not meeting business performance targets)  Collect comprehensive diagnostics information – Include execution metrics as well as plan dependencies  Perform sanity-checks a.k.a. health-checks (HC)  Invest time on an expert analysis  Apply scientific method  Implement required changes17 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 18. SQL Tuning Best Practices Apply Scientific Method  “Systematic observation, measurement, and experiment, and the formulation, testing, and modification of hypotheses” 1. Formulate hypotheses (include estimated gain) 2. Create a test case (TC) if needed 3. Test and reformulate hypotheses 4. If target is achieved stop else go to 118 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 19. How can a Tool help with SQL Tuning? A few things can actually be automated  Display detailed execution plans  Collect diagnostics information – Performance metrics, metadata, dependencies, etc. – Current and historical  Perform health-checks (HC) around the SQL statement  Provide some automated “expert” advice  Extract a test case (TC)  Provide commands for plan stability and gathering of stats, etc.19 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 20. SQL Tuning Tools Inventory Something old, something new, something borrowed, something red  Event 10053  EXPLAIN PLAN FOR, DBMS_XPLAN, AutoTrace  SQL Trace, TKPROF, Method-R, Trace Analyzer (TRCA)  Active Session History (ASH)  Active Monitor Report  DBMS_SQLDIAG and SQL Tuning Advisor (STA)  SQLTXPLAIN (SQLT) and SQL Health-Check (SQLHC)20 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 21. SQLTXPLAIN (SQLT)Introduction21 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 22. SQLTXPLAIN (SQLT) MOS 215187.1 Tool that helps to diagnose one SQL statement that performs poorly  Add-on tool  11g/10g (prior versions for 9i and 8i are available)  Linux, UNIX and Windows  Single instance, RAC and Exadata  Easy to install and use  Not intrusive  Resources conscious  Free download and use22 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 23. SQLTXPLAIN (SQLT) Benefits Why do I want to use it?  Comprehensive diagnostics details for one SQL statement  Over a hundred health-checks (HC) around the SQL statement  Invokes SQL Tuning Advisor (STA) *  Includes Active Session History (ASH) * and SQL Monitor Report *  Includes Automatic Workload Repository (AWR) *  Includes Automatic Database Diagnostic Monitor (ADDM) *  Automatic test case (TC) extraction  SQLT helps to expedite SQL tuning process23 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 24. SQLTXPLAIN (SQLT) mechanics How does SQLT work?  5 core methods to analyze one SQL statement  Inputs one SQL statement and outputs one zip file  Output contains a main html report and many other files  Includes AWR, ADDM, ASH, 10053, 10046, TKPROF, etc.  SQLT TC and 11g Test Case Builder (TCB) output are included  SQLT has additional modules and utilities  SQLT installs a schema and objects on it!24 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 25. SQLT Installation25 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 26. How do I install SQLT? My Oracle Support (MOS) 215187.1  Download tool (sqlt.zip file)  Unzip into database server  Execute sqlt/install/sqcreate.sql connected as SYS26 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 27. SQLT Installation Parameters Provided inline or when asked  Optional Connect Identifier (ie: @PROD)  Password for user SQLTXPLAIN  Default tablespace  Temporary tablespace  Main application user of SQLT  Oracle Pack license [ T | D | N ]27 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 28. How do you know Installation succeeded?28 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 29. How do I uninstall SQLT In case you no longer need SQLT  Execute sqlt/install/sqdrop.sql connected as SYS29 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 30. SQLT Main Methods30 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 31. SQLT Main Methods All act on one SQL statement  SQLT XTRACT  SQLT XECUTE  SQLT XTRXEC  SQLT XPLAIN  SQLT XTRSBY31 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 32. How to use SQLT Main Methods All 5 have these requirements  SQL*Plus connecting as application user  Application user must have SQLT_USER_ROLE granted  Password for SQLTXPLAIN must be provided  One SQL per use of SQLT  SQLT has its own configuration parameters32 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 33. SQLT XTRACT Script sqlt/run/sqltxtract.sql  Most common method  Inputs SQL_ID  SQL statement is not executed33 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 34. SQLT XECUTE Script sqlt/run/sqltxecute.sql  Inputs a script name which contains – ALTER SESSION commands (if applicable) – Binds declaration and assignment (if applicable) – SQL statement  Executes the SQL statement34 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 35. SQLT XTRXEC Script sqlt/run/sqltxtrxec.sql  Combines XTRACT and XECUTE  Inputs SQL_ID35 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 36. SQLT XPLAIN Script sqlt/run/sqltxplain.sql  Inputs a file name which contains – SQL statement  What if SQL contains binds? – Leave them in place, OR – Replace with literals of same data type36 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 37. SQLT XTRSBY Script sqlt/run/sqltxtrsby.sql  For read-only databases  Executes in Primary and connects into stand-by  Works like XTRACT  Inputs SQL_ID and DBLINK37 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 38. SQLT Input SQL_ID or Binds Read-Only HASH_VALUE SQL Text Values Database XTRACT X XECUTE X X XTRXEC X XPLAIN X XTRSBY X X38 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 39. XTRXEC XTRACTSQLT Output XPLAIN Common Output XTRSBY XECUTE39 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 40. SQLT Main DiagnosticsReport40 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 41. What is included in Main Report? Partial list of contents (1/3)  Health-checks  SQL text  Parameters  Cursor sharing  SQL Tuning Advisor (STA) report  Execution plans41 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 42. What is included in Main Report? Partial list of contents (2/3)  Plan stability  Active Session History (ASH)  SQL performance metrics  SQL Monitor  Segment and session statistics  Tables42 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 43. What is included in Main Report? Partial list of contents (3/3)  Indexes  CBO statistics  Objects and dependencies  Policies  Metadata43 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 44. 44 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 45. Navigating the SQLT Main Html Report Typical Navigation 1. SQL Text 2. Plans Summary (execution plans) 3. Observations (health-checks) 4. Branch as per findings45 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 46. 46 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 47. Demonstration SQLT Main Report Navigation47 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 48. Health Checks (HC) related toa SQL statement48 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 49. Why are Health Checks (HC) important? Some reasons to perform health-checks around a SQL statement  A HC may actually point to the root cause of a “bad” plan  May alert of a bigger problem than one SQL  Easier to visualize trends like poor CBO statistics  Reduce re-discovery of known issues  Highlight SQL tuning opportunities  Think of a HC as “your buddy” that gives you some heads-ups49 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 50. Health Check (HC) Types Groups of HC performed by SQLT  Global  EBS  Siebel  Tables – Partitions  Indexes – Partitions  Columns50 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 51. Health Checks Sample51 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 52. Do I have to act on every Observation? The output of a health check become an observation  Not all observations are equally relevant  Review them all and build hypotheses on relevant ones  Experience will help you to identify relevant observations  Look for trends specially among several SQL statements52 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 53. Health-checks without installing SQLT? SQL Health-Check (SQLHC) MOS 1366133.1  SQLHC generates a subset of SQLT health-checks (HC)  SQLHC does not install anything on the database  Execute SQLHC against a SQL_ID  Also available at SQLT under sqlt/utl/sqlhc.sql  Undocumented companion script: sqlt/utl/sqlhcxec.sql  SQLHC is not a replacement for SQLT53 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 54. SQL Test Case (TC)Introduction54 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 55. Why a SQL Test Case (TC)? A SQL Test Case is needed to try hypotheses in two scenarios  Apply SQL Tuning WHAT-IF scenarios – Usually not feasible to test in Source system – No data is needed in most cases  Investigate “unexpected results” – This type of analysis usually requires a subset of data55 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 56. SQL Tuning Test Case Objectives 1. Reproduce an explain plan from a source system – Source and target systems are usually different 2. Test designed WHAT-IF scenarios – Designed during analysis56 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 57. SQL Tuning Test Case Phases 1. Extraction a) SQLT main methods always produce a SQLT TC b) SQLT XTRACT and XECUTE produce a TCB for 11g 2. Implementation a) Follow “Implement SQLT Test Case (TC)” section from sqlt_sNNNNN_readme.html b) Follow sqlt_sNNNNN_tcb_README.txt under sqlt_sNNNNN_tcb.zip57 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 58. Implement SQLT Test Case (TC) From sqlt_sNNNNN_readme.html58 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 59. Summary59 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 60. SQL Tuning Made Easier with SQLTXPLAIN How is that SQLT makes SQL Tuning easier?  Comprehensive collection of diagnostics information – Consolidated into an easy to navigate html report  Over a hundred health-checks around the SQL statement – Some with pointers to particular notes or bugs  Automatic SQL Tuning TC extraction – Allow WHAT-IF evaluations on a test environment  SQL Tuning Advisor report if licensed  SQL commands for statistics generation and plan stability60 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 61. My Oracle Support (MOS) Documents Notes Reference  215187.1 SQLTXPLAIN (SQLT) tool  1454160.1 SQLTXPLAIN (SQLT) FAQ  1366133.1 SQL Health-Check (SQLHC) script  1417774.1 SQLHC FAQ  1455583.1 SQLHC Video61 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 62. Oracle SQL Tuning with Oracle SQLTXPLAIN Stelios Charalambides (2013)  To be published soon62 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 63. Oracle SQL Tuning with Oracle SQLTXPLAIN Stelios Charalambides (2013)  Introduction to SQLT  Examining the Cost-based Optimizer Environment  Gathering Object Statistics  Compensating for Skewed Data  Troubleshooting Query Transformations  Forcing Execution Plans through Profiles  Adaptive Cursor Sharing63 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 64. Oracle SQL Tuning with Oracle SQLTXPLAIN Stelios Charalambides (2013)  Validating Cardinality (Cardinality Feedback)  Tuning in SIEBEL and PeopleSoft Environments  Comparing Execution Plans (COMPARE method)  Exploring Possible Parameter Changes (XPLORE method)  Applying Advanced SQLT Methods  Building Good Test Cases (using Test Case builder)  The Final Word64 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 65. Eager to learn more on SQL Tuning? Some recognized professionals in the field of SQL Tuning  Maria Colgan  Jonathan Lewis  Cary Millsap  Christian Antognini  Guy Harrison65 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 66. Q&A carlos.sierra@oracle.com http://carlos-sierra.net66 East Coast Oracle Users Conference. October 17 & 18, 2012.
  • 67. 67 East Coast Oracle Users Conference. October 17 & 18, 2012.