Automatic Detection of Data Sources of Result-Cached Function

November 28, 2009

Oracle 11g release 1 gave the option of caching function results. It is done using the optional reference of mentioning RESULT_CACHE keyword. The values of these function calls are stored in system global area (SGA). It will have one result for every unique combination of parameter of passed. These are available for every session. Thus this helps in greatly reducing the execution time of an often repeated complex process inside a function which gets same repeated input values. This will increase performance in high concurrent applications.

In Oracle 11g Release 1

It is mentioned, the best candidate for result caching is function that is invoked frequently and depend on information that changes infrequently or never. Consider the following function.


CREATE OR REPLACE
FUNCTION get_sal (p_emp_id in hr.employees.employee_id%type)
RETURN NUMBER
RESULT_CACHE
IS
lv_sal_rtn       hr.employees.salary%type;
BEGIN
select salary
into lv_sal_rtn
from hr.employees
where employee_id = p_emp_id;

RETURN lv_sal_rtn;

 

EXCEPTION
WHEN others then
RETURN 0;
END;

Note: the function execution values are stored in SGA by explicitly mentioning RESULT_CACHE. And the downside of this is, at times you can get wrong return values. This is because the values are already cached in memory and not calculated again. Consider the following example.


SQL> select employee_id as id
2       , salary
3       , get_sal(employee_id)
4    from hr.employees
5   where employee_id = 100
6   /

ID     SALARY GET_SAL(EMPLOYEE_ID)
---------- ---------- --------------------
100      24000                24000

Elapsed: 00:00:00.01

SQL> update hr.employees
2     set salary = salary *1.1
3   where employee_id = 100
4   /

1 row updated.

Elapsed: 00:00:00.14

SQL> commit
2   /

Commit complete.

SQL> select employee_id as id
2       , salary
3       , get_sal(employee_id)
4    from hr.employees
5   where employee_id = 100
6   /

ID     SALARY GET_SAL (EMPLOYEE_ID)
---------- ---------- --------------------
100      26400                24000

 

Elapsed: 00:00:00.00

Even though the second time execution virtually ran in no time, it returned wrong value. Oracle 11g release 1 introduced another clause RELIES_ON to overcome this downside. By this, you can mention objects on what the function values relies on.


CREATE OR REPLACE
FUNCTION get_sal (p_emp_id in hr.employees.employee_id%type)
RETURN number
RESULT_CACHE RELIES_ON (hr.employees)

If we execute the function this time, after updating the base table, you can see that the function returns correct value. The value is not taken from system global area.


SQL> update employees
2     set salary = salary *1.1
3   where employee_id = 100
4   /

1 row updated.

Elapsed: 00:00:00.00

SQL> select employee_id as id
2       , salary
3       , get_sal(employee_id)
4    from employees
5   where employee_id = 100
6   /

ID     SALARY GET_SAL (EMPLOYEE_ID)
---------- ---------- --------------------
100      29040                29040

 

Elapsed: 00:00:00.01

In Oracle 11g Release 2

As you can see, mentioning objects in RELIES_ON gets dirty fast and quick once the function becomes complex. With Oracle 11g release 2, the database automatically detects all data sources that are queried and relied on for caching. The over head you mentioning the objects in RELIES_ON are done away in release 2.

Result Caches in Oracle RAC Environment Are No Longer Private

In the previous section, we have discussed about function result caches. The changes dint stop there. We will discuss in this section more about it. Very often, when architects implement Oracle database, they implement it as real application clusters (RAC).

As of release 11.1, each database instance in an Oracle RAC environment had a private function result cache, available only to sessions on that instance. If a required result was missing from the private cache of the local instance, the body of the function ran to compute the result, which was then added to the local cache. The result was not retrieved from the private cache of another instance.

For Release 11.2, each database instance manages its own local result cache, but the local result cache is no longer private—sessions attached to remote database instances can access its contents. If a required result is missing from the result cache of the local instance, the result might be retrieved from the local cache of another instance, instead of being locally computed.

Using Result Cache Table Annotations

Table data also can be cached for better throughput. You can use table annotations to control result caching. Although the point to note is table annotations are in effect only for the whole query, not for query segments. The primary benefit of these annotations is avoiding the necessity of adding result cache hints to queries at the application level.

A table annotation has a lower precedence than a SQL hint. Thus, you can override table and session settings by using hints at the query level. Permitted values for the RESULT_CACHE table annotation are DEFAULT and FORCE. In DEFAULT mode, the results are not cached.

While creating the table, we need to mention as


SQL> CREATE TABLE sales (...)
2  RESULT_CACHE (MODE DEFAULT);

The RESULT_CACHE clause is optional and you can skip it while creating it too. Now, what we do is, we alter the table and force it cache the results.


SQL> ALTER TABLE sales
2   RESULT_CACHE (MODE FORCE);

Then this query which uses only a portion of the table will benefit from the RESULT_CACHE.


SQL>  SELECT prod_id, SUM(amount_sold)
2     FROM sales
3    GROUP BY prod_id
4   HAVING prod_id=136;

If you want to over ride this, you can pass the hint NO_RESULT_CACHE. The compiler will understand and over ride the less precedent RESULT_CACHE defined at the table level.


SQL>     SELECT /*+ NO_RESULT_CACHE */ *
2        FROM sales
3       ORDER BY time_id DESC;

Check the documentation for more details.


Professional Resume Using Latex Templates

November 15, 2009

It is really difficult to typeset a resume. If you are already using Microsoft word (there are some word templates here) for this, and you think your resume is looking better, then this post is not for you. For all others who want to  try some thing new, I have found a good source.

Latex Resume Templates

Latex is a cool software to typeset professional looking document. It is the first choice for many. What’s more? It’s free and beats every other software which in the class by long margin. The only problem being, you need to learn the commands, tips and tricks to make the most of it.

Sample Resume output

Professional looking Resume

Read the rest of this entry »


Information is beautiful – My Week

October 28, 2009

Inspired by Information is beautiful. The site is maintained by David McCandless, a London-based author, writer and designer. And he says,

I’m interested in how designed information can help us understand the world, cut through BS and reveal hidden connections, patterns and stories underneath. Or, failing that, it can just look cool!

Here is my week.

My week

My week in numbers

Read the rest of this entry »


Urinal Protocol and Hilbert’s Hotel

October 7, 2009

There are two things I read this week and thought weird. One is about ‘Urinal protocol’.

Urinal protocol vulnerability

For those who don’t know what it is, read here. The point is, in a public rest room, every male should take due effort to use the buffer urinal. There are un-written rules, protocols people follow in using urinals. The common one being, using the urinal which is at the maximum distance to the one occupied already. Then, the point of using, allocating these resources effectively comes in as a concern. Read the rest of this entry »


Barbarians led by Bill Gates

September 25, 2009

Recently I read a book about Microsoft’s Operating systems – Windows.

This happened to be a good read if you have interests in product / software development. I have long cherished other operating systems, like Linux and other UNIX systems. Mainly for their ability to do things and robustness. Windows was never in picture. After I finished reading it, I had a complete new look towards Windows.

It is of like, poor chap, he has tried so many things. No wonder why he couldn’t be like Apple mac or Linux OS.

Few pointers from the book.

Read the rest of this entry »


Coorg Trip

June 28, 2009

We have always planned quite a lot of trips but never made it. Eager to set this trend wrong, we decided to make this trip a reality. The biggest blocker was getting a day leave. We planned three day trip instead of two day trip. This meant, we need to take leave on Monday.

Train Journey to Mysore

We started from Chennai central to Mysore. Our train was Cauvery express scheduled at 2130 hrs. We took local train from Velachery to Central. Saurabh supposed to take train from Thiruvanmiyur. We were greedy in reserving a seat next us, but have to give in once crowd started building up inside. We messaged him after getting into train. We said, we are at the last compartment from front or the first compartment from last. I messaged from Faisel’s mobile at every station. When train stopped at Thiruvanmiyur and started with a jerk, there was no sign of Saurabh. For a brief moment I thought Saurabh missed the train. Later he came from the inter-connection of the coaches and we learned that he boarded the adjacent coach. We reached Park town and then central in quick time. Anyone who watched us would have wondered where we are going, because we were carrying big, thick rain coats in our hand, and it was scorching in Chennai. Geetha argued, we should carry this as he belived it is raining heavily in Coorg. Later we realised what a waste carrying it apart from the lonely use of keeping it for your head support in train.

Read the rest of this entry »