Home » SQL & PL/SQL » SQL & PL/SQL » linking gv$sql and gv$session (11g)
linking gv$sql and gv$session [message #639794] Thu, 16 July 2015 07:44 Go to next message
dag1ngapr1nce
Messages: 19
Registered: June 2012
Location: UK
Junior Member
I've come across two methods of joining gv$sql with gv$session which returns different data. 1 method joins by sql_id the other by hash value / address, as follows:

Select s.*
From gv$sql s
Where s.sql_id In
(Select gv.sql_id
From gv$session gv
Where gv.sid = 243);

Select s.*
From gv$sql s
Where (s.hash_value, s.address, s.inst_id) In
(Select gv.sql_hash_value,
gv.sql_address,
gv.inst_id
From gv$session gv
Where gv.sid = 243);

Which is the correct method of joining?
Re: linking gv$sql and gv$session [message #639795 is a reply to message #639794] Thu, 16 July 2015 07:51 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>Which is the correct method of joining?

What problem are you trying to solve?
How will you (or I ) know when correct answer has been posted?

https://www.google.com/webhp?hl=en&tab=ww#hl=en&q=oracle+find+sql+for+session

[Updated on: Thu, 16 July 2015 08:58]

Report message to a moderator

Re: linking gv$sql and gv$session [message #639808 is a reply to message #639794] Thu, 16 July 2015 09:01 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Why inst_id in one and not in the other?

Re: linking gv$sql and gv$session [message #639810 is a reply to message #639794] Thu, 16 July 2015 10:08 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
matspring1 wrote on Thu, 16 July 2015 18:14

Which is the correct method of joining?


To achieve what? And why "gv$" views? Why not "v$"? What is the real problem you want to solve?
Re: linking gv$sql and gv$session [message #639813 is a reply to message #639794] Thu, 16 July 2015 12:42 Go to previous messageGo to next message
John Watson
Messages: 9003
Registered: January 2010
Location: Global Village
Senior Member
You need to be careful with queries like that. I suspect that they may have been written by someone who had not thought things through.
Are you trying to find the statement that the session is executing right now? Generally I would join on sql_id, but that is not a unique identifier (in recent releases, there may be several versions of the same sql_id so you need to inclide child_number in the join condition. Furthermore, you are querying gv$session, and sid is not a unique identifier.
Re: linking gv$sql and gv$session [message #639900 is a reply to message #639813] Mon, 20 July 2015 04:46 Go to previous messageGo to next message
dag1ngapr1nce
Messages: 19
Registered: June 2012
Location: UK
Junior Member
Thanks for replies...

I'm trying to return the current SQL that is being run in my session. This is being called from a front end form to show user the progress of an job that is running. The GV$ tables are the RAC equivalent of the V$ tables and therefore require the inst_id value to restrict by instance.

See revised SQL:

Select s.*
From gv$sql s
Where (s.sql_id, s.CHILD_NUMBER, s.INST_ID ) In
(Select gv.sql_id, gv.SQL_CHILD_NUMBER, gv.INST_ID
From gv$session gv Where gv.sid = 243);

Select s.*
From gv$sql s
Where (s.hash_value, s.address, s.inst_id) In
(Select gv.sql_hash_value,
gv.sql_address,
gv.inst_id
From gv$session gv
Where gv.sid = 243);

Any thoughts on what is the better method is appreciated, and why each SQL returns different data
Re: linking gv$sql and gv$session [message #639902 is a reply to message #639900] Mon, 20 July 2015 04:49 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Why child_number in one and not in the other?

Re: linking gv$sql and gv$session [message #639906 is a reply to message #639900] Mon, 20 July 2015 05:01 Go to previous messageGo to next message
dag1ngapr1nce
Messages: 19
Registered: June 2012
Location: UK
Junior Member
From looking at SQL examples on the internet, I've also noted that the hash_value, address and inst_id join method is mainly used to join gv$session to gv$sqlarea.

I guess then sql_id, child_number, inst_id should be used to join gv$session to gv$sql

Any thoughts?
Re: linking gv$sql and gv$session [message #639907 is a reply to message #639902] Mon, 20 July 2015 05:02 Go to previous messageGo to next message
dag1ngapr1nce
Messages: 19
Registered: June 2012
Location: UK
Junior Member
Its just the way that SQL sessions can be uniquely identified
Re: linking gv$sql and gv$session [message #639908 is a reply to message #639906] Mon, 20 July 2015 05:02 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

My questions are answers, just think a bit.

Re: linking gv$sql and gv$session [message #639909 is a reply to message #639907] Mon, 20 July 2015 05:03 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
matspring1 wrote on Mon, 20 July 2015 12:02
Its just the way that SQL sessions can be uniquely identified


What "Its" is?

Re: linking gv$sql and gv$session [message #639926 is a reply to message #639909] Mon, 20 July 2015 08:05 Go to previous messageGo to next message
dag1ngapr1nce
Messages: 19
Registered: June 2012
Location: UK
Junior Member
My thoughts...

"Its" is the SQL optimizer - The child number is used in the SQL join that is concerned about a specific child record used by the parser; The SQL that joins by hash_value, address and inst_id does not care about the child record returned.

Is the correct way to join between v$sql and v$session dependant upon the version of db used? If so which db version should use which join?

thanks for help
Re: linking gv$sql and gv$session [message #639927 is a reply to message #639926] Mon, 20 July 2015 08:10 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
How does Connection Pooling impact the problem & solution?
Re: linking gv$sql and gv$session [message #639928 is a reply to message #639926] Mon, 20 July 2015 08:12 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Quote:
The SQL that joins by hash_value, address and inst_id does not care about the child record returned.


Are you sure?
Just add it to check.

Re: linking gv$sql and gv$session [message #639935 is a reply to message #639928] Mon, 20 July 2015 08:46 Go to previous message
dag1ngapr1nce
Messages: 19
Registered: June 2012
Location: UK
Junior Member
So I'm corrected the child_number SHOULD be used when joining by hash / address, I did find duplicates in some instances where the child_number was not used.

In this thread I initially said that I was getting different results when using the 2 methods, I think this was down to the timing of running sql 1 before sql 2, I now get exactly the same results when I run the following SQL:

Select s.*
From gv$sql s
Where (s.sql_id, s.CHILD_NUMBER, s.INST_ID ) In
(Select gv.sql_id, gv.SQL_CHILD_NUMBER, gv.INST_ID
From gv$session gv Where gv.sid = 243);

Select s.*
From gv$sql s
Where (s.hash_value, s.address, s.inst_id, s.CHILD_NUMBER) In
(Select gv.sql_hash_value, gv.sql_address, gv.inst_id, gv.child_number
From gv$session gv
Where gv.sid = 243);

Please note this is on a 11g instance, I'm curious as to whether I'd get the same results on 9i, 10g, 12c etc......any thoughts?
Previous Topic: Error with Delete Trigger
Next Topic: Bulk collect limit on what basis we will keep
Goto Forum:
  


Current Time: Fri Jul 31 19:13:37 CDT 2026