Solved

Fastest way to update with aggregates in PLSQL

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
case statement in where clause with not exist 15 52
Fastest way to replace data in Oracle 5 65
execute immediate plsql block 5 47
PL/SQL Display based on value 4 29
Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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.
Via a live example, show how to take different types of Oracle backups using RMAN.

810 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