Skip to Main Content
  • Questions
  • How to identify sql query running against a database

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Sumit.

Asked: February 05, 2017 - 6:00 pm UTC

Last updated: February 06, 2017 - 3:12 pm UTC

Version: 11gr2

Viewed 50K+ times! This question is

You Asked

Hi Toms Team,

First of all thanks to you for sharing information using "Ask Tom" platform.

My question is
When we run any sql against a database (I mean simple select sql eg. select 1 from dual) we can see this information by querying against v$sqlarea and v$session (sometime if the query is more than 1000 characters then we use v$sqltext)

But the same is not applicable to the below :
1. If we execute any procedure having multiple sql statements one after another then in that case sql_text shows only the procedure being executed and not the internal query.How can I check the query which is executing against SQL database inside procedure and the parameters being passed to it if any?
2. Similarly when a third party application executes sql queries from their code can we check the sql executed by using v$session/v$sqlarea/v$sqltext ?

As far as I can see my assumption is that such logic is embedded in AWR report where we can get the exact sql id and sql text which is consuming more time or is expensive.
So here for point 1 and 2 , queries will be available in AWR report.

So would look to know how do we get this information for point 1 and 2 from the Database views instead of AWR ?

Thanks in Advance
Sumit Pawar

and Connor said...

Your assumption for (1) is not correct. If your procedure contains 3 sql's, then within v$sql you will the call to the procedure (as one sql_id) *and* the calls to each of the sql's (as their own sql_id's).

Depending on when you look at (say) v$session, the sql_id will reflect the *currently* running one (eg, if you are in the plsql proc and not running a sql 'right now', you'd see the sql_id for the call to the proc).

One nice thing about v$sql, is that there is the PROGRAM_xxx columns, which indicate the object_id and line number of the plsql program from which a sql command was initiated. You can use this to link poor running sql's back to the source code.

One other thing - if you are looking for stats about sql performance, its more efficient to query v$sqlstats

Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Sam Jacob, February 06, 2017 - 9:52 pm UTC

You can query V$SQL to see the queries running against the database

Line number of plsql

Rajeshwaran, Jeyabal, February 07, 2017 - 1:15 pm UTC

....
One nice thing about v$sql, is that there is the PROGRAM_xxx columns, which indicate the object_id and line number of the plsql program from which a sql command was initiated
.....

could you help me to understand, which column from v$session indicate the line no of the plsql unit?

demo@ORA11G> create or replace procedure p
  2  as
  3  begin
  4     for x in (select b1.object_name , b2.username
  5             from all_objects b1 ,
  6                     all_users b2)
  7     loop
  8             NULL ;
  9     end loop;
 10  end;
 11  /

Procedure created.

demo@ORA11G> select userenv('sid') from dual;

USERENV('SID')
--------------
            11

demo@ORA11G>
demo@ORA11G> exec p ;

PL/SQL procedure successfully completed.


While the above was running from sql*plus, able to see these details from the other session.

demo@ORA11G> column sql_fulltext format a20
demo@ORA11G> column prev_sql_fulltext format a20
demo@ORA11G> column plsql_entry_object_id format 99999 heading c1
demo@ORA11G> column plsql_entry_subprogram_id format 99999 heading c2
demo@ORA11G> column plsql_object_id format 99999 heading c3
demo@ORA11G> column plsql_subprogram_id format 99999 heading c4
demo@ORA11G> select t1.sql_id,
  2      ( select t2.sql_fulltext
  3        from v$sql t2
  4        where t1.sql_id = t2.sql_id and
  5        t1.sql_child_number = t2.child_number ) sql_fulltext,
  6        ( select t2.sql_fulltext
  7        from v$sql t2
  8        where t1.prev_sql_id = t2.sql_id and
  9        t1.prev_child_number = t2.child_number ) prev_sql_fulltext,
 10        plsql_entry_object_id,
 11        plsql_entry_subprogram_id,
 12        plsql_object_id,
 13        plsql_subprogram_id
 14  from v$session t1
 15  where sid = 11 ;

SQL_ID        SQL_FULLTEXT         PREV_SQL_FULLTEXT        c1     c2     c3     c4
------------- -------------------- -------------------- ------ ------ ------ ------
6phkrurssuyjf SELECT B1.OBJECT_NAM BEGIN DBMS_OUTPUT.GE  88503      1
              E , B2.USERNAME FROM T_LINES(:LINES, :NUM
               ALL_OBJECTS B1 , AL LINES); END;
              L_USERS B2


Also looked into this, could not find that.

http://docs.oracle.com/database/121/REFRN/GUID-28E2DC75-E157-4C0A-94AB-117C205789B9.htm#REFRN30223

Please ignore the above

Rajeshwaran, Jeyabal, February 07, 2017 - 1:23 pm UTC

Please ignore the above question, I got the answer from the blog.

https://connormcdonald.wordpress.com/2016/01/20/problematic-sql-plsql-is-your-friend/

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library