Get the latest!
You are here: Home / Tutorials / How long will my SQL query run
How long will my SQL query run

How long will my SQL query run

Have you ever started a query in your favourite Oracle SQL tool and it is just runnin’, and runnin’, and runnin’…? And, you are wondering if the query will let you eat lunch before it finishes…? Well, here is a way you might be able to find out how long your SQL query will take.


V$SESSION_LONGOPS

This view displays the status of various operations that run for longer than 6 seconds (in absolute time). These operations currently include many backup and recovery functions, statistics gathering, and query execution, and more operations are added for every Oracle release. To monitor query execution progress, you must be using the cost-based optimizer and you must:

  • Set the TIMED_STATISTICS or SQL_TRACE parameter to true
  • Gather statistics for your objects with the ANALYZE statement or the DBMS_STATS package

You can add information to this view about application-specific long-running operations by using the DBMS_APPLICATION_INFO.SET_SESSION_LONGOPS procedure. ~ From Oracle® Database Reference, 10gR2 (For full information, look here)

Example of v$session_longops usage

select *
from (
            select  opname
                    ,target
                    ,round(((elapsed_seconds * (totalwork - sofar)) / sofar), 2) time_remaining
                    ,sofar
                    ,totalwork
                    ,units
                    ,elapsed_seconds
                    ,message
                    ,a.start_time
                    ,b.sql_text
            from    v$sql b
                    ,v$session_longops a
            where b.sql_id (+) = a.sql_id
            and round(((elapsed_seconds * (totalwork - sofar)) / sofar), 2) > 0
)
order by time_remaining desc
;

Note that your ability to use the v$ views in Oracle is based on the rights you are set up with in the database. If you do not know, please get with your DBA. If you are doing this in your personal / private database, look on the internet to find out how to set the correct rights.

Hope this helps.

If you have a different way you like to do this, please let us all know so we all can grow in experience.

 

About TJ Abrahamsen

TJ Abrahamsen is the founder and author of OracleTuts. He has worked with Oracle since 1997. His expertise is with Oracle SQL and PL/SQL. He loves teaching others, and like to share his thoughts, ideas, and experience on his blog.
  • sergee

    What’s about hundreds of milions operations (call them Nested Loop) which take less that 6 second ?

    • sergee

      Also How long will it run if it have not started yet (just disigned) ? I.e. How to estimate time without starting SQL query?In yours exapml we just time of carrent operation (full table scan, hash join, sort etc), but if there are, say, 5 operation and now just first of them is executing, so we still don’t now about other 4 operation. How can you say how long will it run ?

      • http://oracletuts.net TJ Abrahamsen

        Hello sergee -

        I am not sure if my title on this post might be a bit misleading, but I am speaking about situations where you have started to run a large query, and it just goes on, and on, and on.

        In many of these cases you can use the v$session_longops view to see what is going on, and it gives you an estimate of how long time is left of the already running query.

        v$session_longops could not be used to estimate time your query will run before it is run…

        Sorry about the confusion.

        You might be able to run an explain plan. Please see Tom Kyte’s posts using this link: http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:2945229900346583015

        Hope this helps.

        ~ TJ

        • Sergee

          Hello TJ Abrahamsen

          Thanks for replay.
          And yes, you are right – v$session_longops is only one easy way to estimate time to complete current operation (either FTS, HJ etc).
          But i just want to note following points :
          1. There are not appear any nested loop operation independent of how long it takes, because each NL iteration very quick, but if number of this iterration is huge it take a lot of time together.
          exmple : 1 iteration takes 0.001 sec, count of iteration is 1 000 000 then time is 0.001*1 000 000= 1 000 sec – this is significantly more then 6 sec.
          2. This view dispaly only current operation .
          exmple : You have explain plan FTS + FTS -> HJ -> FTS -> HJ -> FTS -> HJ ….
          then at the begin you can see just estimation time for current FTS and, while the next step HJ be started, you can’t know about estimation for the latest one.
          3. In last year I started use view gv$session_longops – this one is more universal.
          In RAC env it shows details for all instances, in singl env it shows the same details as v$session_longops.

          Anyway thanks for post.
          I hope it helps somebody.

Scroll To Top