Friday, May 9, 2014

What are Private procedure / functions in a Package ?

A function or procedure in a package which is not declared in package specification, its called as private function / procedure.

Scope of private function /procedure is very limited - on the package itself only.

While in declaring the private procedure/function in package, need to take care that it must be declare first in package declaration part of the package.

create or replace package pack_p1 is
    procedure proc_1;
end pack_p1;

create or replace package body pack_p1 is


    procedure test_proc_1 is

    begin
        dbms_output.put_line('package pack_p1 private procedure');
    end test_proc_1;

    function func_1 is

    begin
        dbms_output.put_line('package pack_p1 private function');
    end func_1;

    procedure proc_1 is

    begin
        dbms_output.put_line('package pack_p1 - proc 1');
        test_proc_1;
    end proc_1;

end pack_p1;

Thursday, May 8, 2014

What to do I have to do when query is too slow, which was earlier running fine?

You need to know why its became slow suddenly. Need to check through following ways, rewrite the query or applying oracle hints on query or using parallellization. So the first step of yours should be to investigate the root cause.

Widely used tools are:
- dbms_profiler
- Explain plan
- SQL*Trace / TKPROF
- Statspack

dbms_profiler:

Use dbms_profiler, if you want to know where time is being spent in PL/SQL code.
Statspack is also a good tool if you are a DBA and you want to check query performance for entire database.

For a single query , explain plan & SQL*Trace & TKPROF are useful tools for query execution.

Explain plan:

in SQL*Plus you have to type:

explain plan for ;
select * from table(dbms_xplan.display);


if you are using older version of plan_table, you have to run the script $ORACLE_HOME\RDBMS\ADMIN\utlxplan.sql.

To get plan table output on serial or parallel execution using below scripts:
$ORACLE_HOME\RDBMS\ADMIN\utlxpls.sql - plan table output with serial execution.
$ORACLE_HOME\RDBMS\ADMIN\utlxplp.sql - plan table output with parallel execution.


The output is shows you what the query optimzer expects.It gives us an idea on choosing correct access path.

SQL*Trace/TKPROF:

Open a new SQL*Plus:
- open a new db session
- alter session set sql_trace true;
- run your query
- disconnect session
- identify your trace file in the server directory as specified in the parameter - user_dump_dest
select value from v$parameter where name = 'user_dump_dest'; 

OR 
Show parameter user_dump_dest;
- on your operating system: tkprof query_op.txt
query_op.txt will now give you valuable information on what has actually happened.

By comparing the output from explain plan with the output from TKPROF, We can able to identify problematic areas of the query.