There are cases where we would like to find out as to how many rows are processed by a long running SQL query. I have experienced many times where a long running query is still executing and I wanted to know as to how much percentage is complete, to decide to terminate or let the query continue to drag to completion. There are few ways to determine the completion status or the number of rows processed by the long running queries.
In TOAD, open the session browser. (an icon on the top right corner with a lens symbol). Select the session that is executing the query. This can be identified using the userid and the tool from where the query is executed. Navigate to the longops tab and it will list the percentage completion of the query.
This approach need to be used when the long running query is a select query and no writes are happening on the database using this query. Here, we use the below query to find the number of rows processed for the sql address and hash value
FROM v$sqlarea
WHERE (address, hash_value) IN
(SELECT sql_address, sql_hash_value
FROM v$session
WHERE sid = &1)
Note: The sid can be obtained by querying the v$session table using the details available in hand like login userid, server name, logon timestamp etc.
Using UNDO Segments
This approach can be applied where the sql query is performing a DML operation. The data gets written to the UNDO segment and the below query refers to the UNDO segments to get the details.
select a.sid,
a.username,b.xidusn undoseg,
b.used_urec totrecords,
b.used_ublk totblocks,
to_char(sysdate,'mmdd hh24:mi:ss') currtime
from v$session a,
v$transaction b
where a.saddr=b.ses_addr and a.sid=&1;