Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solution to implementing caching layer in pl/sql

I have a function with 1 argument (date) which encapsulates 1 query like

SELECT COUNT(*)
  FROM tbl
 WHERE some_date_field BETWEEN param_date - INTERVAL '0 1:00:00' DAY TO SECOND
                           AND param_date

What I want to do is to cache somewhere the result of this query with ttl = 1 minute. The cached result should be shared across all sessions, not just current one.

Any proposals?

PS: Yes, I know about oracle function result cache, but it doesn't fit the requirements.
PPS: Yes, we can create 2nd artificial argument with some value like date in format of yyyymmddhh24mi so it changes each minute and we're able to use function result cache, but I hope it is a solution which will allow me to hide the caching dependencies inside.

like image 410
zerkms Avatar asked Mar 31 '11 06:03

zerkms


People also ask

What is caching in Oracle SQL?

Oracle Database Cache improves the scalability and performance of applications that access Oracle databases by caching frequently used data on a middle-tier system. With Oracle Database Cache, your applications can process several times as many requests as their original capacity.

What is cache in Plsql?

The cross-session PL/SQL function result cache provides a simple way to boost the performance of PL/SQL functions by saving the results of function calls for specific combinations of input parameters in the SGA. These results can be reused by any session calling the same function with the same parameters.

What is cache memory in Oracle?

Oracle In-Memory Database Cache is an Oracle Database product option ideal for caching a performance-critical subset of an Oracle database in the application tier for improved response time.


2 Answers

You want to cache the result of this query, and share the cache across all sessions. The only way I can think of is to wrap the query in a function call, store the result in a small table. The function will query the small table to see if the count has already been stored within the last 1 minute, and if so, return it.

You would keep the table small by running a job periodically to delete rows in the "cache table" that are older than 1 minute - or better still, perhaps truncate it.

However, I can only see this being of benefit if the original SELECT COUNT(*) is a relatively expensive query.

like image 33
Jeffrey Kemp Avatar answered Oct 01 '22 01:10

Jeffrey Kemp


I'd use a global application context, and a job with a refresh interval of 1 minute to set the context.

PS: INTERVAL '1' HOUR is shorter and more meaningful than INTERVAL '0 1:00:00' DAY TO SECOND

like image 104
Rob van Wijk Avatar answered Oct 01 '22 01:10

Rob van Wijk