How can I see queries that are executed against Oracle?

vld_apos picture vld_apos · Jul 13, 2010 · Viewed 48.2k times · Source

I need to see the queries that are being sent to Oracle to execute them. Can someone give me specific detailed instructions on how to do this ?

Answer

bhangm picture bhangm · Jul 13, 2010

If you want to see the queries from a specific user, you can use this (assuming you have privileges to query v$session and v$sqlarea (usually through SELECT_CATALOG_ROLE)

SELECT sess.sid,
       sess.username,
       sqla.optimizer_mode,
       sqla.hash_value,
       sqla.address,
       sqla.cpu_time,
       sqla.elapsed_time,
       sqla.sql_text
  FROM v$sqlarea sqla, v$session sess
 WHERE sess.sql_hash_value = sqla.hash_value
   AND sess.sql_address = sqla.address
   AND sess.username = 'SCOTT'

Replace SCOTT with the appropriate username in your system

Output:

 544 SCOTT      ALL_ROWS   2004330732 07000001064088E8         89391       131836 SELECT sess.sid,        sess.username,
                                                                                        sqla.optimizer_mode,        sqla.h
                                                                                  ash_value,        sqla.address,        s
                                                                                  qla.cpu_time,        sqla.elapsed_time,
                                                                                         sqla.sql_text   FROM v$sqlarea sq
                                                                                  la, v$session sess  WHERE sess.sql_hash_
                                                                                  value = sqla.hash_value    AND sess.sql_
                                                                                  address = sqla.address    AND sess.usern
                                                                                  ame = 'SCOTT'