Using AWR for SQL Analysis

9,410 views

Published on

0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
9,410
On SlideShare
0
From Embeds
0
Number of Embeds
782
Actions
Shares
0
Downloads
513
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Using AWR for SQL Analysis

  1. 1. Michael R. Ault Oracle Guru Texas Memory Systems Using AWR For SQL Analysis
  2. 2. Michael R. Ault Oracle Guru <ul><li>Nuclear Navy 6 years </li></ul><ul><li>Nuclear Chemist/Programmer 10 years </li></ul><ul><li>Kennedy Western University Graduate </li></ul><ul><li>Bachelors Degree Computer Science </li></ul><ul><li>Certified in all Oracle Versions Since 6 </li></ul><ul><li>Oracle DBA, author, since 1990 </li></ul>
  3. 3. Books by Michael R. Ault
  4. 4. Introduction <ul><li>Many times a developer may be given the task of helping the DBA find and resolve an applications poorly performing code. </li></ul><ul><li>Of course first one must define what constitutes poor performance. </li></ul><ul><li>Is poor performance a certain number of logical or physical IO’s? Is it a certain number of consistent reads? </li></ul><ul><li>Ideally it is when all actions require the fewest resources to execute. </li></ul><ul><li>However, you must determine what is good enough performance </li></ul>
  5. 5. Introduction <ul><li>For example, in an order entry situation inserts should be expected to be sub-second. </li></ul><ul><li>Another example would be that in a customer service application the customer information screen should be expected to be populated within less than seven seconds. </li></ul><ul><li>Yet another would be that a decision support system needs to return results within 15 minutes. </li></ul><ul><li>Each of these applications has different expectations for performance and each must be tuned using that expectation set in mind. </li></ul>
  6. 6. Introduction <ul><li>Another key concept when tuning is the concept of enough is enough. </li></ul><ul><li>This concept means to set specific tuning goals and when you reach them, go on to the next problem. </li></ul><ul><li>Tuning Oracle has been likened to a video game with infinite levels, there is always a way to get a few more milliseconds or microseconds of performance from Oracle, you have to know when to quit! </li></ul><ul><li>In this presentation we will look at using the statspack, and by extension the AWR, tool for finding and correcting bad SQL in an application. </li></ul>
  7. 7. What We Will Discuss <ul><ul><li>Using Statspack and AWR to Track Code </li></ul></ul><ul><ul><li>Sorts </li></ul></ul><ul><ul><li>SQL Review </li></ul></ul><ul><ul><li>Commenting Code </li></ul></ul>
  8. 8. Why Should Developers Tune? <ul><ul><li>DBAs are responsible for tuning…right? </li></ul></ul><ul><ul><li>Wrong! Everyone is! </li></ul></ul><ul><ul><li>Oracle and third party tools make it easy. (Well, almost!) </li></ul></ul>
  9. 9. Why Everyone?
  10. 10. Available Tools <ul><li>Explain Plan </li></ul><ul><li>Trace and TKPROF </li></ul><ul><li>DBMS_PROFILER </li></ul><ul><li>Events </li></ul><ul><li>Statspack and AWR </li></ul><ul><li>Enterprise Manager </li></ul>
  11. 11. Let’s Look at Statspack <ul><li>Installation, from SYS user: </li></ul><ul><li>Make sure dbms_jobs and dbms_shared_pool are installed in the system. </li></ul><ul><li>Review the spcreate.sql series of called scripts and eliminate the calls to install the packages in step 1. </li></ul><ul><li>Create a perfstat tablespace </li></ul><ul><li>Run the spcreate.sql script, usually in the $ORACLE_HOME/rdbms/admin directory </li></ul><ul><li>Use the statspack.snap procedure to test the install </li></ul><ul><li>Start automated statistics runs with spauto.sql </li></ul>
  12. 12. AWR Usage <ul><li>In 10g we now have AWR. </li></ul><ul><li>The Automatic Workload Repository (AWR) defaults to a collection interval every 30 minutes </li></ul><ul><li>AWR is like STATSPACK, especially the level-5 STATSPACK collection mechanism where top SQL is collected every hour. </li></ul><ul><li>In addition to the SQL, AWR collects detailed run-time statistics on the top SQL (disk reads, executions, consistent gets, etc) and uses this information to adjust the rolling collection threshold. </li></ul><ul><li>AWR takes advantage of new Oracle10 and 11 to gather its statistics using non-SQL based collection techniques. </li></ul><ul><li>The awrrpt.sql script is used to generate the reports that take the place of the Statspack reports in 10/11g. </li></ul>
  13. 13. Testing Using Statspack or AWR <ul><li>Statspack and AWR are simply statistics capture tools that also provide a report that shows you performance related statistics. </li></ul><ul><li>Part of the statistics are several listings of SQL statements sliced and diced by number of consistent gets, number of physical reads, number of parses, number of executions and number of versions. </li></ul><ul><li>Of course for a statement to appear there, it must have been run! </li></ul>
  14. 14. Testing SQL Using Statspack or AWR <ul><li>Statspack process will only capture what is currently going on in the database. </li></ul><ul><li>It takes snapshots of the current state and allows you to choose two snapshots to compare. </li></ul><ul><li>In the case of the SQL statements, if they are still in the shared pool between the two statspack runs, then your changes may be masked by old code runs. </li></ul><ul><li>For testing purposes you need to flush the shared pool, execute a snapshot, then test your changes and execute another snapshot </li></ul>
  15. 15. Testing Using Statspack or AWR <ul><li>Use the “ALTER SYSTEM FLUSH SHARED_POOL;” command to flush old SQL from the pool. </li></ul><ul><li>Execute the command “execute statspack.snap;” to begin a statspack capture window. </li></ul><ul><li>Run your test code </li></ul><ul><li>Execute the command “execute statspack.snap;” to end the statspack capture window. </li></ul><ul><li>Run the spreport.sql script to generate a report based on the time interval between steps 2 and 4. </li></ul>
  16. 16. Testing Using Statspack or AWR <ul><li>If you don’t already know what SQL is causing issues use the automated statspack gathering (spauto.sql) to capture a profile of statspack runs across the time periods when the users have the problems. </li></ul><ul><li>Running statspack off hours probably won’t tell you a lot, you need to run it when the problem is occurring! </li></ul><ul><li>AWR (unless you turn it off) runs continuously and retains 7 days of data by default </li></ul>
  17. 17. Evaluating Statspack or AWR for Code Issues <ul><li>A number of sections devoted entirely to code. </li></ul><ul><li>There are sections devoted to waits and one to latches. </li></ul><ul><li>Use a cross-mix between waits, latches and the reported SQL to isolate correct the problem SQL. </li></ul><ul><li>Start with the major hitters </li></ul><ul><li>If a SQL shows up in the top ten in both the consistent gets and disk reads area then it is a good candidate. </li></ul><ul><li>For this presentation we will be using example outputs from actual Statspack/AWR reports. </li></ul>
  18. 18. SQL Areas in AWR/Statspack Reports <ul><li>SQL ordered by CPU Time – Sorting, bad paths </li></ul><ul><li>SQL ordered by Gets – Excessive logical IO </li></ul><ul><li>SQL ordered by Reads – Cache starvation, FTS </li></ul><ul><li>SQL ordered by Parse Calls – Cursor sharing, cursor caching </li></ul><ul><li>SQL ordered by Version Count – Versioning is usually due to a bug, check with support </li></ul><ul><li>SQL Detailed Listing – Shows full code (may not be in all reports) </li></ul><ul><li>Tune SQL that appears in more than one of these areas </li></ul><ul><li>Tune SQL at the top of these sections </li></ul>
  19. 19. Evaluating Statspack or AWR for Code Issues <ul><li>Don’t tune one-offs, unless that is the code you are tuning. </li></ul><ul><li>Let’s look at some actual statspack/AWR outputs and see what we can determine from them about the code they contain. </li></ul>
  20. 20. Example Top 5 events <ul><li>Snap Id Snap Time Sessions Curs/Sess Comment </li></ul><ul><li>------- ------------------ -------- --------- ------------------- </li></ul><ul><li>Begin Snap: 1240 26-May-05 15:00:02 34 5.0 </li></ul><ul><li>End Snap: 1241 26-May-05 16:00:02 33 4.9 </li></ul><ul><li>Elapsed: 60.00 (mins) </li></ul><ul><li>Top 5 Timed Events </li></ul><ul><li>~~~~~~~~~~~~~~~~~~ % Total </li></ul><ul><li>Event Waits Time (s) Ela Time </li></ul><ul><li>-------------------------------------------- ------------ ----------- -------- </li></ul><ul><li>db file sequential read 320,448 4,588 32.25 </li></ul><ul><li>direct path read 58,652 2,988 21.00 </li></ul><ul><li>CPU time 2,182 15.34 </li></ul><ul><li>PX Deq: Execute Reply 1,428 1,257 8.83 </li></ul><ul><li>db file scattered read 11,406 1,020 7.17 </li></ul><ul><li>------------------------------------------------------------- </li></ul>
  21. 21. Example Statements <ul><li>Physical Reads Executions Reads per Exec %Total Time (s) Time (s) Hash Value </li></ul><ul><li>--------------- ------------ -------------- ------ -------- --------- ---------- </li></ul><ul><li>90,051 20,322 4.4 1.9 90.85 3506.03 2624188903 </li></ul><ul><li>Module: SQL*Plus </li></ul><ul><li>SELECT /*+ INDEX(p1 iu_pol_isc) */ t1.pol_tran_eff_dt </li></ul><ul><li>FROM pol p1, pol_tran t1 WHERE p1.p </li></ul><ul><li>ol_nbr = :b2 AND p1.ign_sys_cd = </li></ul><ul><li>:b1 AND t1.pol_id = p1.pol_id A </li></ul><ul><li>ND t1.pol_tran_typ_cd = 'CN' AND </li></ul><ul><li>1,466 1 1,466.0 0.0 10.10 13.08 3656618493 </li></ul><ul><li>Module: SQLNav5.exe </li></ul><ul><li>Select /*+ FIRST_ROWS */ Key||','||WRTN_PREM||','||WRTN_EXPSR|| </li></ul><ul><li>','||EARNED From ( Select /*+ INDEX(pcoopt POL_COVG_ON_OFF_PRE </li></ul><ul><li>M_TRAN_IDX1) */ --/*+ INDEX(pcoopt POL_COVG_ON_OFF_PREM_TRAN_I </li></ul><ul><li>DX1) */ --- /*+ INDEX(pcoopt PK_POL_COVG_ON_OFF_PREM_TRAN) */ </li></ul><ul><li>pcoopt.pol_id||','||pcoopt.pol_tran_id||','||pcoop </li></ul><ul><li>852 1 852.0 0.0 11.15 12.26 656581566 </li></ul><ul><li>Module: SQLNav5.exe </li></ul><ul><li>Select /*+ FIRST_ROWS */ Key||','||WRTN_PREM||','||WRTN_EXPSR|| </li></ul><ul><li>','||EARNED From ( Select /*+ FIRST_ROWS */ --/*+ INDEX(pcoo </li></ul><ul><li>pt POL_COVG_ON_OFF_PREM_TRAN_IDX1) */ --- /*+ INDEX(pcoopt PK_ </li></ul><ul><li>POL_COVG_ON_OFF_PREM_TRAN) */ pcoopt.pol_id||','|| </li></ul><ul><li>pcoopt.pol_tran_id||','||pcoopt.veh_unit_nbr||','|| </li></ul>
  22. 22. Analysis <ul><li>The three SQL statements are application SQL statements, the other of the top 10 were all from monitoring tools. </li></ul><ul><li>Of the three, two are limited to only a single execution however we have one SQL that was executed a whopping 20,322 times. </li></ul><ul><li>Even though it only does 4.4 reads per execution, it does a great number of these. By tuning this SQL we can reduce the physical IO requirements of the application. </li></ul><ul><li>That we are getting lots of small reads (4.4 IOs per execution) indicates that this may be the source of many of our sequential read waits reported in this period. </li></ul><ul><li>Generally sequential read waits can be mitigated by more cache memory, we may not need to tune this query. </li></ul>
  23. 23. Analysis <ul><li>Direct path reads are due to sorts and some full scanning. </li></ul><ul><li>We will look at sorting later </li></ul>
  24. 24. Analysis <ul><li>The scattered read waits are probably due to the monitoring SQL being generated by the monitoring reads. </li></ul><ul><li>The problem here may also be memory related as the memory may not be large enough to hold the data needed by the application. </li></ul>
  25. 25. Analysis - Repeated Statements <ul><li>Physical Reads Executions Reads per Exec %Total Time (s) Time (s) Hash Value </li></ul><ul><li>--------------- ------------ -------------- ------ -------- --------- ---------- </li></ul><ul><li>90,051 20,322 4.4 1.9 90.85 3506.03 2624188903 </li></ul><ul><li>Module: SQL*Plus </li></ul><ul><li>SELECT /*+ INDEX(p1 iu_pol_isc) */ t1.pol_tran_eff_dt </li></ul><ul><li>FROM pol p1, pol_tran t1 WHERE p1.p </li></ul><ul><li>ol_nbr = :b2 AND p1.ign_sys_cd = </li></ul><ul><li>:b1 AND t1.pol_id = p1.pol_id A </li></ul><ul><li>ND t1.pol_tran_typ_cd = 'CN' AND </li></ul><ul><li>Buffer Gets Executions Gets per Exec %Total Time (s) Time (s) Hash Value </li></ul><ul><li>--------------- ------------ -------------- ------ -------- --------- ---------- </li></ul><ul><li>796,391 20,322 39.2 2.0 90.85 3506.03 2624188903 </li></ul><ul><li>Module: SQL*Plus </li></ul><ul><li>SELECT /*+ INDEX(p1 iu_pol_isc) */ t1.pol_tran_eff_dt </li></ul><ul><li>FROM pol p1, pol_tran t1 WHERE p1.p </li></ul><ul><li>ol_nbr = :b2 AND p1.ign_sys_cd = </li></ul><ul><li>:b1 AND t1.pol_id = p1.pol_id A </li></ul><ul><li>ND t1.pol_tran_typ_cd = 'CN' AND </li></ul>
  26. 26. Analysis <ul><li>We know it is the same code, even though we can’t see all of it by the hash value: 2624188903 being identical. </li></ul><ul><li>This is the SQL to analyse. </li></ul><ul><li>We can obtain the entire SQL statement by extracting it from the V$SQLTEXT view using the hash value provided or with AWR it is provided in the report. </li></ul><ul><li>Using this hash code, we can also (if we are in 9i or greater) extract the explain plan for the code from the V$SQL_PLAN table as well. </li></ul><ul><li>Using the DBMS_XPLAN package makes getting explain plans a snap. </li></ul>
  27. 27. Recursive SQL <ul><li>Occurs whenever a SQL statement is parsed </li></ul><ul><li>Is SQL executed on behalf of a client </li></ul><ul><li>SQL against underlying data dictionary tables </li></ul><ul><li>Determines table, index, column existence </li></ul><ul><li>Determines permissions and grants </li></ul><ul><li>Determines settings (initialization parameters and undoc settings) </li></ul>
  28. 28. Recursive SQL <ul><li> Snap Id Snap Time Sessions Curs/Sess Comment </li></ul><ul><li> ------- ------------------ -------- --------- ------------------- </li></ul><ul><li>Begin Snap: 12 07-Jun-04 17:53:55 117 8.2 </li></ul><ul><li>End Snap: 13 07-Jun-04 18:03:18 107 7.4 </li></ul><ul><li>Elapsed: 9.38(mins) </li></ul><ul><li>Instance Efficiency Percentages (Target 100%) </li></ul><ul><li>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ </li></ul><ul><li> Buffer Nowait %: 100.00 Redo NoWait %: 99.98 </li></ul><ul><li> Buffer Hit %: 98.55 In-memory Sort %: 100.00 </li></ul><ul><li> Library Hit %: 99.51 Soft Parse %: 98.80 </li></ul><ul><li> Execute to Parse %: 63.14 Latch Hit %: 99.90 </li></ul><ul><li>Parse CPU to Parse Elapsd %: 68.58 % Non-Parse CPU: 99.45 </li></ul><ul><li>Top 5 Timed Events </li></ul><ul><li>~~~~~~~~~~~~~~~~~~ % Total </li></ul><ul><li>Event Waits Time (s) Ela Time </li></ul><ul><li>------------------------------------ ------------ --------- -------- </li></ul><ul><li>CPU time 1,570 75.13 </li></ul><ul><li>latch free 13,348 193 9.21 </li></ul><ul><li>SQL*Net more data to client 327,015 147 7.03 </li></ul><ul><li>log file sync 3,263 91 4.34 </li></ul><ul><li>db file scattered read 191,897 44 2.13 </li></ul><ul><li> ------------------------------------------------------------- </li></ul>
  29. 29. Recursive SQL <ul><li>We see: </li></ul><ul><li>lots of CPU time being used </li></ul><ul><li>lots of buffer gets and lots of physical reads </li></ul><ul><li>Our key waits are for SQL area (latch free) events. </li></ul><ul><li>We can see that the various parse related ratios are very much less than 100% </li></ul><ul><li>Soft parse is also an indicator </li></ul><ul><li>This would indicate re-parsing (hard parsing) was occurring. </li></ul><ul><li>Reparsing is generally caused by lack of bind variables. </li></ul><ul><li>In some 10/11 releases versioning can also be an issue </li></ul><ul><li>Fix versioning by using CURSOR_SHARING=FORCE or setting _sqlexec_progression_cost high </li></ul>
  30. 30. If we look at the Report <ul><li>We see a number of SQL statements that are not using bind variables and some that use a mix of literal and bind variables. </li></ul><ul><li>In this situation we can apply the comparisons we used for the buffer reads and the physical reads as well as non-use of bind variables to find and repair the problem SQL statements. </li></ul><ul><li>If you are unable to correct the non-use of bind variables, using the CURSOR_SHARING initialization parameter will help with the parse situation. </li></ul>
  31. 31. Sorts <ul><li>SQL doing sorts will effect performance </li></ul><ul><li>Un-needed sorts can be caused by: </li></ul><ul><ul><li>DISTINCT </li></ul></ul><ul><ul><li>ORDER BY </li></ul></ul><ul><ul><li>GROUP BY </li></ul></ul><ul><ul><li>CARTESIAN JOIN </li></ul></ul><ul><li>Monitor using Sort statistics </li></ul>
  32. 32. Sort Statistics Statistic Total per Second per Trans -------------------------------- ------------------ -------------- ------------- sorts (disk) 3,529 0.2 0.0 sorts (memory) 9,012,270 417.6 6.0 sorts (rows) 110,063,794,220 5,099,850.2 73,302.3 workarea executions - multipass 0 0.0 0.0 workarea executions - onepass 5,293 0.3 0.0 workarea executions - optimal 7,113,060 329.6 4.7 Tablespace IO Stats DB/Inst: CC1/cc1 Snaps: 84084-84108 -> ordered by IOs (Reads + Writes) desc Tablespace ------------------------------ Av Av Av Av Buffer Av Buf Reads Reads/s Rd(ms) Blks/Rd Writes Writes/s Waits Wt(ms) -------------- ------- ------ ------- ------------ -------- ---------- ------ TEMP 11,484,000 532 16.3 4.1 3,478,365 161 12,266 2.0 REPMAN_TEMP 1,703,767 79 27.2 8.2 1,457,241 68 0 0.0
  33. 33. Sort Statistics PGA Aggr Target Histogram DB/Inst: test/test Snaps: 84-108 -> Optimal Executions are purely in-memory operations Low High Optimal Optimal Total Execs Optimal Execs 1-Pass Execs M-Pass Execs ------- ------- -------------- -------------- ------------ ------------ 2K 4K 6,308,435 6,308,435 0 0 512K 1024K 353,344 353,344 0 0 1M 2M 201,558 201,558 0 0 2M 4M 22,468 22,468 0 0 4M 8M 21,796 21,725 71 0 8M 16M 28,892 28,714 178 0 16M 32M 30,478 30,346 132 0 32M 64M 19,898 18,690 1,208 0 64M 128M 9,080 7,284 1,796 0 128M 256M 1,682 732 950 0 256M 512M 734 179 555 0 512M 1024M 329 58 271 0 1G 2G 131 14 117 0 2G 4G 17 4 13 0 -------------------------------------------------------------
  34. 34. What SQL Areas Are SORTS Effecting? <ul><li>Logical IO </li></ul><ul><li>Physical IO </li></ul><ul><li>Longest Elapsed Time </li></ul>If you see the same SQL statement in several of the above areas, it is a candidate for reviewing sort activity I wish they would add an area showing SQL that are doing FTS and one with SQL that has IO to the temporary tablespace In AWR reports the Segment Statistics reports also help. Look for objects in the Direct IO report section and review SQL that address them
  35. 35. SQL Structure Issues <ul><li>Review the detailed code listings for: </li></ul><ul><li>Insufficient joins: you need N-1 where N is number of tables </li></ul><ul><li>Excessive tables in join: _MAX_OPTIMIZER_PERMUTATIONS is set to 2000, this means a maximum of 6 tables can be utilized optimally </li></ul><ul><li>Improper use of hints (re-evaluate hints at every upgrade) </li></ul><ul><li>Improper use of DISTINCT, may show lazy programmer syndrome (LPS) </li></ul><ul><li>Proper use of bind variables </li></ul>
  36. 36. Identify SQL using Comments <ul><li>Placing a comment in each SQL statement that identifies the SQL. An example of this is: </li></ul><ul><li>CURSOR get_latch IS </li></ul><ul><li>SELECT /* DBA_UTIL.get_latch */ </li></ul><ul><li>a.name,100.*b.sleeps/b.gets </li></ul><ul><li>FROM </li></ul><ul><li>v$latchname a, v$latch b </li></ul><ul><li>WHERE </li></ul><ul><li>a.latch# = b.latch# and b.sleeps > 0; </li></ul><ul><li>You simply query the V$SQLAREA or V$SQLTEXT, or the V$SQL_PLAN VPT to find code entries with '%DBA_UTIL%' in the SQL_TEXT column. In addition, in any Statspack output, the code identifies itself. </li></ul>
  37. 37. Conclusion <ul><li>In this presentation I have tried to convey the importance of using statspack to help find and isolate SQL statements that need tuning. </li></ul><ul><li>We have covered the installation and use of Statspack and have discussed AWR and its use along with statspack. </li></ul><ul><li>DBAs and Developers should utilize statspack or AWR for point monitoring of development environments and for continued monitoring of production environments. </li></ul>
  38. 38. Questions? Mike Ault [email_address]

×