Watching SQL Execute on Oracle - Part I
July 1, 2004
Do you have SQL running within your database? Of course you do. This article is the first in a series to introduce you to a method of finding information about the SQL your users are executing in your databases.
Your ability as a DBA to detect who is accessing the database and the SQL they are executing is paramount in your ability to properly manage and give feedback on the type of work your database is requested to do. This article will begin our series with an introduction to determining who is logged into your database and what SQL they are executing. This article is not concerning itself with the tuning of SQL but rather a primer so that you can get familiar with or re-acquainted with the underlying tables within Oracle that give information on who and what is being done around the SQL.
The V$SESSION is often the jumping off place to determine who is logged into the database and a high level overview of what they are doing. In Table 1 I have given a subset of the columns this view contains. While there are other columns in this view that relate to operating system information and the application being executed, I am only presenting those columns that give us the basis for our jumping off point to determine that this is in fact an active user and also the columns we will need later to join to the actual SQL being executed.
In order to extract the information, you can issue a SQL statement such as the following in Listing 1. This is a very simplistic statement but gives us all the information we need to determine if the user is actually executing SQL at the time. If a user has SQL that is executing, the status column will be ACTIVE and the SQL_ADDR & SQL_HASH_VALUE will be populated. I have given the output of two different executions of this particular SQL against the V$SESSION view. The first shows where a user was noticed logged into our database but is basically inactive and there is no SQL address or hash value. The second execution of this SQL shows a user who is actively executing SQL, denoted by the values in the address and hash value columns.