Top  Expensive SQLs

I have following query to identify top expensive sql statements.
SELECT Substr(a.sql_text,1,50) sql_text,
Trunc(a.disk_reads/Decode(a.executions,0,1,a.executions)) reads_per_execution,
a.buffer_gets,
a.disk_reads,
a.executions,
a.sorts,
a.address
FROM v$sqlarea a
ORDER BY 2 DESC
However, I have a need to fetch corresponding session information that are running these queries. Is there a way I can connect v$sqlarea with v$session?
Thanks,
R

You should use hash_value and sql_address as there can be more sqls with same hash value but different address.
This might give you some idea for improvement:
-- sessions and other useful stuff                                                             
SELECT   s.SID SID, s.serial# serial, s.status status,
         (i.block_gets + i.consistent_gets) logical_reads,
         i.consistent_gets consistent_gets, i.block_gets block_gets,
         i.physical_reads physical_reads,
         (i.block_changes + i.consistent_changes) block_changes,
         NVL (DECODE (TYPE,
                      'BACKGROUND', 'SYS (' || b.NAME || ')',
                      s.username
              SUBSTR (p.program, INSTR (p.program, '('))
             ) oracle_user,
         s.process client_pid, s.machine machine, p.spid server_pid,
         NVL (s.osuser, '(' || b.NAME || ')') os_user,
         s.program client_program, s.logon_time logon_time,
         s.username user_name, s.module module,
         q.sql_text,
         s.sql_address sql_address, s.sql_hash_value sql_hash_value
    FROM v$session s,
         v$process p,
         v$sess_io i,
         v$bgprocess b,
         v$sqlarea q
   WHERE p.addr = s.paddr
     AND i.SID = s.SID
     AND p.addr = b.paddr(+)
     AND q.address(+) = s.sql_address
     AND q.hash_value(+) = s.sql_hash_value
     AND TYPE != 'BACKGROUND'
ORDER BY user_nameThis will work also on older versions of the database <= 9i.
HTH, Joze
Co-author of the forthcoming book "Expert Oracle Practices"
http://www.apress.com/book/view/9781430226680
Oracle related blog: http://joze-senegacnik.blogspot.com/
Blog about flying: http://jsenegacnik.blogspot.com/
Blog about Building Ovens, Baking and Cooking: http://senegacnik.blogspot.com
Edited by: Joze Senegacnik on 10.12.2009 0:27

Similar Messages

  • Top expensive SQL

    Hello,
    I have this query to derive top expensive SQLs. But I also need to derive % of shared pool area consumed by each statement. How can I get this information?
    SELECT *
    FROM (SELECT Substr(a.sql_text,1,50) sql_text,
    Trunc(a.disk_reads/Decode(a.executions,0,1,a.executions)) reads_per_execution,
    a.buffer_gets,
    a.disk_reads,
    a.executions,
    a.sorts,
    a.address
    FROM v$sqlarea a
    ORDER BY 2 DESC)
    WHERE rownum <= 10
    Thanks,
    R

    Okay, I think this is how it is going to work. Please let me know what you think.
    1) Get SGA allocated for shared pool and free % from following query
    SELECT f.pool, f.NAME, s.sgasize, f.BYTES,
    ROUND (f.BYTES / s.sgasize * 100, 2) freeb
    FROM (SELECT SUM (BYTES) sgasize, pool
    FROM v$sgastat
    GROUP BY pool) s,
    v$sgastat f
    WHERE f.NAME = 'free memory' AND f.pool = s.pool;
    2) Get shareable_memory for each SELECT AND match hash value with the top exepnsive sql query I posted in Original post.
    select * from stats$sql_summary
    3) Compute % consumed by each sql
    shareable_meory * 100/sga allocated value for shared pool
    Thanks,
    R

  • How to find top 10  SQL statments which are consuming more cpu time.

    hi all,
    Is there any command or script to monitor the top 10 sql statments which are consuming more cpu time.
    I know by using AWR REPORT we can find it, i want the command or script to find the top cpu utilization sql statments.
    Regards
    Subhash.

    Subhash,
    A quick and dirty Google search could have get you started with the following:
    Thread: how to get top CPU consuming sql oracle 10g
    Re: how to get top CPU consuming sql oracle 10g
    Oracle SQL top sessions
    http://www.dba-oracle.com/oracle10g_tuning/t_sql_top_sessions.htm
    "How to Find top 10 expensive sql's", version 9.2.0
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:73325450402303
    HTH,
    Thierry

  • How to generate Top 10 SQL's in the grid report.

    Hello,
    can anyone give a sample query to be used against to get the information of the top 10 SQL's from the database during the time frame we provide to the report during run time.
    Thanks for your time in advance,
    -Vj

    Subhash,
    A quick and dirty Google search could have get you started with the following:
    Thread: how to get top CPU consuming sql oracle 10g
    Re: how to get top CPU consuming sql oracle 10g
    Oracle SQL top sessions
    http://www.dba-oracle.com/oracle10g_tuning/t_sql_top_sessions.htm
    "How to Find top 10 expensive sql's", version 9.2.0
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:73325450402303
    HTH,
    Thierry

  • Application Tuning to find the most expensive sqls

    I have a schema in oracel 9i Release 2 and I want to do a performance testing from application side. Application is developed in Java . There are many queries in java side and some stored procedures are called while running the application.
    So I need to find the most expensive sqls by running the application and tracing the top sqls
    If I set trace enable at database level the trace files will be generated for all the connected sessions right? I want the trace file for only one schema. If I enable trace for a session I will not be able to trap the sqls since the application runs from another session . connection pooling also is used. suggest some good approaches to capture the most expensive sqls?

    Does the answers in your application tuning not suitable for you ?
    Nicolas.

  • Expensive SQL Statements of the day?

    Hi...
    How can i find out Expensive SQL Statements of the day please.?
    Our SAP Version is ECC 6.0 on Oracle 10.2.0.4.0
    Rgds

    Hi Srinivas,
    On ECC 6.0, go to ST04 t-code --> performance --> SQL Cache --> You see SQL Statements where you need to sort by SQL Statements by Total Execution Time(ms).
    I have tried on one of my R/3 4.7EE system, ST04n --> Resource COnsumption --> Top SQL Statements --> you will see 50 Top most expensive SQL Statements interms of wait time.
    also Read: [Re: Diagnosis of the expensive ABAP programs having database time > 90%;
                      [Please Read before Posting in the Performance and Tuning Forum;
    Regards,
    Kanthi Kiran

  • Expensive SQL statements in GTS

    Hi,
    Recently EWA has been run for the GTS system.There are expensive SQL statements that were causing the Performance issue.It would be a great help if anyone can advise on how these expensive SQL statments can be fine tuned. As all these statements were in standard SAP programs is it that we had the only option to create secondary indices ?
    Moderator message - Please respect the 5,000 character maximum when posting. Post only the relevant portions of code
    Please see Please Read before Posting in the Performance and Tuning Forum before posting
    Any suggestion on performance improvement is appreciated.
    Thanks in advance!!
    Regards,
    Sharon.
    Edited by: Rob Burbank on Nov 15, 2010 9:08 AM

    please ask specific questions.
    Otherwise there are services by SAP, either training or support, but they are not for free.
    Siegfried

  • Expensive SQL statements in EWA

    Hi experts,
    how can I add expensive SQL statements to Early watch alert reports? In my IDES system this is generated automatically but not for different other systems. Can this be activated by specific settings?
    Thanks and best regards, Basti

    Hi bd,
    Please check these points:
    What database are you using?
    What is the version of the ST-SER in your solution manager?
    Is the service preparation finished in the managed system(run se38 -> RTCCTOOL to check)?
    Have you checked note 1564508?
    Besides that, do you see any issue in the SDCCN collection logs for the EWA?
    Regards,
    Daniel.

  • Urgt:How to identify TOP 5 SQL using more CPU time  without using statspack

    How to identify the TOP 5 SQL queries which are consuming more CPU time during the timespan of 24 hours for entire database. There are N number of users who have issued sql queries, out of which few users have disconnected and few user are still connected to the database(I need to consider all sessions). My database version is 9.0.1. I don't want to use statspack. Is there any way to identify TOP 5 SQL consuming more CPU time without affecting the performance of the database. Can any one help me on this? Its urgent!!. I don't want to use statspack becasue it degrades the performance and i cannot afford to run it for 24 hours.

    You don't run statspack for 24 hours, that would infact be pointless...
    Run statspack for say a 10 or 15 minute window during a particular "busy" period on your database. If you run it for longer then all the useful data will have been "averaged out". You want a snapshot in time, not an average over the day.

  • How to identify TOP 5 SQL consuming more CPU time without using statspack

    How to identify the TOP 5 SQL queries which are consuming more CPU time during the timespan of 24 hours for entire database. There are N number of users who have issued sql queries, out of which few users have disconnected and few user are still connected to the database(I need to consider all sessions). My database version is 9.0.1. I don't want to use statspack. Is there any way to identify TOP 5 SQL consuming more CPU time without affecting the performance of the database. Can any one help me on this? Its urgent!!.

    My database version is 9.0.1. I don't want
    to use statspack. Is there any way to identify TOP 5
    SQL consuming more CPU time without affecting the
    performance of the database. Can any one help me on
    this? Its urgent!!.I cant understand why you dont want to use statspack. As it is very easy to use and very quick to identify bottlenecks. Try using statspack. Its really wonderful.
    Particularly in your case statspack is the best.
    Regards,
    Sanju.

  • Top 10 SQL's  by Physical reads

    Dear experts,
    Oracle version - 11.1.0.7
    Do we have a way to pull top 10 SQL's (physical reads) for a 24 hour period based on AWR data or other V$ views.
    Thanks

    From DBA_HIST_SQLSTAT, all you can tell is who the parsing user was.
    Not the executing user.
    So for queries which are executed multiple times by multiple users, DBA_HIST_SQLSTAT is insufficient and there's no great solution.
    You could have a look in the underlying ASH data - V$ACTIVE_SESSION_HISTORY and DBA_HIST_ACTIVE_SESS_HISTORY which is respectively sampled data and a sample of a sample and it might give an indication of who is executing.
    Edited by: Dom Brooks on Jul 19, 2012 10:18 AM
    Part of sentence was missing

  • EarlyWatch - Expensive SQL Statements help

    Hi all,
    i need to analyze an EWA report and i don't know how exactly can i identify an "expensive SQL statement".
    The report says:
    During this session, the following expensive SQL statements were identified as causing a database load of at least 1%.
    The corresponding stored procedure names are referenced in the table below.
    And in the table with stored procedure names, a row looks like this:
    ID     Stored Procedure Name
    1     ##Y4DCWWES7EHY100000053960000254832042037
    What does it mean and how can i identify the name of program and the exact statement (probably select) that is causing this problem?
    Thank you,
    Ondrej

    Hi Ondrej,
    you're running on SQL Server. SAP creates stored procedures for each and every statement run against the database. The name you get seems to be a temporary stored procedure which (if I'm not wrong) is marked by the two # characters at the beginning. You may be still find it in the stored procedures of your system.
    Open the Enterprise Manager and open your database, then the stored procedure link (you should run the Enterprise Manager on the server or on a PC with enough RAM). The search for the procedure and double click on it. I think that should show you the SQL statement. There should also the report name is a comment.
    Regards
    Ralph

  • Expensive SQL statements & no proper indexing INCLUDE LPSSTU03-PSST FuncGrp

    Hi there forum
    During a recent upgrade we have implemented EhP4 with relevant Support Packs.
    Our Early Watch Report nows highlights INCLUDE LPSSTU03 in Function Module Group PSST as having expensive SQL statements along with improper indexing.
    Although I have seached through OSS and Forums I can find nothing of help.
    Is there an EXPERT out there who can help because SAP tells me that is the way it works!
    Thanks.
    Carl Cavendish-Davies

    Herewith the latest results from the Early Watch report on this issue:
    Object Type     Total Executions Disk Reads     Elapsed Time     Buffer Gets     Records Processed
    TABLE     242481         1873373     12035981     36237218     34390383
    SELECT
    "MANDT" , "OBJNR" , "STAT" , "CHGNR" , "USNAM" , "UDATE" , "UTIME" , "TCODE" , "CDTCODE" , "INACT" , "CHIND"
    FROM
    "JCDS"
    WHERE
    "MANDT" = :A0 AND "OBJNR" IN ( :A1 , :A2 , :A3 , :A4 , :A5 )&
    Execution Plan
    V$SQL_PLAN addr= C000000774FF4038 hash= 3408408745 sql_id 4w93fuv5khc59
    SELECT STATEMENT Estimated Costs= 6 Estimated Rows= 0 Optimizer: ALL_ROWS
    3 INLIST ITERATOR
    2 TABLE ACCESS BY INDEX ROWID JCDS
    Estimated Costs= 5 Estimated Rows= 28 Estim. Bytes: 1,848
    Estim. CPU-Costs = 41,426 Estim. IO-Costs = 5
    1 INDEX RANGE SCAN JCDS~0
    Estimated Costs= 1 Estimated Rows= 28 Access predicates:
    "MANDT"=:A0 AND (("OBJNR"=:A1 OR "OBJNR"=:A2 OR "OBJNR"=:A3 OR "OBJNR"=:A4
    OR "OBJNR"=:A5)) Search Columns: 2 Estim. CPU-Costs = 6,777 Estim. IO-Costs = 1
    Program Data:
    Origin of ABAP coding
    Program Name Line  Created By Last Changed By Last Changed On
    LPSSTU03      44     SAP          SAP               25.03.2003
    000044 SELECT * FROM JCDS
    000045 APPENDING CORRESPONDING FIELDS OF TABLE T_JCDS
    000046 FOR ALL ENTRIES IN R_OBJNR
    000047 WHERE OBJNR = R_OBJNR-OBJNR.

  • Expensive SQL Statements

    Hi all,
             Am a new bee to this group I'll be greateful if any one help me in understanding about the Expensive SQL Statements.why n where thy r used
    Thanks..
    Mohammed

    Hi shankaran,
    1. Is there any way in finding out the expensive sql statements been used in a program.
      There is no direct way to find out.
    2. Only when the program is run,  
       and thru basis tcodes/performance testing tcodss,
      can we know whether some sqls are very heavy !
    3. PERFORMANCE TUNING IS VERY SENSITIVE TOPIC
    4. There are some basic recommendatations.
       a) don't use select *, just use select fied1,field2
       b) Incrase the fileter criteria (ie. more where conditions to narrow down the search cretierai)
       c) Create secondar indexes on columns 
        of tables, which are frequently
       used in programs.
    regards,
    amit m.

  • Expensive SQL statements & no proper indexing INCLUDE LBSVAU14-BSVA FuncGrp

    Expensive SQL statements & no proper indexing INCLUDE LBSVAU14 - Function Group BSVA (Status management)
    Hi there Forum
    During a recent upgrade we have implemented EhP4 with relevant Support Packs.
    Our Early Watch Report nows highlights INCLUDE LBSVAU14 as having expensive SQL statements along with improper indexing.
    Although I have seached through OSS and Forums I can find nothing of help.
    Is there an EXPERT out there who can help because SAP tell me that is the way it works!
    Thanks.
    Carl Cavendish-Davies

    Herewith the latest results from the Early Watch report on this:
    Object Type     Total Executions         Disk Reads   Elapsed Time   Buffer Gets    Records Processed
    TABLE     457663                 3282704       21081305        28310151       20031340
    SELECT
    FROM
    "JEST"
    WHERE
    "MANDT" = :A0 AND "OBJNR" IN ( :A1 , :A2 , :A3 , :A4 , :A5 )&
    Execution Plan
    V$SQL_PLAN addr= C000000774FF4038 hash= 3408408745 sql_id 4w93fuv5khc59
    SELECT STATEMENT Estimated Costs= 3 Estimated Rows= 0 Optimizer: ALL_ROWS
    3 INLIST ITERATOR
    2 TABLE ACCESS BY INDEX ROWID JEST
    Estimated Costs= 3 Estimated Rows= 10 Estim. Bytes: 330
    Estim. CPU-Costs = 20,886 Estim. IO-Costs = 3
    1 INDEX RANGE SCAN JEST~0
    Estimated Costs= 1 Estimated Rows= 10 Access predicates:
    "MANDT"=:A0 AND (("OBJNR"=:A1 OR "OBJNR"=:A2 OR "OBJNR"=:A3 OR "OBJNR"=:A4
    OR "OBJNR"=:A5)) Search Columns: 2 Estim. CPU-Costs = 6,137 Estim. IO-Costs = 1
    Program Data:
    Origin of ABAP coding
    Program Name       Line     Created By     Last Changed By    Last Changed On
    LBSVAU14            110     SAP     SAP              08.04.2007
    000110 SELECT * FROM JEST CLIENT SPECIFIED
    000111 APPENDING TABLE JEST_BUF
    000112 FOR ALL ENTRIES IN OBJNR_TAB
    000113 WHERE MANDT EQ CLIENT
    000114 AND OBJNR = OBJNR_TAB-OBJNR.

Maybe you are looking for