Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old August 26th, 2004, 10:53 AM
cheesyhoward cheesyhoward is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 6 cheesyhoward User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 15 sec
Reputation Power: 0
Caching results of formulas during a select

I regularly have to process data in a table (2 million rows+) and update a few fields (MEASUREMENT1 in this example) with conversions or averaged values. Many of the original values are the same eg there could be 1000+ rows with the value 123 in the measurement field.

I can do most of the conversions within a select statement. However this is slow because for each row, the value in each ‘measurement’ field has the formula applied to it.

CREATE OR REPLACE PROCEDURE process_data
AS
CURSOR test_update IS
SELECT x_pos, y_pos, round(power(2,MEASUREMENT1/12),8) the_result, done
FROM measurement_table
ORDER BY x_pos, y_pos
FOR UPDATE OF measurement1, done;

current_row test_update%ROWTYPE;
converted_measurement FLOAT;

BEGIN

OPEN test_update;

LOOP
FETCH test_update INTO current_row;
EXIT WHEN test_update%NOTFOUND;
converted_measurement := current_row.the_result;
-- other stuff happens here with x_pos y_pos
UPDATE measurement_table
SET measurement1 = converted_measurement, done = 1
WHERE CURRENT OF test_update;


END LOOP;

CLOSE test_update;
commit;
END;
/


Another approach is not to do the conversion during the select statement but instead do it for each new measurement value and store the conversion in a lookup table. This means the amount of time spent doing conversions is reduced because after the conversion has been done once the value can be looked up in ‘power_lookup1’.


CREATE OR REPLACE PROCEDURE process_data
AS
CURSOR test_update IS
SELECT x_pos, y_pos, MEASUREMENT1 the_result, done
FROM measurement_table
ORDER BY x_pos, y_pos
FOR UPDATE OF measurement1, done;

current_row test_update%ROWTYPE;
converted_measurement FLOAT;

TYPE power_lookup IS TABLE OF FLOAT INDEX BY VARCHAR2(25);
power_lookup1 power_lookup;

BEGIN

OPEN test_update;

LOOP
FETCH test_update INTO current_row;
EXIT WHEN test_update%NOTFOUND;
-- see if current measurement is already in the lookup table
IF power_lookup1.EXISTS(to_char(current_row.the_result)) = false THEN
-- This conversion has not yet been done so perform it & add to lookup table
power_lookup1(to_char(current_row.the_result)) := round(power(2,current_row.the_result/12),8);
END IF;
-- get power from lookup table
converted_measurement := power_lookup1(to_char(current_row.the_result));
-- other stuff happens here with x_pos & y_pos
UPDATE measurement_table
SET measurement1 = converted_measurement, done = 1
WHERE CURRENT OF test_update;


END LOOP;

CLOSE test_update;
commit;
END;
/


The lookup table version is about 5 times quicker than the original. This code is a very simple example of what actually takes place in the real table. The real table contains many more formulas so I would prefer not to create lots of lookup tables. Is there any technique or Oracle Hint that will cache the result of the power formula and can ideally be done directly in a select statement?

Reply With Quote
  #2  
Old August 30th, 2004, 01:11 AM
Jigar_pota Jigar_pota is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 7 Jigar_pota User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Try "Cache" Hint

Hi,

Try the the "cache" hint in the select statement to tell oracle to cache the result of select statement.

All the best,

Jigar

Reply With Quote
  #3  
Old September 6th, 2004, 03:54 AM
cheesyhoward cheesyhoward is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 6 cheesyhoward User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 15 sec
Reputation Power: 0
I have fixed the problem using bulk fetch / insert statments. Thanks for the help.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > Caching results of formulas during a select


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway