Wednesday, August 21, 2013

SQL Query to find top SQL's with two specific AWR Snapshots

SQL:

WITH subq as
(SELECT *
    FROM (SELECT s.sql_id,
                                  SUM(nvl(s.executions_delta, 0)) execs,
                                  SUM(round(s.elapsed_time_delta / 1000000, 3)) exec_time,
                                  SUM(round(s.buffer_gets_delta)) io_logical,
                                  SUM(round(s.cpu_time_delta / 1000000, 3)) cpu_time,
                                  SUM(nvl(s.parse_calls_delta, 0)) parse_calls,
                                  SUM(round(s.iowait_delta / 1000000, 3)) io_wait,
                                  SUM(nvl(s.disk_reads_delta, 0)) disk_reads
                     FROM dba_hist_sqlstat s, dba_hist_snapshot ss
                  WHERE s.snap_id BETWEEN start_snap_id AND end_snap_id
                        AND ss.snap_id = s.snap_id
                        AND ss.instance_number = s.instance_number
                        AND s.executions_delta > 0
                 GROUP BY s.sql_id
                 ORDER BY exec_time DESC)
  WHERE rownum < num_of_sqls)
SELECT subq.*, substr(sql_text, 1, 8000) sqltext
    FROM dba_hist_sqltext st, subq
 WHERE st.sql_id=subq.sql_id
 ORDER BY 3 desc

Things to replace in above sql before executing:

start_snap_id: replace this with starting AWR snapshot number of your database from your peak day snapshots for the period which you want collect the top SQL's. For example: Starting snapshot of database TEST on wednesday at 9AM is 12345.

end_snap_id: replace this with ending AWR snapshot number of your database from your peak day snapshots for the period which you want collect the top SQL's. For example: Ending snapshot of database TEST on wednesday at 9AM is 12355.

num_of_sqls: Give the number of how many top SQL's you want to collect using the query for example if you want to collect top 50 SQL's replace num_of_sqls with 50



No comments: