Home » RDBMS Server » Performance Tuning » pl/sql (oracle 10g)
pl/sql [message #522276] Wed, 07 September 2011 17:20 Go to next message
zodiacsom
Messages: 37
Registered: December 2009
Location: pune
Member
Hi all ..

i am executing the following procedure it took more than 2hr to complete .... please help to imporve perfomance

PROCEDURE CREATE_BAL_RES_MEDICAL
AS
l_key NUMBER(10,0);
c_key NUMBER(10,0);
lcat NUMBER(10,0);
loss_res NUMBER(10,2);
CURSOR Cursor1 IS
select A.XREF_KEY,B.XREF_res2-A.XREF_res2 dif from clmxref a join exist_clmxref
b on B.XREF_KEY = A.XREF_KEY

where B.XREF_res2 <> A.XREF_res2;
BEGIN
OPEN Cursor1;
LOOP
FETCH Cursor1 INTO c_key,loss_res;
EXIT WHEN Cursor1%NOTFOUND;
SELECT COALESCE(MAX(L.loss_key) + 1, 1) INTO l_KEY FROM LOSS L;
Select id into lcat from claim c join rv4_loss_category l on c.clm_type=l.li
neofcoverage where claim_key=c_key and bucket=2;

INSERT INTO loss (loss_key,loss_claim, loss_user, loss_type, loss_enterdate,
loss_date, loss_category, loss_amt, loss_paytype,

loss_enteruser, loss_category_nbr, ref_losscategory)
VALUES (l_key,c_key, 'SYSTEM', 'R', SYSDATE,SYSDATE,'MEDICAL',loss_res,'A','
SYSTEM',2, lcat);

COMMIT;
END LOOP;
CLOSE Cursor1;
END;
Re: pl/sql [message #522278 is a reply to message #522276] Wed, 07 September 2011 18:36 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
http://www.orafaq.com/forum/mv/msg/84315/433888/136107/#msg_433888
Re: pl/sql [message #522292 is a reply to message #522276] Thu, 08 September 2011 01:11 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
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.

For any peformances question, please read http://www.orafaq.com/forum/mv/msg/84315/433888/102589/#msg_433888 and post the required information.

Regards
Michel
Re: pl/sql [message #522309 is a reply to message #522292] Thu, 08 September 2011 04:07 Go to previous messageGo to next message
cookiemonster
Messages: 13958
Registered: September 2008
Location: Rainy Manchester
Senior Member
1) Use a sequence for loss_key.
2) Rewrite the rest as a single insert/select.
Re: pl/sql [message #522395 is a reply to message #522309] Fri, 09 September 2011 04:13 Go to previous messageGo to next message
msol25
Messages: 396
Registered: June 2011
Senior Member
Zodiac thanks for querrying with Orafaq,

Please follow posting guidelines and always include starting and closing tags for giving your coding
part in orafaq.
Please follow guidelines given by Michel.

I don't know about your schema table size and i have no table records as well,But i can give you
best solution upto my best level for your procedure:

A) ignore usage of max function in executable section of subprograms(Like Procedure,functions etc)

B) Always try to execute your query in cursor only.

C) But you may need to execute query in executable section of subprogram,so tune it properly before usage into executable section
of subprogram.

Tune your query like:

 
   explain plan for 
      select   sum(col1)
              ,col_2
      from     tab
      group by col_2;

  


But i am sure,if you are good programmer then you can write your complete query in cursor only.


If you need any help from orafaq,then please mention your example properly otherwise its difficult
for us to answer you.

Re: pl/sql [message #522396 is a reply to message #522395] Fri, 09 September 2011 04:16 Go to previous messageGo to next message
msol25
Messages: 396
Registered: June 2011
Senior Member
Zodiac,

Please ignore usage of Aggregate Function(like Max) into executable section of Subprogram because aggregate functions are very costly.
Re: pl/sql [message #522398 is a reply to message #522395] Fri, 09 September 2011 04:29 Go to previous messageGo to next message
cookiemonster
Messages: 13958
Registered: September 2008
Location: Rainy Manchester
Senior Member
msol25 wrote on Fri, 09 September 2011 10:13

Please follow posting guidelines and always include starting and closing tags for giving your coding
part in orafaq.
Please follow guidelines given by Michel.

If you're going to tell people to use code tags it helps to post a link to the page that shows how. However since Michel has already done this that would be unecessary repition.

I don't know about your schema table size and i have no table records as well,But i can give you
best solution upto my best level for your procedure:

msol25 wrote on Fri, 09 September 2011 10:13

A) ignore usage of max function in executable section of subprograms(Like Procedure,functions etc)

Why?

msol25 wrote on Fri, 09 September 2011 10:13

B) Always try to execute your query in cursor only.

Why? And all sql statements are cursors. Just most of them are implicit cursors. I assume you mean explicit cursors.

msol25 wrote on Fri, 09 September 2011 10:13

C) But you may need to execute query in executable section of subprogram,so tune it properly before usage into executable section of subprogram.

Can you execute queries outside of executable sections? What does that even mean?

msol25 wrote on Fri, 09 September 2011 10:13

Tune your query like:

 
   explain plan for 
      select   sum(col1)
              ,col_2
      from     tab
      group by col_2;

  


How does that tune a query? It gives some info you need but it doesn't tune it.

Re: pl/sql [message #522400 is a reply to message #522396] Fri, 09 September 2011 04:31 Go to previous messageGo to next message
cookiemonster
Messages: 13958
Registered: September 2008
Location: Rainy Manchester
Senior Member
msol25 wrote on Fri, 09 September 2011 10:16
Zodiac,

Please ignore usage of Aggregate Function(like Max) into executable section of Subprogram because aggregate functions are very costly.

That's really bad advice. Oracle can do aggregates very efficiently, and even if they are expensive, if you need them to get the answer then you need them.

How else would you do it? Other than using a sequence instead like I already suggested.
Re: pl/sql [message #522405 is a reply to message #522398] Fri, 09 September 2011 04:49 Go to previous messageGo to next message
msol25
Messages: 396
Registered: June 2011
Senior Member
Thanks Cookie for your query,

Please find complete steps for tuning a particular query:

Step 1: First check your explain plan using:


         explain plan for
           select   sum(col1)
                   ,col_2
           from     tab
           group by col_2;

         


Step 2: Then execute Query :


          select   *
          from     table(dbms_xplan.display);

         


Step 3: If you find full table scan for that table i.e. your indexes not working for retrieving
20-30% records from table,then use this query for those specific tables like :


          begin
             dbms_stats.gather_table_stats
             ( 
                ownname          => 'ABCD', 
                tabname          => 'TAB',
                estimate_percent => dbms_stats.auto_sample_size, 
                method_opt       => 'for all columns size skewonly', 
                cascade          => true,
                degree           => 7
            );
          end;

         


Step 4: After doing this activity then execute steps 1 and 2 :


          explain plan for
           select   sum(col1)
                   ,col_2
           from     tab
           group by col_2;

         


Step 5: Run:


          select   *
          from     table(dbms_xplan.display);

         



In this you can find cost of query and you can find index is working properly or not.If not as per
your expectation then use hint using query:


           select   /*+ index(t,idx) */ sum(t.col1)
                   ,t.col_2
           from     tab t
           group by col_2;

        


Many Thanks for your query cookie,If you can add anything according to your experience then please
add into given steps.

[Updated on: Fri, 09 September 2011 04:57] by Moderator

Report message to a moderator

Re: pl/sql [message #522407 is a reply to message #522405] Fri, 09 September 2011 04:57 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Quote:
If not as per your expectation then use hint using query:

NO!
If it does not use the index this is because the index is worse than the table scan.

Regards
Michel
Re: pl/sql [message #522416 is a reply to message #522407] Fri, 09 September 2011 05:12 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
@Michel: Or the stats are rubbish Smile
Re: pl/sql [message #522417 is a reply to message #522416] Fri, 09 September 2011 05:15 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
I just gathered them (step 3) (and I assume Oracle does not compute wrong statistics). Wink

Regards
Michel
Re: pl/sql [message #522419 is a reply to message #522417] Fri, 09 September 2011 05:16 Go to previous messageGo to next message
msol25
Messages: 396
Registered: June 2011
Senior Member
Hi Michel,

How you can say Optimizer will take right decision always.

Please Michel i can't agree this point...

Re: pl/sql [message #522421 is a reply to message #522419] Fri, 09 September 2011 05:18 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

It will be true against developer with a likelyhood of 100 to 1.

Regards
Michel
Re: pl/sql [message #522422 is a reply to message #522421] Fri, 09 September 2011 05:21 Go to previous messageGo to next message
msol25
Messages: 396
Registered: June 2011
Senior Member
Yes Michel,

lots of time i have seen Optimizer is not taking right decision in case of CBO.

Re: pl/sql [message #522423 is a reply to message #522417] Fri, 09 September 2011 05:35 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
Michel Cadot wrote on Fri, 09 September 2011 11:15
I just gathered them (step 3) (and I assume Oracle does not compute wrong statistics). Wink

Regards
Michel


You know what I meant Wink

Anecdotally, I've got a pain in the ass query right now where the range scan of an index is thought to be 500 rows (+/-), in actuality it's millions. For the life of me I can't make it see sense.

I digress, as you were folks Very Happy
Re: pl/sql [message #522433 is a reply to message #522405] Fri, 09 September 2011 08:00 Go to previous messageGo to next message
cookiemonster
Messages: 13958
Registered: September 2008
Location: Rainy Manchester
Senior Member
msol25 wrote on Fri, 09 September 2011 10:49
Thanks Cookie for your query,

Wasn't so much a query as me pointing out that your explanation was woefully incomplete.

msol25 wrote on Fri, 09 September 2011 10:49


Step 3: If you find full table scan for that table i.e. your indexes not working for retrieving
20-30% records from table,then use this query for those specific tables like :

Where did the figure of 20-30% come from? And your example query will retrieve 100% but never mind.


msol25 wrote on Fri, 09 September 2011 10:49

Many Thanks for your query cookie,If you can add anything according to your experience then please
add into given steps.


My suggestions for this particular problem are above. In more general terms - whole books have been written on this subject, do you really think it can be summed up in a short post?

In addition - you've ignored all my other points
Re: pl/sql [message #522447 is a reply to message #522433] Fri, 09 September 2011 13:16 Go to previous messageGo to next message
msol25
Messages: 396
Registered: June 2011
Senior Member
Sorry Cookie,

If you are not satisfied with my give solution,but i can explain as per your raised issues.

aggregate functions is always costly because no index can work with aggregate function and it will always work with full table scan.


I am talking only for implicit cursor but not about explicit cursor and I am not telling not use aggregate function
in executable section.If possible then try to resolve using single efficient query rather than writing small queries
each time for each value.
I want to say i.e. In most of cases we are working with relational databases and maintaining integrity constraints as well,so why
we should not bother to write single query by using integrity constraints.

why database is not having flow control for querrying on multiple tables.

If you are using lots of queries in your executable section then it will be definitely Databse design issue and we need
to resolve as soon as possible.

If Optimizer is correct always then,why oracle experts in orafaq giving solution to use hints in some specific cases.?

Cookie Keep in mind Optimizer is not god and it can be wrong and Optimizer uses Krushkal algorithm for calculating cost of
query and as per cost based algorithms(like krushkal algorithm) you can only find cost of path but not calculating traffic on that path.

Sometime we use hints by keeping in mind i.e. route can be busy so we need to use different path using hint.



I hope You will satisfy with my suggestion...
Re: pl/sql [message #522449 is a reply to message #522447] Fri, 09 September 2011 13:23 Go to previous messageGo to next message
msol25
Messages: 396
Registered: June 2011
Senior Member
hi Zodiac,

Please find path for getting information regarding usage of starting and ending tags for your code.

http://www.orafaq.com/forum/t/174502/164379/
Re: pl/sql [message #522454 is a reply to message #522447] Fri, 09 September 2011 13:58 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Quote:
aggregate functions is always costly because no index can work with aggregate function and it will always work with full table scan.

This is wrong.

Quote:
I want to say i.e. In most of cases we are working with relational databases and maintaining integrity constraints as well,so why
we should not bother to write single query by using integrity constraints

What does this mean?

Quote:
why database is not having flow control for querrying on multiple tables.

What does this mean?

Quote:
I hope You will satisfy with my suggestion...

I don't know for cookiemonster, but for myself I don't understand half of you say.

Regards
Michel
Re: pl/sql [message #522475 is a reply to message #522395] Sat, 10 September 2011 07:53 Go to previous messageGo to next message
cookiemonster
Messages: 13958
Registered: September 2008
Location: Rainy Manchester
Senior Member
msol25 wrote on Fri, 09 September 2011 19:16

If you are not satisfied with my give solution,but i can explain as per your raised issues.

There appear to be some words missing from that sentence.

msol25 wrote on Fri, 09 September 2011 19:16

aggregate functions is always costly because no index can work with aggregate function and it will always work with full table scan.

That's absolute rubbish.

msol25 wrote on Fri, 09 September 2011 19:16

I am talking only for implicit cursor but not about explicit cursor and I am not telling not use aggregate function
in executable section.

Again - why do you keep going on about executable sections? Where do you expect them to get the aggregate? In the declare section?

msol25 wrote on Fri, 09 September 2011 19:16

If possible then try to resolve using single efficient query rather than writing small queries
each time for each value.

So when you wrote this:
msol25 wrote on Fri, 09 September 2011 10:13

B) Always try to execute your query in cursor only.

You meant this:
B) Always try to execute your query in a single cursor only.

Review what you write before you post it and the rest of us might actually be able to understand


msol25 wrote on Fri, 09 September 2011 19:16

I want to say i.e. In most of cases we are working with relational databases and maintaining integrity constraints as well,so why
we should not bother to write single query by using integrity constraints.

What have integrity constraints got to do with anything?

msol25 wrote on Fri, 09 September 2011 19:16

why database is not having flow control for querrying on multiple tables.
What does that even mean?


msol25 wrote on Fri, 09 September 2011 19:16
If Optimizer is correct always then,why oracle experts in orafaq giving solution to use hints in some specific cases.?

Cookie Keep in mind Optimizer is not god and it can be wrong and Optimizer uses Krushkal algorithm for calculating cost of
query and as per cost based algorithms(like krushkal algorithm) you can only find cost of path but not calculating traffic on that path.

Who said it was always right? It's almost always right though, especially in the most recent versions as lots of work has been done on it. You should never need an index hint for a simple query. And the queries in this problem are simple. If there is a problem here due to indexes it will most likely be because the necessary index doesn't exist.

msol25 wrote on Fri, 09 September 2011 19:16

Sometime we use hints by keeping in mind i.e. route can be busy so we need to use different path using hint.

Route can be busy? No idea what that means either.
Re: pl/sql [message #522482 is a reply to message #522475] Sat, 10 September 2011 09:43 Go to previous messageGo to next message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
I think this last quote is some kind of joke, a try to be funny... I think it as it otherwise it is a total nonsense.

Regards
Michel
Re: pl/sql [message #522488 is a reply to message #522482] Sat, 10 September 2011 12:29 Go to previous messageGo to next message
cookiemonster
Messages: 13958
Registered: September 2008
Location: Rainy Manchester
Senior Member
It appears to tie in to the previous paragraph which mentions traffic
Re: pl/sql [message #522614 is a reply to message #522488] Mon, 12 September 2011 15:37 Go to previous messageGo to next message
zodiacsom
Messages: 37
Registered: December 2009
Location: pune
Member
thanks all of u ..


4r your great time
Re: pl/sql [message #522625 is a reply to message #522614] Mon, 12 September 2011 23:13 Go to previous message
Michel Cadot
Messages: 68716
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Please read OraFAQ Forum Guide and do NOT use IM/SMS speak.

Regards
Michel
Previous Topic: Advice on Gather_stats
Next Topic: Perfomance problem with select distinct from cartesian join with OR-ed filter
Goto Forum:
  


Current Time: Fri Nov 22 01:56:44 CST 2024