Solved

Fastest way to update with aggregates in PLSQL

Posted on 2014-01-22
5
432 Views
Last Modified: 2014-01-23
I have two tables:
Product_table
      Product_ID
      AVG_Cost

Transaction_Table
      Product_ID
      Item_Cost
      Condition_ID


I wish to update the AVG_COST in the Product_table(2100 records) with the
Average Item_Cost from the TransactionTable (4 million records)  where the Condition_ID is not 47 or 61

What is the best way to handle this in PLSQL?
0
Comment
Question by:GNOVAK
  • 2
  • 2
5 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 39800964
I assume you wanted the average cost per product not simply the average cost of all products.

fastest way is to NOT use pl/sql.  Just one sql statement


UPDATE product_table p
   SET avg_cost =
           (SELECT AVG(item_cost)
              FROM transaction_table t
             WHERE p.product_id = t.product_id AND t.condition_id NOT IN (47, 61))


if by chance you did want to assign a global average to every product, simply remove this portion of the subquery

"p.product_id = t.product_id AND"
0
 

Author Comment

by:GNOVAK
ID: 39801088
Thanks.
That's what I had thought - it had gotten up to 16 minutes and I thought there was something wrong, so I'm running again. I have indexed on product_Id. I would hate to see how long it takes without that index!

When I try to hardcode the product_id into the statement it takes 444msec.  That made me wonder.  Do you think it might be faster to loop through a cursor, grab the product_id and do an execute immediate for each concontenated product_id?

These updates are excruitiating...
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 39801118
>>> Do you think it might be faster to loop through a cursor, grab the product_id and do an execute immediate for each concontenated product_id?


No, definitely not.

The only way you might get better wall-clock time is if you split the list of products into pieces and did them in parallel.  Of course, doing this then creates contention on reads and writes, so no guarantee.

Something like this...

UPDATE product_table p
   SET avg_cost =
           (SELECT AVG(item_cost)
              FROM transaction_table t
             WHERE p.product_id = t.product_id AND t.condition_id NOT IN (47, 61))
where product_id between 1 and 100;

UPDATE product_table p
   SET avg_cost =
           (SELECT AVG(item_cost)
              FROM transaction_table t
             WHERE p.product_id = t.product_id AND t.condition_id NOT IN (47, 61))
where product_id between 101 and 200;

UPDATE product_table p
   SET avg_cost =
           (SELECT AVG(item_cost)
              FROM transaction_table t
             WHERE p.product_id = t.product_id AND t.condition_id NOT IN (47, 61))
where product_id between 201 and 300;

etc.  Each of them running in their own session





In any case, writing your own loop which then forces a parse per row is definitely NOT going to be faster.
0
 

Author Comment

by:GNOVAK
ID: 39801141
Bummer.... Well it's a good time to catch up on my reading...thanks again!
0
 
LVL 8

Expert Comment

by:Surrano
ID: 39802325
Not sure but this inner aggregate query on a 4M table repeated 2100 times gives me the creeps. I'd do something like this:

create table avg_helper as
select product_id pid, avg(item_cost) a from transaction_table t 
where t.condition_id not in (47,61)
group by product_id;

create index avg_helper_pid on avg_helper (pid);

update product_table p set avg_cost = (select a from avg_helper h where p.product_id=h.pid);

drop table avg_helper;

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now