Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 
 Home
 E-mail Us
 Oracle Articles
New Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

 Applications
 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB  

Don Burleson Blog 


 

 

 


 

 

 
 

Find historical SQL by date

Oracle Database Tips by Donald BurlesonMarch 27,  2015

Oracle's AWR and/or STATSPACK provides a historical repository for high-impact SQL statements in the dba_hist_sqlstat and stats$sqlstat tables, and you can query these tables to locate historical SQL associated with a Oracle performance problem.

 AWR uses a "Top N" method which defaults to collect the Top-30 SQL statements for each SQL category (statistics_level=typical).  If you set statistics_level = all, AWR will collect the top 100 SQL statements.

Also see my notes on

In STATSPACK, historical SQL by date is collected via threshold settings.

See these important notes on viewing SQL execution plans over time and analyzing historical SQL with AWR.

This article contains a great SQL statement to locate all SQL associated with a specific date.  This script can easily be modified to accept a date. It lists by sql_id the number of executions of each SQL statement before and after the specified date.

Once you have the sql_id, it is a simple matter to access dba_hist_sqltext by sql_id to get the full text. Getting this same information from STATSPACK is possible using the stats$sql_summary table:

Here is a handy SQL query that will display a summary of SQL execution history:

select * from
(
   select
      sql_id,
      sql_plan_hash_value,
      event,sql_exec_id,
      sql_exec_start,current_obj#,
      sql_plan_line_id,
      sql_plan_operation,
      sql_plan_options,
      SUM (delta_read_io_requests) lio_read ,
      SUM (delta_read_io_bytes) pio_read ,
      count(*) count_1
   from
      dba_hist_active_sess_history
   where
      sql_id='xxxx'
   and 
      snap_id >= :strt_snap
   and
      snap_id <= :end_snap
   group by
      sql_id,
      sql_plan_hash_value,
      event,sql_exec_id,
      sql_exec_start,
      current_obj#,
      sql_plan_line_id,
      sql_plan_operation,
      sql_plan_options
  )
  order by count_1 desc;

This script accepts three parameters, the SQL ID, the start and end snap numbers, and displays details about the historical SQL execution.

select
s.sql_id,
sum(case
when begin_interval_time = to_date('14-feb-2009 1100','dd-mon-yyyy hh24mi') then s.executions_total
else 0
end) sum_after,
(sum(case
when begin_interval_time >= to_date('14-feb-2009 1100','dd-mon-yyyy hh24mi') then s.executions_total
else 0
end) -
sum(case
when begin_interval_time < to_date('14-feb-2009 1100','dd-mon-yyyy hh24mi') then s.executions_total
else 0
end)) difference
from
   dba_hist_sqlstat s,

   dba_hist_snapshot sn
where
   sn.begin_interval_time between to_date('05-nov-2008 0001','dd-mon-yyyy hh24mi')
and
   to_date('05-nov-2008 2359','dd-mon-yyyy hh24mi')
and

   sn.snap_id=s.snap_id
group by
   s.sql_id

order by
   difference desc;

For full scripts to find historical SQL from inside Oracle, see the Oracle script collection, available for immediate download.


 

 

��  
 
 
Oracle Training at Sea
 
 
 
 
oracle dba poster
 

 
Follow us on Twitter 
 
Oracle performance tuning software 
 
Oracle Linux poster
 
 
 

 

Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  

and include the URL for the page.


                    









Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


 

Copyright © 1996 -  2020

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.