12/23/2012
Sangam 12
10/11/2012
A new book on Performance tuning
The presentation itself was very informative. His presentation style was awesome and as stylish as Rajnikanth. The man inspired me. So enhance my performance tuning knowledge, as a starting place would be none other than Rich's updated version of performance tuning book. Flipkart delivered the book on monday. Here is the newest collection to my books.
This is gonna keep me busy.
Regards,
Vijay
9/26/2012
Hierarchical Profiler - DBMS_HPROF
Initial setup:
PLSHPROF utility:
Reference
- Oracle 11g Advanced application developers guide
- Oracle 11g PLSQL packages and types reference.
- Www.oracle-base.com
9/20/2012
Dbms_Trace
Controlling Trace:
9/06/2012
Get_dependency
Dbms_utility provides a procedure to find various objects dependent on a particular object. This package has a procedure, get_dependency, which uses dbms_output procedure to print the list of objects which are dependent on the given object.
Syntax is:
DBMS_UTILITY.GET_DEPENDENCY
type IN VARCHAR2,
schema IN VARCHAR2,
name IN VARCHAR2);
Example usage: set serveroutput on;
BEGIN
dbms_utility.get_dependency('TABLE','HR','EMPLOYEES');
END;
This would list the objects dependent on employees table in HR schema as below in my hr schema: DEPENDENCIES ON HR.EMPLOYEES
------------------------------------------------------------------
*TABLE HR.EMPLOYEES()
* VIEW HR.EMP_DETAILS_VIEW()
* TRIGGER HR.SECURE_EMPLOYEES()
* TRIGGER HR.UPDATE_JOB_HISTORY()
* PROCEDURE HR.ANNUAL_SALARY()
Regards,Vijay
9/05/2012
Bulk Compilation of Invalid Objects
Compile_Schema procedure in dbms_utility package can be used to compile all the objects in a schema or only the invalid objects in a schema.
Syntax is:
DBMS_UTILITY.COMPILE_SCHEMA (
schema IN VARCHAR2,
compile_all IN BOOLEAN DEFAULT TRUE,
reuse_settings IN BOOLEAN DEFAULT FALSE);
If compile_all parameter is set as false, this procedure will compile only the invalid objects in the schema. Reuse_settings parameter is a boolean parameter to instruct oracle to reuse session settings for the objects compiled.Example usage:
This is an example usage to compile all invalid objects in HR schema:
BEGIN
DBMS_UTILITY.COMPILE_SCHEMA('HR',FALSE,TRUE);
END;
Regards,
Vijay
9/03/2012
DBMS_PROFILER
DBMS_PROFILER package, introduced in Oracle8i, provides a means to collect profiling data of plsql program units for performance improvement.
To use dbms_profiler package, we need to create the database tables which will store the profiler data. These tables can be created by running proftab.sql script which will be present in /rdbms/admin folder in the oracle installation folder.
This script creates:
- A sequence plsql_profiler_runnumber
- table plsql_profiler_data
- plsql_profiler_units
- plsql_profiler_runs
Lets do the initial setup:
1: CONN SYS/PASSWORD AS SYSDBA
2: @/home/oracle/app/oracle/product/11.2.0/dbhome_2/rdbms/admin/proftab.sql
3: CREATE PUBLIC SYNONYM plsql_profiler_runs FOR plsql_profiler_runs;
4: CREATE PUBLIC SYNONYM plsql_profiler_units FOR plsql_profiler_units;
5: CREATE PUBLIC SYNONYM plsql_profiler_data FOR plsql_profiler_data;
6: CREATE PUBLIC SYNONYM plsql_profiler_runnumber FOR plsql_profiler_runnumber;
7: GRANT SELECT ON plsql_profiler_runnumber TO PUBLIC;
8: GRANT SELECT, INSERT, UPDATE, DELETE ON plsql_profiler_data TO PUBLIC;
9: GRANT SELECT, INSERT, UPDATE, DELETE ON plsql_profiler_units TO PUBLIC;
10: GRANT SELECT, INSERT, UPDATE, DELETE ON plsql_profiler_runs TO PUBLIC;
The sets involved in profiling a plsql unit using dbms_profiling is:
- Start the profiling session using dbms_profiler.start_profiling
- Run the plsql unit to be profiled
- Stop the profiling session
- After this query the profiler tables we created earlier to check for which subprograms or line took maximum time to execute. Analyze why it took this long and modify as appropriate.
An Example:
1: create or replace procedure annual_salary
2: as
3: l_annual_salary NUMBER;
4: begin
5: for i in ( select employee_id, salary from employees)
6: loop
7: l_annual_salary := i.salary * 12;
8: end loop;
9: end annual_salary;
10: /
11: DECLARE
12: l_result BINARY_INTEGER;
13: BEGIN
14: l_result := DBMS_PROFILER.start_profiler(run_comment => 'annual_salary' );
15: annual_salary;
16: l_result := DBMS_PROFILER.stop_profiler;
17: END;
18: /
Run this query
SELECT runid, run_date, run_comment, run_total_time
FROM plsql_profiler_runs
where run_comment = 'annual_salary'
ORDER BY runid;
This gave below output:
------ --------- -------------------------------------------------- --------------
4 02-SEP-12 annual_salary 50000000
To analyze which subprogram or line in the plsql unit profiled took how much time :
SELECT u.runid, u.unit_number, u.unit_type, u.unit_owner, u.unit_name, d.line#, d.total_occur,
d.total_time, d.min_time, d.max_time
FROM plsql_profiler_units u JOIN plsql_profiler_data d ON u.runid = d.runid
AND u.unit_number = d.unit_number
WHERE u.runid = 4 ORDER BY u.unit_number, d.line#;
This produces below output:
------ ----------- -------------------- -------------- --------------- ---------- ----------- ---------- ---------- ----------
4 1 ANONYMOUS BLOCK <anonymous> <anonymous> 1 0 0 0 0
4 1 ANONYMOUS BLOCK <anonymous> <anonymous> 3 0 0 0 0
4 1 ANONYMOUS BLOCK <anonymous> <anonymous> 4 0 233173 233173 233173
4 1 ANONYMOUS BLOCK <anonymous> <anonymous> 5 2 82399 10519 71880
4 1 ANONYMOUS BLOCK <anonymous> <anonymous> 6 1 14025 14025 14025
4 1 ANONYMOUS BLOCK <anonymous> <anonymous> 7 0 0 0 0
4 2 PROCEDURE HR ANNUAL_SALARY 1 1 29804 8765 21038
4 2 PROCEDURE HR ANNUAL_SALARY 4 1 0 0 0
4 2 PROCEDURE HR ANNUAL_SALARY 6 109 16819193 7012 675853
4 2 PROCEDURE HR ANNUAL_SALARY 8 107 1495468 8765 120969
4 2 PROCEDURE HR ANNUAL_SALARY 9 1 0 0 0
RUNID UNIT_NUMBER UNIT_TYPE UNIT_OWNER UNIT_NAME LINE# TOTAL_OCCUR TOTAL_TIME MIN_TIME MAX_TIME
------ ----------- -------------------- -------------- --------------- ---------- ----------- ---------- ---------- ----------
4 2 PROCEDURE HR ANNUAL_SALARY 10 1 15778 15778 15778
12 rows selected.
The lines 6 and 8 has taken maximun time.
Using dbms_profiler, one can tune their plsql units iteratively.
Regards,
Vijay
8/04/2012
Logging Errors using Error back trace and Error Stack functions
DBMS_UTILITY.FORMAT_ERROR_BACKTRACE
RETURN VARCHAR2;
DBMS_UTILITY.FORMAT_ERROR_STACK
RETURN VARCHAR2;
Create or replace procedure proc0 is
Begin
RAISE NO_DATA_FOUND;
END;
CREATE OR REPLACE PROCEDURE PROC1 IS
BEGIN
PROC0();
END;
CREATE OR REPLACE PROCEDURE PROC2 IS
BEGIN
PROC1();
END;
CREATE OR REPLACE PROCEDURE PROC3 IS
BEGIN
PROC2();
END;
CREATE OR REPLACE PROCEDURE PROC4 IS
BEGIN
PROC3();
END;
/*CALL WITHOUT ERROR BACK TRACE*/
BEGIN
PROC4();
END;
Error report:
ORA-01403: no data found
ORA-06512: at "HR.PROC0", line 3
ORA-06512: at "HR.PROC1", line 3
ORA-06512: at "HR.PROC2", line 3
ORA-06512: at "HR.PROC3", line 3
ORA-06512: at "HR.PROC4", line 3
ORA-06512: at line 2
01403. 00000 - "no data found"
*Cause:
*Action:
Using Error back trace function, one can
redirect these same error message to the logging mechanism they use for their application.
In the below example I redirect the same into dbms_output.
/*CALL WITH ERROR BACK TRACE*/
SET SERVEROUTPUT ON;
BEGIN
PROC4();
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(‘-------------------------------------------’);
DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_ERROR_STACK);
DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
DBMS_OUTPUT.PUT_LINE(‘-------------------------------------------’);
END;
anonymous block completed
-------------------------------------------
ORA-01403: no data found
ORA-06512: at "HR.PROC0", line 3
ORA-06512: at "HR.PROC1", line 3
ORA-06512: at "HR.PROC2", line 3
ORA-06512: at "HR.PROC3", line 3
ORA-06512: at "HR.PROC4", line 3
ORA-06512: at line 2
-------------------------------------------
Cheers,
Vijay
7/25/2012
Passwords in SQL Developer
But there are some set of people who think saving the password is not the right thing to do. So if you are one among them, you can disable this feature by removing the Save Password Option by adding below text to sqldeveloper.conf file in SQLDEVELOPERBIN folder
AddVMOption -Dsqldev.savepasswd=false
Regards,
Vijay
7/23/2012
Display Line number in SQL Developer
I am sure this will save you considerable amount of time while debugging the code.
7/18/2012
SQL Developer - Formatting SQL
Once you have the sql to be formatted in worksheet ( for eg., as in below pic)
Sql before formatting |
Press the keys Ctrl + Shift + F7 together, Format properties dialog box as below would appear.
Once you click Apply button, the sql gets formatted as shown below:
7/16/2012
7/13/2012
SQL Developer Tip - 2 Colourful Girds in Table Data
Jobs Table Data without Grid pattern |
Add caption |
Now you can see your resultset of sql would look colourful
7/12/2012
SQL Developer Tips
4/14/2012
Installing Oracle XE
Oracle offers a free database verion called Oracle XE ( with a term of condition that it need to be used for educative purposes and a few limitations as well). One can download it from www.oracle.com
An installation guide was posted by Scott Wesley.
You can install this oracle express edition and play around your own db.
Cheers
Vijay
3/29/2012
File Handling for Beginners
There are two ways to specify the directory location of the file. First this can be mentioned against UTL_FILE_DIR parameter in the database initialisation file.Secondly one can create a directory object and use this object reference. This approach is introduced in Oracle 9i and is recommended to use.
Creation of Directory:
CREATE DIRECTORY <DIRECTORY_NAME> AS <DESTINATION FOLDER>
where<DIRECTORY_NAME> is the name of directory object. EG: UTLTEST
<DESTINATION FOLDER> is the name of the folder in the operating system
eg: /home/oracle/utltest
Let me create a test directory:
CONN SYS/<password> as SYSDBA
CREATE DIRECTORY UTLTEST AS '/home/oracle/utltest';
-- NOTE this directory path is applicable to unix environment
Metadata about this object can be found in ALL_DIRECTORIES data dictionary view.Once a directory is created, our schema user(HR in below examples) need read write permission granted. SYS DBA need to assign these privilege:
GRANT READ, WRITE ON DIRECTORY UTLTEST TO HR;
Opening a file:
Before we can read or write into a file we must open it. UTL_FILE function allows us to open a file in three mode Read , Write and append mode. This function returns a file handler of type FILE_TYPE in UTL_FILE package. It takes parameters file location, file name and mode to open the file. The file location parameter should represent a valid directory name. Combination of file location and file name need to give the full path of the file. As far as file mode is concerned, use 'R' for Read mode, 'W' for write mode and 'A' for append mode where we can read and write the file. For Read and Append mode the file must exist when we try to open the file.Writing to a file:
Reading from a file:
Example 1: Writing into a file:
DECLARE
FH UTL_FILE.FILE_TYPE;
L_STRING VARCHAR2(1000);
BEGIN
FH := UTL_FILE.FOPEN('UTLTEST','EMPLOYEES' || TO_CHAR(SYSDATE,'DDMMYYHHMISS') || '.CSV','W');
FOR EMP_CUR IN (SELECT * FROM EMPLOYEES)
LOOP
--L_STRING VARCHAR2(1000);
L_STRING := EMP_CUR.EMPLOYEE_ID || ','
|| EMP_CUR.FIRST_NAME || ','
|| EMP_CUR.LAST_NAME || ','
|| EMP_CUR.EMAIL || ','
|| EMP_CUR.PHONE_NUMBER || ','
|| EMP_CUR.HIRE_DATE || ','
|| EMP_CUR.JOB_ID || ','
|| EMP_CUR.SALARY || ','
|| EMP_CUR.COMMISSION_PCT || ','
|| EMP_CUR.MANAGER_ID || ','
|| EMP_CUR.DEPARTMENT_ID;
UTL_FILE.PUT_LINE(FH,L_STRING);
END LOOP;
UTL_FILE.FCLOSE(FH);
END;
Output:
PL/SQL procedure successfully completed.
You can now see a file created in /home/oracle/utltest directory.Example 2: Reading from a file:
DECLARE
FH UTL_FILE.FILE_TYPE;
L_LINE VARCHAR2(32767);
BEGIN
FH := UTL_FILE.FOPEN('UTLTEST','EMPLOYEES290312075110.CSV','R');
LOOP
UTL_FILE.GET_LINE(FH,L_LINE);
DBMS_OUTPUT.PUT_LINE(L_LINE);
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
UTL_FILE.FCLOSE(FH);
END;
Contents of the previously created file are displayed on the screen. Only a part of the output is shown below.
200,Jennifer,Whalen,JWHALEN,515.123.4444,17-SEP-87,AD_ASST,4400,,101,10
201,Michael,Hartstein,MHARTSTE,515.123.5555,17-FEB-96,MK_MAN,13000,,100,20
202,Pat,Fay,PFAY,603.123.6666,17-AUG-97,MK_REP,6000,,201,20
203,Susan,Mavris,SMAVRIS,515.123.7777,07-JUN-94,HR_REP,6500,,101,40
204,Hermann,Baer,HBAER,515.123.8888,07-JUN-94,PR_REP,10000,,101,70
205,Shelley,Higgins,SHIGGINS,515.123.8080,07-JUN-94,AC_MGR,12000,,101,110
206,William,Gietz,WGIETZ,515.123.8181,07-JUN-94,AC_ACCOUNT,8300,,205,110
Reference :
Cleanup scripts:
CONN SYS/<password> as sysdba
DROP DIRECTORY UTLTEST;