11.07.2015 Views

Oracle Database 11 g - Online Public Access Catalog

Oracle Database 11 g - Online Public Access Catalog

Oracle Database 11 g - Online Public Access Catalog

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 4 ■ PERFORMANCE MANAGEMENT 187performance would be dramatically improved. If any of the tables that are part of the functions’queries undergo a DML change, the database purges the PL/SQL result cache.You can cache the results of PL/SQL functions executed by a session in the SGA and makethem available to all other sessions in the database. You don’t have to design a cache managementpolicy to take advantage of this new feature—it is completely automatic. Frequentlyinvoked complex PL/SQL functions that operate on relatively fixed data are ideal candidatesfor caching.Enabling PL/SQL Result CachingAs we mentioned earlier, both server-side result caches—the SQL query result cache and thePL/SQL result cache—share the same infrastructure. The database uses the same result cachebuffer for both SQL query result caching as well as PL/SQL function result caching. Thus, PL/SQLresult caching is automatically enabled in the database, with a default amount of cache sizememory (based on the SGA size) as long as you don’t explicitly turn caching off by setting theresult_cache_max_size parameter to zero.An ExampleYou can activate the PL/SQL function result cache by using the result_cache clause in a PL/SQLfunction, as shown in the following example:SQL> create or replace function count_emp (dept_no number)2 return number3 result_cache relies on(emp)4 is5 emp_ct number;6 begin7 select count(*) into emp_ct from emp8 where department_id=dept_no;9 return emp_ct;10 end;<strong>11</strong> /Function created.SQL>The previous code enables result caching on the function count_emp. The relies_on clauseis optional, and you use it to indicate that the function relies or depends on the table (empin this example) for its results. You can use the relies_on clause to specify tables or views onwhich the function depends. You can also specify a set of tables or views with the relies_on clause.The clause ensures that the database purges (invalidates) the server result cache when thedependent table undergoes a DML operation. This automatic purging of the results maintainscache consistency.Once you create a function that populates the result cache using the result_cache option,it’s time to test the function. To do this, you must call the PL/SQL function from inside a SQLquery, as shown here:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!