Home » SQL & PL/SQL » SQL & PL/SQL » SELECT works, INSERT SELECT hangs (merrged 2) (Oracle 11g on XP (testing server))
SELECT works, INSERT SELECT hangs (merrged 2) [message #507635] Tue, 17 May 2011 07:29 Go to next message
Unknown_Entity
Messages: 9
Registered: May 2011
Junior Member
Hello, I tried to google this problem but was unable to find and solution. I have quite a long query in format of

INSERT INTO table
SELECT (...)

which in SQL Developer just "hangs", it never finishes, does nothing --- if I query the v$session I can identify its SID and just watch time_waited (or something like it) to increase.

The strange think though is, that the select without insert completes in just a few seconds. If I replace selected columns with static values (i.e. instead of select a, b ... I do select 'alpha', 'beta' ) and use same table joins and conditions as before it works as well (only change is that I add where rownum=1 to avoid dupes).

Could you please point me to how to debug this? I have full access to the server and its logs.

Things I ve tried are:

select * from dba_locks -> none shows as blocking when the query is running

select * from dba_waiters -> empty

Thank you for your help, I ve spend one full day on this and its getting really frustrating:/
Re: SELECT works, INSERT SELECT hangs [message #507636 is a reply to message #507635] Tue, 17 May 2011 07:31 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
SQL Developer defaults to the first 50 rows - is your select bring ALL the rows back?

An insert would need to handle all rows first, there is no first n option there.
Re: SELECT works, INSERT SELECT hangs [message #507639 is a reply to message #507636] Tue, 17 May 2011 07:43 Go to previous messageGo to next message
cookiemonster
Messages: 13967
Registered: September 2008
Location: Rainy Manchester
Senior Member
To see what long running queries are doing query v$session_longops.
I suspect Roachcoach's analysis is correct.
Re: SELECT works, INSERT SELECT hangs [message #507651 is a reply to message #507639] Tue, 17 May 2011 07:57 Go to previous messageGo to next message
Unknown_Entity
Messages: 9
Registered: May 2011
Junior Member
Thank you for your replies, it seems to work now, for some reason :/

What I did is that I appended commit; after all the executes in my query window - could that be the reason? And if yes, why didnt it show in dba_waiters or in dba_locks as blocking? thanks
Re: SELECT works, INSERT SELECT hangs [message #507654 is a reply to message #507651] Tue, 17 May 2011 08:00 Go to previous messageGo to next message
cookiemonster
Messages: 13967
Registered: September 2008
Location: Rainy Manchester
Senior Member
Adding a commit to the end will make no difference to the speed. It'll also make no difference to locks, since if your insert really was hanging then it'd the other session blocking it that would need to commit.
It probably appeared to start working due to caching - After the select has been run a few times the datablocks will all be loaded in the cache.
Subsequent queries against them will run faster is oracle no longer needs to go to disk to get the data.
Re: SELECT works, INSERT SELECT hangs [message #507660 is a reply to message #507654] Tue, 17 May 2011 08:11 Go to previous messageGo to next message
Unknown_Entity
Messages: 9
Registered: May 2011
Junior Member
it is true that I did force the select query to the last row (ctrl+end) in developer prior to trying the insert/select. But the (full) select completed in few seconds as well and the insert/select query would hang for hours before.

My only guess is there had to be some lock in place but not showing in dba_locks or waiters.

I would love to understand this problem :/ because I fear it will happen again Smile
Re: SELECT works, INSERT SELECT hangs [message #507663 is a reply to message #507660] Tue, 17 May 2011 08:14 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Maybe if you COPY AND PASTE your SQL*Plus session (with "set timing on") we can see what you see.

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code, use code tags and align the columns in result.
Use the "Preview Message" button to verify.
Also always post your Oracle version, with 4 decimals.

Regards
Michel
Re: SELECT works, INSERT SELECT hangs [message #507669 is a reply to message #507663] Tue, 17 May 2011 08:24 Go to previous messageGo to next message
cookiemonster
Messages: 13967
Registered: September 2008
Location: Rainy Manchester
Senior Member
Next time try tracing the session. That'll definitely show what it's waiting on.
Re: SELECT works, INSERT SELECT hangs [message #507670 is a reply to message #507669] Tue, 17 May 2011 08:29 Go to previous messageGo to next message
ThomasG
Messages: 3212
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
Also, I have included this query in our "Helpdesk" tools. It pretty much has returned the problematic query every time someone complained that "the system hangs"

(Should work in 10.X and up, 9.X would need an additional join to v$session_event)

select DISTINCT s.sid,s. SERIAL#, event, p1text, p1, p2text, p2, p3text, p3,
       wait_time, seconds_in_wait, state, osuser, program,client_info, s.MODULE, sql_text
  from v$session s
  join v$sql q ON q.hash_value = s.sql_hash_value
 where event not in ('SQL*Net message from client', 'SQL*Net message to client', 'jobq slave wait')
  AND  event not like 'PX Deq Credit%';
Re: SELECT works, INSERT SELECT hangs [message #507694 is a reply to message #507670] Tue, 17 May 2011 09:42 Go to previous messageGo to next message
Unknown_Entity
Messages: 9
Registered: May 2011
Junior Member
thank you for the "debug" query,

however we now have another problem, we have a query with "where rownum <= 100" which hangs as well. If we omit the where rownum, it works immediately...

In the debug query it shouws as "event SQL*Net message to client" (which you filtred out in the debug query, but that is the source of our problems - the query yesterday hanged with the same event).

The problem is what do we do about it? Thank you.
Re: SELECT works, INSERT SELECT hangs [message #507698 is a reply to message #507694] Tue, 17 May 2011 09:49 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>The problem is what do we do about it?
It would be helpful if you followed Posting Guidelines - http://www.orafaq.com/forum/t/88153/0/

It is really, Really, REALLY difficult to fix a problem that can not be seen.
use COPY & PASTE so we can see what you do & how Oracle responds.
Re: SELECT works, INSERT SELECT hangs [message #507699 is a reply to message #507694] Tue, 17 May 2011 09:50 Go to previous messageGo to next message
ThomasG
Messages: 3212
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
Well, the "message to/from client" events are pretty much there every time the client isn't doing anything. (for example waiting for user input).

So the solution would be to tell people to "type faster" ;-P

The *Other* possibility of those waits could happen would be an actual network bottleneck, where the database or the client is just waiting for the data to get transferred. But I have never seen one of those, unless it was a very slow WAN connection.
Re: SELECT works, INSERT SELECT hangs [message #507702 is a reply to message #507699] Tue, 17 May 2011 09:57 Go to previous messageGo to next message
Unknown_Entity
Messages: 9
Registered: May 2011
Junior Member
what do you mean tell them to type faster?

SELECT * FROM table WHERE rownum <= 100

press F5 to run the query -> it hangs forever...

I do apologize for not copy/pasting results, but the db computer is on site without net access and its data are rather sensitive:/
Plus I really dont know what to copy/paste anyway, if you tell me what to paste here I will - now its just IDE "running" the simple query forever
Re: SELECT works, INSERT SELECT hangs [message #507705 is a reply to message #507702] Tue, 17 May 2011 10:01 Go to previous messageGo to next message
cookiemonster
Messages: 13967
Registered: September 2008
Location: Rainy Manchester
Senior Member
Run a sql trace on the session - that's the best way to find out what it's doing.
You do realise that your example query will return a random selection of rows from the table?
Re: SELECT works, INSERT SELECT hangs [message #507706 is a reply to message #507705] Tue, 17 May 2011 10:02 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
http://wiki.oracle.com/page/HOW+TO+trace+your+own+session
Re: SELECT works, INSERT SELECT hangs [message #507711 is a reply to message #507706] Tue, 17 May 2011 10:11 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
I use SQL developer a lot, but it can get...funky on some queries/script outputs. If you think the wait is suspect, first thing you should try is using SQL*Plus.

Start>Run>cmd
sqlplus USER/PASS@SID
set timing on

Re: SELECT works, INSERT SELECT hangs [message #507714 is a reply to message #507706] Tue, 17 May 2011 10:14 Go to previous messageGo to next message
Unknown_Entity
Messages: 9
Registered: May 2011
Junior Member
thank you, I will try to trace the session later,

and I assume it returns first 100 rows from the output, provided that I use order by clause it should return the same rows each time.
Re: SELECT works, INSERT SELECT hangs [message #507715 is a reply to message #507702] Tue, 17 May 2011 10:17 Go to previous messageGo to next message
ThomasG
Messages: 3212
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
Unknown_Entity wrote on Tue, 17 May 2011 16:57
what do you mean tell them to type faster?


I mean that especially the "message from client" wait event is just the client waiting for user input. So the only way to get rid of that wait event is to tell the user to type faster.
Re: SELECT works, INSERT SELECT hangs [message #507716 is a reply to message #507714] Tue, 17 May 2011 10:17 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
If you mean

Select * from
(select * from T order by 1)
where rownum <4


then yes.
Re: SELECT works, INSERT SELECT hangs [message #507720 is a reply to message #507715] Tue, 17 May 2011 10:21 Go to previous messageGo to next message
Unknown_Entity
Messages: 9
Registered: May 2011
Junior Member
ThomasG wrote on Tue, 17 May 2011 10:17

I mean that especially the "message from client" wait event is just the client waiting for user input. So the only way to get rid of that wait event is to tell the user to type faster.


I am not sure if you understand what is my problem;
1. I enter the SQL query
2. I run it
3. it never completes, hangs on waiting for client

so what use is it to type faster:) ?
Re: SELECT works, INSERT SELECT hangs [message #507721 is a reply to message #507716] Tue, 17 May 2011 10:24 Go to previous messageGo to next message
cookiemonster
Messages: 13967
Registered: September 2008
Location: Rainy Manchester
Senior Member
So you're actually comparing:
Select * from
(select * from T order by column)
where rownum <100

and
Select * from
(select * from T order by column)


In which case oracle can use a different approach for finding the data in each case. And that'd be a different approach again to what you initially said you were running. Posting accurate sql is necessary if you want accurate help.
You can do the trace.
You can also do an explain plan of each and post them here.
Re: SELECT works, INSERT SELECT hangs [message #507724 is a reply to message #507721] Tue, 17 May 2011 10:31 Go to previous messageGo to next message
Unknown_Entity
Messages: 9
Registered: May 2011
Junior Member
it seems really ridiculous, but now the same query works on computer A and not on computer B. I tested both raw sqlplus connection and SQL developer connection on both computers. Every time either both work or both dont. Both sqlplus clients can successfully connect to the DB.
Re: SELECT works, INSERT SELECT hangs [message #507727 is a reply to message #507724] Tue, 17 May 2011 10:34 Go to previous messageGo to next message
cookiemonster
Messages: 13967
Registered: September 2008
Location: Rainy Manchester
Senior Member
Then you definitely need to run a trace.
Re: SELECT works, INSERT SELECT hangs [message #507728 is a reply to message #507720] Tue, 17 May 2011 10:46 Go to previous messageGo to next message
ThomasG
Messages: 3212
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
Unknown_Entity wrote on Tue, 17 May 2011 17:21
ThomasG wrote on Tue, 17 May 2011 10:17

I mean that especially the "message from client" wait event is just the client waiting for user input. So the only way to get rid of that wait event is to tell the user to type faster.


I am not sure if you understand what is my problem;
1. I enter the SQL query
2. I run it
3. it never completes, hangs on waiting for client

so what use is it to type faster:) ?


My point is: When it still is "waiting for the client", then it is NOT really "running" it is still waiting for the client to start it. Which means you never really did point 2 "run it"
Re: SELECT works, INSERT SELECT hangs [message #507731 is a reply to message #507727] Tue, 17 May 2011 11:07 Go to previous messageGo to next message
Unknown_Entity
Messages: 9
Registered: May 2011
Junior Member
cookiemonster wrote on Tue, 17 May 2011 10:34
Then you definitely need to run a trace.


now I have the SQL trace, the query which I traced still hangs (20mins), nothing have been written into the long in past approx 19minutes..,

WAIT #0: nam='SQL*Net message from client'

and

PARSING IN CURSOR #3 about 10 times...

and then
PARSING IN CURSOR #4 which actually contains the SQL it hangs on :/

Anything particular that would be helpful?

Re: SELECT works, INSERT SELECT hangs [message #507733 is a reply to message #507731] Tue, 17 May 2011 11:10 Go to previous messageGo to next message
cookiemonster
Messages: 13967
Registered: September 2008
Location: Rainy Manchester
Senior Member
It'd make life a lot easier if you posted the full contents so we can see it for ourselves. It'd also help if you got a trace of the same statement working for comparison purposes.
Re: SELECT works, INSERT SELECT hangs [message #507752 is a reply to message #507714] Tue, 17 May 2011 12:34 Go to previous message
joy_division
Messages: 4963
Registered: February 2005
Location: East Coast USA
Senior Member
Unknown_Entity wrote on Tue, 17 May 2011 11:14

and I assume it returns first 100 rows from the output, provided that I use order by clause it should return the same rows each time.


Incorrect assumption. WHERE clause comes first, then it ORDERs the output.
Previous Topic: About Nested table (2 Merged)
Next Topic: cursor loop question
Goto Forum:
  


Current Time: Wed Aug 20 04:52:07 CDT 2025