Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

HOME About

Home SQLUs
QUERIES ORACLE
Privacy Policy ORACLE
Contact Us BI UNIX POSTGRESQL INTERVIEW Q/A

SQL WORLD
PARSE, BIND, OPTI MIZE, EXECUTE

PLSQL TUTORIALS SQL PERFORMANCE TUNING


Search … 
How to do Oracle PL SQL Performance Tuning | PL SQL performance
tuning
RECENT POSTS

PL SQL performance tuning :


What is Star Schema with real industry
In my previous articles i have given the brief idea of SQL peformance tuning.Now a step forward there are some example ?
basic steps for PL SQL performance tuning.It is important for every developer to tune the plsql code.PL SQL
performance tuning is very important while adding business logic in oracle.PLSQL engine uses the PLSQL What is Snow ake schema with industryTOP
optimizer to rearrange the code for better performance.In this article i will try to give the brief introduction of PL examples?
SQL performance tuning and what changes programmer needs to do for tuning the performance of PLSQL code. What are Common Table Expression(CTE)
with Examples?
Scenarios of PL SQL performance tuning : How to nd the Character set of Oracle
There are some speci c situations where user needs to do some changes of PL SQL performance tuning.I will try with examples?
to explain the different situations and how to tackle that situations and do performance tuning. How to create csv le using PL/SQL step by
step?
1.Datatype Conversion :
While using the datatype conversion PL SQL engine uses the implicit conversion of the datatype.User needs to
LIKE US ON FACEBOOK
avoid the implicit conversion of datatypes.So avoiding implicit conversion will improve the performance.

Program :

DECLARE
V_No NUMBER;
V_Char CHAR(10);
BEGIN
V_No := V_No + 50; -- converted implicitly; slow CATEGORIES
V_No := V_No + 50.0; -- not converted; fast
V_Char := 25; -- converted implicitly; slow
V_Char := TO_CHAR(25); -- converted explicitly; still slow
BI Tutorials
V_Char := '25'; -- not converted; fast
END; Business Analyst

Data Mining

Data Modeling Interview Questions


In above example user will get how to use the different datatypes in PL SQL. Simple example is when user uses
the Number datatype,the PL SQL engine always considers number datatype with one precision so try to use one ITIL Certi cation
precision while using simple integer number.Just like that while using character datatype try to avoid the OCA Preparation
TO_CHAR function and always use single quote.
Oracle 18c

Oracle Errors

Oracle Upgrade

PLSQL Tutorials

PostgreSQL

Roles and Responsibilities

Scrum Master

SQL Complex Queries

SQL Difference Between

SQL Interview Questions

SQL Interview Questions For Freshers


2.Make use of E cient SQL statements for PL SQL performance
SQL Joins
tuning:
SQL Performance Tuning
The PLSQL code will slow down because of the inef cient use of SQL statements. So make sure that user needs
to use the perfect SQL statement. SQL Practice Quiz

SQL Tutorials
A.Appropriate Index use:
Unix Interview Questions
User needs to create appropriate indexes on speci c column of the table. User should create the indexes on
columns in where condition of the statement. Unix Tutorials

1. User needs to create bit map indexes in case of distinct values in the table.
TAGS

Syntax:

Basic SQL Queries Complex View

Create bitmap index index_name on tablename(column_name); difference between simple view and complex
view

2. User needs to create simple index in case of non-distinct values. Equi join Inner Join

Logical table in SQL Non Equi Join


Syntax:
Oracle Outer Join PLSQL

Postgres Self Join Simple View


Create index index_name on tablename(column_name);
SQL SQL Joins SQL Views

3. User needs to create composite index in case of joins (hash tables). View Views in SQL

Syntax:

Create index index_name on tablename(column1,column2);

B.Up to date table stats:

User should gather the stats of tables after indexing the columns or changes in table structure. Kindly use the
subprogram of dbms_stats package to gather the stats.

Syntax for gathering stats using parallel 30 :

execute dbms_stats.gather_table_stats(ownname => ‘SIEBEL_ODS’, tabname => ‘Table_name’,


cascade => true, estimate_percent => DBMS_STATS.AUTO_SAMPLE_SIZE, method_opt => ‘FOR
ALL COLUMNS SIZE AUTO’ , degree=>30);

C.Analyze the execution plan of SQL statement and try to improve performance :

1.Check the explain plan and try to reduce cost using indexes.

2.Check the joins using trace facility.

D.Change the SQL statements whenever necessary :

Try to avoid group by as well as distinct statements in SQL.User needs to rewrite the SQL statements whenever
necessary.

 3.Use Latest Features of PLSQL :


User needs to use the latest PLSQL features to improve the performance of PLSQL
code.

1.Use of Forall Statement :

 Try to replace the use of insert,update and delete loops to Forall statements.

Example for Forall statement :

CREATE TABLE Employee2 AS SELECT * FROM Employee1;

The above statement will create the Employee2 table same as Employee1.

DECLARE

TYPE List_Number IS VARRAY(20) OF NUMBER;  — Define the List_Num Variable of VARRAY


TYPE

departments List_Number := NumList(101, 304, 705);  — department numbers

BEGIN

FORALL i IN departments.FIRST.. departments.LAST        —Defining forall

DELETE FROM Employee2 WHERE department_id = departments(i);

—Delete all data for Department number 101,304,705

COMMIT;

END;

2.Use of Bulk Collect :

Use of bulk collect in case of Select into clause.The Bulk collect statement improves the performance of the
PLSQL block. When reading-in lots of related rows, bulk collect can run 10x faster than a conventional loop.  This
tuning is possible because Oracle reduces context switches into a single operation.

Example of Bulk Collect :

DECLARE
TYPE test_bulk_collect IS TABLE OF s_srv_req%ROWTYPE;

V_test_bulkcollect test_bulk_collect;

CURSOR C_bulk_collect IS
SELECT *
FROM   OF s_srv_req;
BEGIN
OPEN C_bulk_collect;
LOOP
FETCH C_bulk_collect
BULK COLLECT INTO l_tab LIMIT 10000;     ——-10000 records collected with PLSQL engine
(No context switching)

DBMS_OUTPUT.put_line(V_test_bulkcollect.count || ‘ rows’);
EXIT WHEN C_bulk_collect %NOTFOUND;
END LOOP
CLOSE C_bulk_collect;
END;

4.Use of correct function calls :


User needs to use correct function calls in PLSQL blocks. If that function is called in Speci c SQL query then user
needs to create correct functional based index on that function.

Query :

Select count(row_id) from T_Service_request;

Kindly create index on rowed which is functional based.

Create index FI_T_Service_request on T_Service_request(Count(row_id));

5.Looping Practices :
It is very important to optimize the loops in PLSQL statement. Kindly follow the following practices while
working with loops.

1. Initialization outside the loop :

Kindly initialize the variables outside the loop to improve the performance of the PLSQL block.

2. Use of Forall statement :

Kindly use Forall statement in case of processing bulk DMLs.

3. Use of Union,Intersect,Minus,Hierarchical Queries :

Try to use union,intersect,minus as well as Hierarchical queries of ‘connect by’ to improve the performance.

4. Bulk Collect :

Process set of rows with bulk collect statement in spite of using loops.

6.String Function Practices :


1.Use In built String Functions :

Use multiple string functions  rather than use PLSQL block.Oracle has de ned set of pre de ned optimized string
functions like Replace,Instr,Substr,RPAD,LPAD etc.

2.Use of REGEX function :

If user wants to deal with regular expression make use of REGEX functions rather than writing whole code.

7.Use of AND OR Operator for PL SQL performance tuning :


Check whether the AND condition and or condition use in SQL statement. Try to avoid OR condition as it is full
scanning the table.

8.Use of PLS and Binary Datatypes :


Try to use the datatypes, which are using machine arithmetic datatypes like PLS, and binary arithmetic
datatypes.

PLS_INTEGER :

When user wants to de ne normal integer; He/She tries to de ne the datatype as INTEGER or NUMBER.
PLS_INTEGER requires less storage than Integer or Number datatype. So try to use PLS_INTEGER in case of
INTEGER or Number datatype.

BINARY_INTEGER:

The Binary_Integer is same as PLS_INTEGER but PLS_INTEGER is faster in Oracle 9i and Oracle 8i.

BINARY_DOUBLE / BINARY_FLOAT :

Kindly use BINARY_DOUBLE or BINARY_FLOAT for oating point number declaration in PLSQL.

9.Use of Execute Immediate statement and Cursor Variables:


Kindly use the ‘Execute immediate’ statement and cursor variables while developing the PLSQL blocks. Native
dynamic SQL code is more compact and much faster than calling the DBMS_SQL package.User can use ref cursor
to process dynamic SQL statements.

Example :

DECLARE

TYPE Employee_Cursor IS REF CURSOR;

Cv_employee  Employee_Cursor;              —-Open Ref Cursor

Name VARCHAR2(10);

Salary   NUMBER := 50000;

table_name VARCHAR2(30) := ’employee’;

BEGIN

OPEN Cv_employee FOR ‘SELECT Name, salary FROM ‘ || table_name ||

‘ WHERE salary > :s’ USING Salary;

CLOSE Cv_employee ;                       —-Close Ref Cursor

END;

10.Declaring Size of Variable :


Lot of programmers does not think about the size of variables declaration. You might need to allocate large
VARCHAR2 variables when you are not sure how big an expression result will be.So be generous while declaring
the size of variable.Check the functionality of that column and according to that specify the size of the variable.

11.Use of Packages in spite of Subprograms :


Kindly use the packages to encapsulate all the similar functionality subprograms. The whole package is loaded in
to same-shared memory IO so Subsequent calls to related subprograms in the package require no disk I/O, and
your code executes faster.

12.Use of DBMS_PROFILER for PL SQL performance tuning :


Try to use DBMS_PROFILER which will use to gather and save the runtime stats. The Pro ler traces the
execution of your program, computing the time spent at each line and in each subprogram. You can use the
collected data to improve performance.The Pro ler is useful when the same query we need to execute again and
again.

Lets take an example,when user needs the same report for speci c month again and again.The date range is same
so at that time user needs to set pro ler for that speci c query so that result will come fast.

Accepting pro le for Speci c Query :

 execute dbms_sqltune.accept_sql_profile(task_name => ‘Query Id’, task_owner =>


‘Username’,       replace => TRUE);

13.Use of Di erent hints in SQL statements :


It is pro table to use the hints like parallel and append in internal sql statements. Parallel and append hints
improves the performance of the query but it will take the CPU bandwidth. So take care while using the parallel
and append hints in the PLSQL code.

Syntax of Hint :

Select Employee_name from Employee;

16 X faster performance :

Select /*+ parallel(Employee,16)*/ Employee_name from Employee;

Always try to use append link while inserting the bunch of records.

Example :

INSERT /*+ APPEND */ INTO t1 SELECT * FROM S_ORG_EXT;

These are some simple methods to tune the performance of PLSQL code. Hope this article will be helpful to
everyone who really want to do the PL SQL performance tuning.There are lot of new techniques and
methodologies used to tune the performance of plsql application.

How to use the DBMS_Pro ler in PL SQL Developer Roles and What is Full Form of PL SQL? PL
PL SQL? PL SQL Tuning -1 Responsibilities | Resume for PL SQL Full Form with simple
SQL Developer examples

14 Replies to “How to do Oracle PL SQL


Performance Tuning | PL SQL performance tuning”

1. Bhagyam Babu says:

October 17, 2018 at 9:10 am


Got a chance to read this today.Quick notes and explained in very simple ways.
Thank you.

Amit S says:

October 17, 2018 at 2:59 pm


Thanks Bhagyam Babu

2. Muruganathan says:

December 7, 2018 at 11:48 am


Good and simple explanation.It is very easy to understand. Thank you.

Amit S says:

December 7, 2018 at 11:50 am


Thank u Muruganathan for lovely comments!!!

3. Vinothkumar says:

December 27, 2018 at 1:11 pm


Can you explain the AWR

Amit S says:

December 28, 2018 at 9:31 am


Sure Vinod!!! I will explain AWR report in detail in article soon!! Thanks for your valuable suggestions.

4. Ramya says:

January 2, 2019 at 10:16 am


It’s really an Excellent article.

Amit S says:

January 2, 2019 at 4:01 pm


Thanks Ramya for nice words..

5. sudheer kumar says:

April 20, 2019 at 1:15 pm


Thank you very much sir.. it’s really very much helpful to me..

Amit S says:

May 14, 2019 at 11:17 am


Thanks Sudheer for your nice help!!

6. srinu says:

January 2, 2020 at 5:35 pm


If user wants to deal with regular expression make use of REGEX functions rather than writing whole code.
in that regexp or regex right word ?

Amit S says:

June 5, 2020 at 1:52 pm


Yes ! You can use…kindly go through regular expression article

http://www.complexsql.com/regexp_like-examples/

7. Rama says:

June 17, 2020 at 7:50 pm


Good Information.

Amit S says:

July 27, 2020 at 8:26 pm


Thanks rama for good words!

Comments are closed.

Previous Next
What is SQL Create table with primary key OBIEE developer roles and responsibilities | OBIEE Resume

SQL WORLD | Designed by: Theme Freesia | WordPress | © Copyright All right reserved Home | About Us | Privacy Policy | Contact Us

You might also like