How to identify SQL queries with the most waits in Oracle?


You want to identify the SQL statements responsible for the most waits in your database.


We can use below SQL statement to identify SQL causing problem.The below query will rank SQL statements that ran during the past 30 minutes and display them as per the total time waited by each query.


SELECT ash.user_id,   u.username,   s.sql_text,   SUM(ash.wait_time + ash.time_waited) ttl_wait_time FROM v$active_session_history ash,   v$sqlarea s,   dba_users u WHERE ash.sample_time BETWEEN sysdate - 60/2880 AND sysdate AND ash.sql_id  = s.sql_id AND ash.user_id = u.user_id GROUP BY ash.user_id,   s.sql_text,   u.username ORDER BY ttl_wait_time ;

When you have a performance problem, it is recommended to validate which SQL statements are waiting for the most. To find the queries that are waiting for the most, you must sum the values in the wait_time and the time_waited columns of the "V$ACTIVE_SESSION_HISTORY" for a specific SQL statement. In order to do this, you must join the V$SQLAREA view with the "V$ACTIVE_SESSION_HISTORY" view, using SQL_ID as the join column.