Solved

Fastest way to update with aggregates in PLSQL

Posted on 2014-01-22
5
436 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 74

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 74

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

726 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