Skip to main content

Finding the SQL_ID

When to use it

When someone reports a slow screen or report and you need to find the statement behind it. Almost every performance tool asks for a SQL_ID.

Run this

If you know the session:

SELECT inst_id, sid, serial#, username, status, sql_id, prev_sql_id, event,
       last_call_et AS seconds_in_state
FROM   gv$session
WHERE  username = UPPER('<USERNAME>')
AND    status = 'ACTIVE';

If you know part of the SQL text:

SELECT sql_id, child_number, executions,
       ROUND(elapsed_time / 1e6) AS elapsed_seconds,
       SUBSTR(sql_text, 1, 100)  AS sql_text
FROM   gv$sql
WHERE  sql_text LIKE '%<SQL_TEXT_FRAGMENT>%'
AND    sql_text NOT LIKE '%gv$sql%'
ORDER  BY elapsed_time DESC;

How to read the output

  • sql_id identifies the statement text. The same statement always has the same SQL_ID.
  • child_number identifies one version of its plan in memory. Several children can mean several different plans.
  • event is what the session is waiting for right now. No event with status ACTIVE usually means it is on CPU.
  • elapsed_seconds is the total across all executions, so divide by executions for the average.

What to do next

  • Write down the SQL_ID. Then look at its plan on the next page.
  • If the statement is no longer in memory, it may still be in AWR: see "Seeing the execution plan".

Risk level

READ ONLY Safe to run at any time. It only queries the data dictionary.