Link to home
Start Free TrialLog in
Avatar of Jerry N
Jerry NFlag for United States of America

asked on

Fastest way to update with aggregates in PLSQL

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?
ASKER CERTIFIED SOLUTION
Avatar of Sean Stuber
Sean Stuber

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jerry N

ASKER

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...
Avatar of Sean Stuber
Sean Stuber

>>> 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.
Avatar of Jerry N

ASKER

Bummer.... Well it's a good time to catch up on my reading...thanks again!
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