Home » SQL & PL/SQL » SQL & PL/SQL » Improve query using bulk collect into (Oracle 11.1 Toad on Windows server)
Improve query using bulk collect into [message #598159] Thu, 10 October 2013 15:43 Go to next message
jxh461
Messages: 185
Registered: March 2005
Senior Member
Hi guys,

I am trying to improve a procedure which is looping through a query to make inserts.
Can someone tell me how make this more efficient ?

 FOR P IN (
 
                        SELECT  O.TYPEID
                                ,o.KEY
                                ,O.ID
                                ,O.NAME
                                ,O.LGNUM
                                ,O.LGNAME
                        FROM   cOD O
                        WHERE  O.KEY     <> -1
                        AND    O.CURRFLAG = 1
                        AND    (O.TYPEID = 'PS')   --this is returning 3000+ rows
             
          ) LOOP


 INSERT INTO C_OP (		TYPEID
                               ,CKEY
                               ,CID
                               ,CNAME
                               ,CLGNUM
                               ,CLGNAME
                               ,PKEY
                               ,PID
                               ,PNAME
                               ,PLGNUM
                               ,PLGNAME
                               ,ISBOTTOMORG)
                         SELECT OP.TYPEID
                               ,OP.KEY
                               ,OP.ID
                               ,OP.NAME
                               ,OP.LGNUM
                               ,OP.LGNAME
                               ,OP.PKEY
                               ,OP.PID
                               ,OP.PNAME
                               ,OP.PGNUM
                               ,OP.PGNAME
                               ,NVL2(Q.ORGKEY, 1, 0)
                         FROM
				(SELECT P.TYPEID
                                     ,C.KEY
                                     ,C.ID
                                     ,C.NAME
                                     ,C.LGNUM
                                     ,C.LGNAME
                                     ,P.KEY          AS PKEY
                                     ,P.ID           AS PID
                                     ,P.NAME         AS PGNAME
                                     ,P.LGNUM   AS PGNUM
                                     ,P.LGNAME  AS PGNAME
                               FROM   COD C
                               START WITH C.ORGKEY = P.ORGKEY
                               CONNECT BY PRIOR C.KEY      = C.CURRP
                                              AND C.CURRENTFLAG = 1) OP
                         LEFT JOIN CLQ Q ON OP.KEY = Q.KEY;

end loop;

commit;

end;


Thanks for any help on this in advance.

[Updated on: Thu, 10 October 2013 15:43]

Report message to a moderator

Re: Improve query using bulk collect into [message #598161 is a reply to message #598159] Thu, 10 October 2013 16:42 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
1. Why do you want to commit inside the procedure you are talking about?
2. What is the bottleneck you are facing currently with your code?
Re: Improve query using bulk collect into [message #598164 is a reply to message #598161] Thu, 10 October 2013 17:16 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
I give up.
Please explain why the top SELECT (SELECT .... FROM cOD O .....) exists, since it does not contribute anything to the code below it.r

>Can someone tell me how make this more efficient ?
realize that we don't have your tables or data, so we can run posted code.
Not I.

Since NOBODY can optimize SQL just by looking at it, we need a few more details.
http://www.orafaq.com/forum/m/433888/136107/#msg_433888
Please refer to URL above & be sure to provide the details requested:
1) DDL for all tables & indexes
2) EXPLAIN PLAN
3) output from SQL_TRACE & tkprof
icon11.gif  Re: Improve query using bulk collect into [message #598174 is a reply to message #598159] Fri, 11 October 2013 00:26 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Replace the loop by a single INSERT SELECT.

Re: Improve query using bulk collect into [message #598186 is a reply to message #598164] Fri, 11 October 2013 03:21 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
BlackSwan wrote on Thu, 10 October 2013 23:16
I give up.
Please explain why the top SELECT (SELECT .... FROM cOD O .....) exists, since it does not contribute anything to the code below it.r

You gave up too quickly then since the for loop variables are used in the insert/select, both in the select part and the where clause.
Re: Improve query using bulk collect into [message #598188 is a reply to message #598159] Fri, 11 October 2013 03:49 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
jxh461 wrote on Fri, 11 October 2013 02:13

 START WITH C.ORGKEY = P.ORGKEY



I fail to understand from where P.ORGKEY came from? Either it should be P.KEY or you need to keep ORGKEY in the select list of the FOR loop.
Re: Improve query using bulk collect into [message #598792 is a reply to message #598188] Thu, 17 October 2013 15:19 Go to previous messageGo to next message
jxh461
Messages: 185
Registered: March 2005
Senior Member
You are correct.

The clause should be

START WITH C.KEY = P.KEY
Re: Improve query using bulk collect into [message #598793 is a reply to message #598159] Thu, 17 October 2013 15:21 Go to previous messageGo to next message
jxh461
Messages: 185
Registered: March 2005
Senior Member
I ended up using temp table instead and joined with the rest of the query. Slightly improved performance. Thanks
Re: Improve query using bulk collect into [message #598795 is a reply to message #598793] Thu, 17 October 2013 15:23 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
jxh461 wrote on Fri, 18 October 2013 01:51
I ended up using temp table instead and joined with the rest of the query. Slightly improved performance.


Please post what you did. It might help you to fix your issue even better, also, it will help others browsing this forum.

One more thing to add, you mentioned in the subject "Improve query using bulk collect into". But your code nowhere has any BULK COLLECT. Please post the relevant code so that someone would definitely help/assist you, and that, you need to supply enough information.

[Updated on: Thu, 06 March 2014 03:05] by Moderator

Report message to a moderator

Re: Improve query using bulk collect into [message #598800 is a reply to message #598792] Thu, 17 October 2013 15:42 Go to previous messageGo to next message
jxh461
Messages: 185
Registered: March 2005
Senior Member
I wanted to edit my post but I no longer have that option.

FOR P IN (
 
                        SELECT  O.TYPEID
                                ,o.KEY
                                ,O.ID
                                ,O.NAME
                                ,O.LGNUM
                                ,O.LGNAME
                        FROM   cOD O
                        WHERE  O.KEY     <> -1
                        AND    O.CURRFLAG = 1
                        AND    (O.TYPEID = 'PS')   --this is returning 3000+ rows
             
          ) LOOP


 INSERT INTO C_OP (		TYPEID
                               ,CKEY
                               ,CID
                               ,CNAME
                               ,CLGNUM
                               ,CLGNAME
                               ,PKEY
                               ,PID
                               ,PNAME
                               ,PLGNUM
                               ,PLGNAME
                               ,ISBOTTOMORG)
                         SELECT OP.TYPEID
                               ,OP.KEY
                               ,OP.ID
                               ,OP.NAME
                               ,OP.LGNUM
                               ,OP.LGNAME
                               ,OP.PKEY
                               ,OP.PID
                               ,OP.PNAME
                               ,OP.PGNUM
                               ,OP.PGNAME
                               ,NVL2(Q.ORGKEY, 1, 0)
                         FROM
				(SELECT P.TYPEID
                                     ,C.KEY
                                     ,C.ID
                                     ,C.NAME
                                     ,C.LGNUM
                                     ,C.LGNAME
                                     ,P.KEY          AS PKEY
                                     ,P.ID           AS PID
                                     ,P.NAME         AS PGNAME
                                     ,P.LGNUM   AS PGNUM
                                     ,P.LGNAME  AS PGNAME
                               FROM   COD C
                               START WITH C.KEY = P.KEY
                               CONNECT BY PRIOR C.KEY      = C.CURRP
                                              AND C.CURRENTFLAG = 1) OP
                         LEFT JOIN CLQ Q ON OP.KEY = Q.KEY;

end loop;

commit;

end;


I will be posting later on what my alternative is but still very interested in better solutions.
Re: Improve query using bulk collect into [message #598808 is a reply to message #598800] Thu, 17 October 2013 16:12 Go to previous message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
1. So, this is a performance tuning question.

2. Michel already posted,
Michel Cadot wrote on Fri, 11 October 2013 10:56

Replace the loop by a single INSERT SELECT.


why to use PL/SQL when you can do it using plain SQL?

3. Did you attempt to find the bottleneck where exactly the code is consuming most of the time?

I insist, to tune a code, it needs more information than just posting a PART OF YOUR CODE. Go through the performance tuning forum sticky on the top, it will help you a lot.

Regards,
Lalit
Previous Topic: Help with PL SQL
Next Topic: Getting error while creating backup table
Goto Forum:
  


Current Time: Fri Apr 26 14:23:17 CDT 2024