Solved

Fastest way to update with aggregates in PLSQL

Posted on 2014-01-22
5
431 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

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…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

746 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

10 Experts available now in Live!

Get 1:1 Help Now