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

 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
 Oracle Support

 SQL Tuning

 Oracle UNIX
 Oracle Linux
 Remote s
 Remote plans
 Application Server

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

 Remote S


 Consulting Staff
 Consulting Prices
 Help Wanted!


 Oracle Posters
 Oracle Books

 Oracle Scripts

Don Burleson Blog 







Oracle SQL top sessions

Oracle Tips by Burleson Consulting

This is an excerpt from my latest book "Oracle Tuning: The Definitive Reference", and you can download the working scripts from the code depot. 

Identifying Problem SQL

One short path to identifying performance problems in an Oracle database includes the following steps:

§       Find the sessions responsible for hogging the most resources (I/O, CPU, etc.).

§       Identify the code these sessions are running.

§       Peel away the bad code these sessions have executed from the good and acceptable code.

§       Highlight the worst SQL and then work to tune it for better performance.

This process has been made much easier in Oracle9i and Oracle10g, especially with respect to identifying problem SQL that gets run in a production database. The following sections work through these four steps and see how several performance views can assist in this process.

Find the Problem Sessions

Even if there is no database monitor that offers a top sessions view, it is easy to pinpoint the sessions that are giving the database grief. Different database professionals have their own ideas about what constitutes a top session. Some feel that the sum total of physical I/O alone tells the story, while others look at CPU, and still others use a combination of physical and logical I/O.


Whatever the preference, the script in top_20_sessions.sql can be used to quickly bubble to the top twenty sessions in an Oracle database. The initial sort is on physical I/O, but this can be changed to any other column desired.


§       top_20_sessions.sql



select * from

(select b.sid sid,

     decode (b.username,null,,b.username) user_name,

     d.spid os_id,

     b.machine machine_name,

     to_char(logon_time,'dd-mon-yy hh:mi:ss pm') logon_time,

    (sum(decode(,'physical reads  ',value,0)) +

     sum(decode(,'physical writes',value,0)) +

     sum(decode(,'physical writes direct',value,0)) +

     sum(decode(,'physical writes direct (lob)',value,0))+

     sum(decode(,'physical reads  direct (lob)',value,0)) +

     sum(decode(,'physical reads   direct',value,0)))


    (sum(decode(,'db block gets',value,0)) +

     sum(decode(,'db block changes',value,0)) +

     sum(decode(,'consistent changes',value,0)) +

     sum(decode(,'consistent gets ',value,0)) )


    (sum(decode(,'session pga memory',value,0))+

     sum(decode(,'session uga memory',value,0)) )


     sum(decode(,'parse count (total)',value,0)) parses,

     sum(decode(,'cpu used by this session',value,0))


     sum(decode(,'parse time cpu',value,0)) parse_cpu,

     sum(decode(,'recursive cpu usage',value,0))


     sum(decode(,'cpu used by this session',value,0)) -

     sum(decode(,'parse time cpu',value,0)) -

     sum(decode(,'recursive cpu usage',value,0))


     sum(decode(,'sorts (disk)',value,0)) disk_sorts,

     sum(decode(,'sorts (memory)',value,0)) memory_sorts,

     sum(decode(,'sorts (rows)',value,0)) rows_sorted,

     sum(decode(,'user commits',value,0)) commits,

     sum(decode(,'user rollbacks',value,0)) rollbacks,

     sum(decode(,'execute count',value,0)) executions

from sys.v_$sesstat  a,

     sys.v_$session b,

     sys.v_$statname c,

     sys.v_$process d,

     sys.v_$bgprocess e

where a.statistic#=c.statistic# and


      c.NAME in ('physical reads  ',

                 'physical writes',

                 'physical writes direct',

                 'physical reads   direct',

                 'physical writes direct (lob)',

                 'physical reads   direct (lob)',

                 'db block gets',

                 'db block changes',

                 'consistent changes',

                 'consistent gets ',

                 'session pga memory',

                 'session uga memory',

                 'parse count (total)',

                 'CPU used by this session',

                 'parse time cpu',

                 'recursive cpu usage',

                 'sorts (disk)',

                 'sorts (memory)',

                 'sorts (rows)',

                 'user commits',

                 'user rollbacks',

                 'execute count'


group by b.sid,


         decode (b.username,null,,b.username),


         to_char(logon_time,'dd-mon-yy hh:mi:ss pm')

order by 6 desc)

where rownum < 21




The above query can also be modified to exclude Oracle background processes, the SYS and SYSTEM user, etc. The end result should be a current list of top offending sessions in the database as ranked by various performance metrics, which is the normal way to rank problem user accounts. Figure 15.15 shows a sample output of this query:


Figure 15.15: The Ion tool sample top_20_sessions.sql query output


Some DBAs feel that this method, while useful, lacks depth. Specifically, because DBAs know that a user’s resource consumption is almost always tied to inefficient SQL, they would like to cut to the chase and find the problem sessions in a database that have, for example, caused most of the large table scans on the system or have submitted queries containing Cartesian joins.


Such a thing was difficult to determine in earlier versions of Oracle, but fortunately, 9i provides a new performance view that can be used to derive such data. The v$sql_plan  view contains execution plan data for all submitted SQL statements. Such a view provides a wealth of information regarding the performance and efficiency of SQL statements and the sessions that submitted them.


This is an excerpt from my latest book "Oracle Tuning: The Definitive Reference". 

You can buy it direct from the publisher for 30%-off and get instant access to the code depot of Oracle tuning scripts:




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 -  2017

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.

Remote Emergency Support provided by Conversational