SlideShare ist ein Scribd-Unternehmen logo
1 von 44
Performance Tuning
SQL and PL/SQL
Presentation By
P. S. Mukesh Yadav and Suresh C Mishra

11/10/2008

VESL TECHNOLOGIES LTD

1
• Tips in SQL
• Tips in PL/SQL

11/10/2008

Topics

VESL TECHNOLOGIES LTD

2
Tips in SQL
• Limit the use of sub queries; often they can
be replaced with a JOIN.
• Use EXISTS and more specific conditions in
the where clause instead of the DISTINCT
clause to eliminate duplicates.
• Never Mix Datatypes.Use character and
number appropriately according to the
usage.
11/10/2008

VESL TECHNOLOGIES LTD

3
Example of First Tip…
SELECT ename FROM emp
WHERE deptno IN
(SELECT deptno FROM emp
WHERE comm = 10
AND sal>4000);

SELECT ename FROM emp d
WHERE EXISTS (
SELECT e.deptno FROM emp e
WHERE 1=1
and e.deptno = d.deptno
AND e.comm = 10
AND e.sal > 4000);

SELECT ename FROM emp e,dept d
WHERE e.deptno = d.deptno
AND e.comm = 10
AND e.sal > 4000

11/10/2008

VESL TECHNOLOGIES LTD

4
Example of Second Tip…
Ex :

SELECT
ooha.order_type_id,ooha.order_
number
FROM oe_order_headers_all
ooha
WHERE EXISTS (SELECT 1
FROM oe_transaction_types_tl
ottt
WHERE ooha.order_type_id =
ottt.transaction_type_id);

select distinct
ooha.order_type_id,ooha.
order_number
from
oe_order_headers_all
ooha,oe_transaction_type
s_tl ottt
where
ooha.order_type_id =
ottt.transaction_type_id

11/10/2008

VESL TECHNOLOGIES LTD

5
Tips in SQL Contd..
• Avoid the use of NOT IN.Instead, a NOT
EXISTS sub query may run faster.
• Use Minus Operator instead of Exists sub
queries(Not in and Not Exists) results in
faster execution plan.
• Rewrite (negative(ex : not in)) sub queries as
outer joins to improve performance.
• Never use numbers in sorting.
11/10/2008

VESL TECHNOLOGIES LTD

6
Contd….
Ex :
SELECT * FROM EMP A WHERE DEPTNO NOT
IN (SELECT DEPTNO FROM DEPT WHERE
DEPTNO=A.DEPTNO)

select * from
ap_invoices_all where invoice_id not in(select
invoice_id from
ap_invoice_payments_all
)

11/10/2008

SELECT * FROM EMP A WHERE NOT EXISTS
(SELECT 1 FROM DEPT WHERE
DEPTNO=A.DEPTNO)

SELECT aia.invoice_id
FROM ap_invoices_all aia,
ap_invoice_payments_all aipa
WHERE aia.invoice_id = aipa.invoice_id(+);

VESL TECHNOLOGIES LTD

7
Contd….
Ex :

select * from
ap_invoices_all where
invoice_id not in(select
invoice_id from
ap_invoice_payments_all
)

SELECT * FROM
ap_invoices_all aia WHERE NOT
EXISTS (SELECT * FROM
ap_invoice_payments_all WHERE
invoice_id = aia.invoice_id)

select * from
ap_invoices_all
where invoice_id in
(select invoice_id from
ap_invoices_all
minus
select invoice_id from
ap_invoice_payments_all
)

11/10/2008

VESL TECHNOLOGIES LTD

8
Tips in SQL Contd..

• Avoid running a query in a loop.Make use of
UNION and execute it at the end of the loop.
• Avoid the LIKE predicate.Always replace
LIKE with = when appropriate.
• Use LIKE rather than the SUBSTR function.
• Avoid Where Column Like '%string'.
• Consider Indexes for Max() and Min().

11/10/2008

VESL TECHNOLOGIES LTD

9
Contd….
• Ex :

select * from
dept where dname like
'RESEARCH'

select ename from emp
where ename like ‘X%’

11/10/2008

select * from
dept where dname =
'RESEARCH'

select ename from emp
where
substr(ename,1,1) = ‘X’

VESL TECHNOLOGIES LTD

10
Tips in SQL Contd..
• Use Decode and Case for complex
aggregations.
• Use WHERE instead of HAVING.
• Use UNION ALL instead of UNION.
• Prefer using GROUP BY only when any
aggregate functions are present.
• Avoid usage of ‘TO_CHAR‘ and use ‘TRUNC‘
instead.
11/10/2008

VESL TECHNOLOGIES LTD

11
Contd….
• Ex :

SELECT E_Name FROM
Employees_Norway UNION SELECT
E_Name FROM Employees_USA

select *
from emp
where to_char(emp_joindate,
’mmddyyyy’) < to_char(sysdat
e,’mmddyyyy’)

11/10/2008

Ex :
SELECT E_Name FROM
Employees_Norway UNION ALL
SELECT E_Name FROM
Employees_USA

select *
from emp
where emp_joindate < trunc
(sysdate)

VESL TECHNOLOGIES LTD

12
SELECT COUNT(*)
FROM emp
WHERE status = 'Y'
AND emp_name LIKE
'SMITH%';

SELECT COUNT(*)
FROM emp
WHERE status = 'N'
AND emp_name LIKE
'SMITH%';

Ex :
SELECT DEPTID,
SUM(SALARY)
FROM EMP
GROUP BY DEPTID
HAVING DEPTID = 100;

SELECT COUNT(DECODE(status, 'Y', 'X', NULL))
Y_count,
COUNT(DECODE(status, 'N', 'X', NULL))
N_count
FROM emp
WHERE emp_name LIKE 'SMITH%';

11/10/2008

SELECT DEPTID,
SUM(SALARY)
FROM EMP
WHERE DEPTID = 100
GROUP BY DEPTID;

VESL TECHNOLOGIES LTD

13
Contd….
SELECT COUNT (*)
FROM emp
WHERE sal < 2000;

SELECT COUNT (*)
FROM emp
WHERE sal BETWEEN 2000
AND 4000;

SELECT COUNT (*)
FROM emp
WHERE sal>4000;

SELECT COUNT (CASE WHEN sal < 2000
THEN 1 ELSE null END) count1,
COUNT (CASE WHEN sal BETWEEN 2001 AND 4000
THEN 1 ELSE null END) count2,
COUNT (CASE WHEN sal > 4000
THEN 1 ELSE null END) count3
FROM emp;

11/10/2008

VESL TECHNOLOGIES LTD

14
Tips in SQL Contd..
• Use NVL to check against a number instead
of the NULL value.
• Use <= 99 and >= 1 instead of < 100 and > 0
for faster comparisons.
• Avoid using functions on indexed columns.
• Use SET TIMING ON to test the execution
time of a slow query.
• Use EXPLAIN PLAN to view the execution
steps of a slow query.
11/10/2008

VESL TECHNOLOGIES LTD

15
Contd….
• Ex :
select cdl.* from
cs_incidents_all_b ciab,
jtf_tasks_b jtb,
jtf_task_assignments jta,
csf_debrief_headers cdh,
csf_debrief_lines cdl
where ciab.incident_id = jtb.source_object_id
and jtb.task_id = jta.task_id
and jta.task_assignment_id = cdh.task_assignment_id
and cdh.debrief_header_id = cdl.debrief_header_id
and ciab.incident_number = '1097852'

11/10/2008

VESL TECHNOLOGIES LTD

16
Contd….
Operation
SELECT STATEMENT Optimizer Mode=CHOOSE
TABLE ACCESS BY INDEX ROWID
NESTED LOOPS
NESTED LOOPS
NESTED LOOPS
NESTED LOOPS
TABLE ACCESS BY INDEX ROWID
INDEX UNIQUE SCAN
TABLE ACCESS FULL
TABLE ACCESS BY INDEX ROWID
INDEX RANGE SCAN
TABLE ACCESS BY INDEX ROWID
INDEX RANGE SCAN
INDEX RANGE SCAN

11/10/2008

Object Name

CSF_DEBRIEF_LINES

CS_INCIDENTS_ALL_B
CS_INCIDENTS_U2
JTF_TASKS_B
JTF_TASK_ASSIGNMENTS
JTF_TASK_ASSIGNMENTS_N2
CSF_DEBRIEF_HEADERS
CSF_DEBRIEF_HEADERS_N1
CSF_DEBRIEF_LINES_N1

Rows Bytes
4
3
4
1
1
1
1
1
1
1
2
1
1
4

Cost Object Node In/Out

PStart

PStop

1134
3
1134
1131
1129
1126
4
2
10 1122
20
3
2
14
2
1
2

414
776
56
42
22
12

VESL TECHNOLOGIES LTD

17
Contd….
Ex :

select sys.resourceID,
sys.client_version0
from dbo.v_R_System as sys
where UPPER(netbios_name0) =
'COMPUTER'

select sys.resourceID,
sys.client_version0
from dbo.v_R_System as sys
where netbios_name0 =
'COMPUTER’

11/10/2008

VESL TECHNOLOGIES LTD

18
Tips in SQL Contd..
• Remove unnecessary large full table scans.
• Minimize outer joins and use them only if
necessary.
• Only use CHAR for data that must be an exact
length such as phone numbers or gender
identification fields because it pads variable
length data with white-space that will cause
string comparisons to fail.

11/10/2008

VESL TECHNOLOGIES LTD

19
Tips in SQL Contd..
• Use an indexed column or primary key
column when counting rows.
• Avoid unnecessary sorting.
• Always better to use base tables instead of
views.
• If a view is in a query,try to optimize the view
first before optimizing the query.
• Always place all the hard-coded values at the
end of a query.
11/10/2008

VESL TECHNOLOGIES LTD

20
Contd….
• Ex :
select count(*)
From emp

11/10/2008

select
count(emp _id) from
emp;

VESL TECHNOLOGIES LTD

21
Tips in SQL Contd..
•
•
•
•
•

Using the indexes carefully.
Position of Tables in the proper order.
Sequence of Joins in the Where Clause.
Put the queries as simple as possible.
Avoid using of NOT when ever dealing with
Indexed Columns.
• Avoid usage of ! Operator use > instead.
• Always use a column list in your INSERT
statements.
11/10/2008

VESL TECHNOLOGIES LTD

22
Contd….
Ex :
select *
from emp
where emp_id !=
007

INSERT INTO
EuropeanCountries
VALUES (1, 'Ireland')

11/10/2008

select *
from emp
where emp_id > 0

INSERT INTO
EuropeanCountries
(CountryID,
CountryName)
VALUES (1, 'England')

VESL TECHNOLOGIES LTD

23
Tips in SQL Contd..

• Make Use of rownum.
• To Check Duplicate Data in a Query.

• Use SQL standards and conventions to
reduce parsing.
• Make sure to do INSERT,UPDATE and DELETE
operations prior checking the data with a
SELECT statement with proper conditions in
the where clause.
• Recheck whether any joins are missing.
11/10/2008

VESL TECHNOLOGIES LTD

24
Ex :

Contd….
SELECT DOCUMENT_NO,COUNT(*)
FROM documents
GROUP BY DOCUMENT_NO
HAVING
COUNT(*) > 1;

11/10/2008

VESL TECHNOLOGIES LTD

25
Tips in SQL Contd..

• Use Column Names instead of * in a SELECT
Statement.
• Use an index on the most queried columns in
SELECT, WHERE, and JOIN statements.
• Use BETWEEN instead of IN.
• Make sure all joined tables have a matching
join condition.
• Make sure columns selected from multiple
tables are dereferenced by an alias.
11/10/2008

VESL TECHNOLOGIES LTD

26
Contd….
• Ex :
SELECT empno FROM emp WHERE
empno IN (508858, 508859,
508860, 508861,508862, 508863,
508864)

11/10/2008

SELECT empno FROM emp WHERE
empno BETWEEN 508858 and
508864

VESL TECHNOLOGIES LTD

27
Tips in SQL Contd..
•
•
•
•
•

Call Functions in SQL Queries.
Minimize Table Lookups in a Query.
Use where instead of Order By.
Never do a calculation on an indexed column.
Give the names from _tl tables instead of
hard-coding the id.Ex : Status
• Add Who Columns for any new table created
for easier archiving.

11/10/2008

VESL TECHNOLOGIES LTD

28
Contd….
Ex :
select tab_name from tables where
tab_name = (select tab_name from
tab_columns where version = 604) and
db_ver = (select db_ver from
tab_columns where version = 604)

update emp set emp_cat = (select
max(category) from
emp_categories),sal = (select
max(salrange) from
emp_categories)where emp_dept =
20

11/10/2008

select tab_name from tables where
(tab_name, db_ver) = (select
tab_name,db_ver from tab_columns
where version = 604)

update emp set (emp_cat , sal )=
(select max(category),
max(salrange) from
emp_categories) Where
emp_dept = 20

VESL TECHNOLOGIES LTD

29
Contd….
Ex :
select * from emp
where
sal*12>25000

11/10/2008

select * from emp
where
sal>25000/12

VESL TECHNOLOGIES LTD

30
Tips in SQL Contd..
• Use sub queries and joins instead of multiple
separate queries.
• Use >= instead of >
• Use Union in place of OR for Indexed
Columns.
• Use the EXISTS clause or = operator instead
of the IN clause whenever you need a single
record.

11/10/2008

VESL TECHNOLOGIES LTD

31
Contd….
Ex :
select * from
oe_order_headers_all
where order_number>3

11/10/2008

select * from
oe_order_headers_all
where order_number>=4

VESL TECHNOLOGIES LTD

32
Tips in SQL Contd..
• Avoid IS NULL & IS NOT NULL on Indexed
Columns.Use >=0 if necessary.
• Use a system generated ID as the primary
key to avoid duplicates.
• Use Optimizer Hints.
• Generate a tkprof and observe the trace file.
• Focus on critical transactions(Production
Data)..

11/10/2008

VESL TECHNOLOGIES LTD

33
Contd….
Ex :

11/10/2008

/*+index(t1) index(t2)……..index(tn)*/
/*+FULL(t1) FULL(t2)……FULL(tn)*/
/*+OPTIMIZER_GOAL=CHOOSE*/
/*+OPTIMIZER_GOAL=FIRST_ROWS*/
/*+ORDERED*/
/*+RULE*/
/*+OPTIMIZER_GOAL=ALL_ROWS*/
/*+OPTIMIZER_GOAL=ORDERED*/
/*+OPTIMIZER_GOAL=RULE*/
/*+USE_NL(t1,t2,t3,t4,t5,t6) ORDERED */

VESL TECHNOLOGIES LTD

34
Tips in PL/SQL
•
•
•
•

Break the piece of code into small blocks to
achieve modularity.
Grouping related logic as a single block.
Use Packages for each major functionality.
Avoid hard-coding.

11/10/2008

VESL TECHNOLOGIES LTD

35
Tips in PL/SQL Contd..

• Follow PL/SQL Coding standards for
procedures, variables, table types,rec types,
functions and packages.
• Encapsulate your SQL.
• Avoid repeating the SQL in different places.
• Exception Handling.
• Usage of Bind Variables and Global Variables.
• Make Function Calls efficiently.

11/10/2008

VESL TECHNOLOGIES LTD

36
Tips in PL/SQL Contd..
• Use BULK COLLECT when there is huge
amount of data to be fetched from a select
query in a cursor.
• Use FOR ALL.
• Use Bulk Bind.
• Reorder Conditional Tests to put the Least
Expensive First.
• Logic in a simplest way.
• Usage of End Labels.
• Avoid usage of mutating tables.
11/10/2008

VESL TECHNOLOGIES LTD

37
• Ex :

11/10/2008

Contd….

CREATE OR REPLACE FUNCTION get_a_mess_o_emps
(deptno_in IN dept.depno%TYPE)
RETURN emplist_t
IS
emplist emplist_t := emplist_t();
TYPE numTab IS TABLE OF NUMBER;
TYPE charTab IS TABLE OF VARCHAR2(12);
TYPE dateTab IS TABLE OF DATE;
enos numTab;
names charTab;
hdates dateTab;
BEGIN
SELECT empno, ename, hiredate
BULK COLLECT INTO enos, names, hdates
FROM emp
WHERE deptno = deptno_in;
emplist.EXTEND(enos.COUNT);
FOR i IN enos.FIRST..enos.LAST
LOOP
emplist(i) := emp_t(enos(i),
names(i), hiredates(i));
END LOOP;
RETURN emplist;
END;
VESL TECHNOLOGIES LTD

38
Contd….
• Ex :
PROCEDURE reality_meets_dotcoms (deptlist dlist_t)
IS
BEGIN
FORALL aDept IN deptlist.FIRST..deptlist.LAST
DELETE emp WHERE deptno = deptlist(aDept);
END;

11/10/2008

VESL TECHNOLOGIES LTD

39
Tips in PL/SQL Contd..
• Use Anchored declaration during declaration of
variables.
• Reduce N/w traffic.
• Make use of ‘user_’ tables or views in the
custom packages.
• Use Standard Public Apis.
• Group related sub-programs in a Package.
• Make use of Optimizer Hints.

11/10/2008

VESL TECHNOLOGIES LTD

40
Tips in PL/SQL Contd..
• Make use of the apis being called in the
standard forms of Oracle Applications.
• Minimize Datatype Conversions.
• Use of CASE statements, is a much better
alternative to nested if-elsif statements.
• Declare Local Variables – Oracle Datatype.
• Make Loops as Efficient as Possible.

11/10/2008

VESL TECHNOLOGIES LTD

41
Tips in PL/SQL Contd..
• Use of pls_integer to declare integers and
binary_float or binary_double for floating
point.
• Replace and simplify IF statements with
Boolean expressions.
• Never exit from a FOR or WHILE loops.
• Never declare the FOR loop index (either an
integer or a record).
• Minimize the function Calls.
11/10/2008

VESL TECHNOLOGIES LTD

42
Contd……
•
•
•
•

Unit Testing.
Debugging.
Analysis on the Exceptions.
Test Cases.

11/10/2008

VESL TECHNOLOGIES LTD

43
Contd…

Thank You

11/10/2008

VESL TECHNOLOGIES LTD

44

Weitere ähnliche Inhalte

Was ist angesagt?

Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Achmad Solichin
 
Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)Achmad Solichin
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsSalman Memon
 
Producing Readable Output with iSQL*Plus - Oracle Data Base
Producing Readable Output with iSQL*Plus - Oracle Data BaseProducing Readable Output with iSQL*Plus - Oracle Data Base
Producing Readable Output with iSQL*Plus - Oracle Data BaseSalman Memon
 
Oracle SQL, PL/SQL best practices
Oracle SQL, PL/SQL best practicesOracle SQL, PL/SQL best practices
Oracle SQL, PL/SQL best practicesSmitha Padmanabhan
 
Subqueries -Oracle DataBase
Subqueries -Oracle DataBaseSubqueries -Oracle DataBase
Subqueries -Oracle DataBaseSalman Memon
 
How to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better PerformanceHow to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better Performanceoysteing
 
Advanced functions in PL SQL
Advanced functions in PL SQLAdvanced functions in PL SQL
Advanced functions in PL SQLHosein Zare
 
1. dml select statement reterive data
1. dml select statement reterive data1. dml select statement reterive data
1. dml select statement reterive dataAmrit Kaur
 
Les08 (manipulating data)
Les08 (manipulating data)Les08 (manipulating data)
Les08 (manipulating data)Achmad Solichin
 
Single-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSingle-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSalman Memon
 
Where conditions and Operators in SQL
Where conditions and Operators in SQLWhere conditions and Operators in SQL
Where conditions and Operators in SQLRaajendra M
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functionsNitesh Singh
 
How to analyze and tune sql queries for better performance webinar
How to analyze and tune sql queries for better performance webinarHow to analyze and tune sql queries for better performance webinar
How to analyze and tune sql queries for better performance webinaroysteing
 
Forall & bulk binds
Forall & bulk bindsForall & bulk binds
Forall & bulk bindsNawaz Sk
 

Was ist angesagt? (20)

Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)
 
Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)
 
plsql Les07
plsql Les07 plsql Les07
plsql Les07
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT Statements
 
Producing Readable Output with iSQL*Plus - Oracle Data Base
Producing Readable Output with iSQL*Plus - Oracle Data BaseProducing Readable Output with iSQL*Plus - Oracle Data Base
Producing Readable Output with iSQL*Plus - Oracle Data Base
 
Oracle SQL, PL/SQL best practices
Oracle SQL, PL/SQL best practicesOracle SQL, PL/SQL best practices
Oracle SQL, PL/SQL best practices
 
Explain that explain
Explain that explainExplain that explain
Explain that explain
 
Subqueries -Oracle DataBase
Subqueries -Oracle DataBaseSubqueries -Oracle DataBase
Subqueries -Oracle DataBase
 
How to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better PerformanceHow to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better Performance
 
Advanced functions in PL SQL
Advanced functions in PL SQLAdvanced functions in PL SQL
Advanced functions in PL SQL
 
1. dml select statement reterive data
1. dml select statement reterive data1. dml select statement reterive data
1. dml select statement reterive data
 
Les08 (manipulating data)
Les08 (manipulating data)Les08 (manipulating data)
Les08 (manipulating data)
 
Les07
Les07Les07
Les07
 
Single-Row Functions in orcale Data base
Single-Row Functions in orcale Data baseSingle-Row Functions in orcale Data base
Single-Row Functions in orcale Data base
 
Where conditions and Operators in SQL
Where conditions and Operators in SQLWhere conditions and Operators in SQL
Where conditions and Operators in SQL
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
 
Beg sql
Beg sqlBeg sql
Beg sql
 
Beg sql
Beg sqlBeg sql
Beg sql
 
How to analyze and tune sql queries for better performance webinar
How to analyze and tune sql queries for better performance webinarHow to analyze and tune sql queries for better performance webinar
How to analyze and tune sql queries for better performance webinar
 
Forall & bulk binds
Forall & bulk bindsForall & bulk binds
Forall & bulk binds
 

Ähnlich wie Sql performance vesl technologies

Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standardsAlessandro Baratella
 
Sql coding-standard-sqlserver
Sql coding-standard-sqlserverSql coding-standard-sqlserver
Sql coding-standard-sqlserverlochaaaa
 
Basic sqlstatements
Basic sqlstatementsBasic sqlstatements
Basic sqlstatementsAnjac
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sqlN.Jagadish Kumar
 
Using_PATSTAT_with_SQL_v2.8.pptx
Using_PATSTAT_with_SQL_v2.8.pptxUsing_PATSTAT_with_SQL_v2.8.pptx
Using_PATSTAT_with_SQL_v2.8.pptxssuserf96a5b
 
Basic sqlstatements
Basic sqlstatementsBasic sqlstatements
Basic sqlstatementsSubash T
 
Advanced MySQL Query Optimizations
Advanced MySQL Query OptimizationsAdvanced MySQL Query Optimizations
Advanced MySQL Query OptimizationsDave Stokes
 
Performance tuning
Performance tuningPerformance tuning
Performance tuningami111
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index TuningManikanda kumar
 
Sql query tuning or query optimization
Sql query tuning or query optimizationSql query tuning or query optimization
Sql query tuning or query optimizationVivek Singh
 
Les18[1]Interacting with the Oracle Server
Les18[1]Interacting with  the Oracle ServerLes18[1]Interacting with  the Oracle Server
Les18[1]Interacting with the Oracle Serversiavosh kaviani
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsNirav Shah
 
TSQL Coding Guidelines
TSQL Coding GuidelinesTSQL Coding Guidelines
TSQL Coding GuidelinesChris Adkin
 
Assg2 b 19121033-converted
Assg2 b 19121033-convertedAssg2 b 19121033-converted
Assg2 b 19121033-convertedSUSHANTPHALKE2
 
SQL Beginners anishurrehman.cloud.pdf
SQL Beginners anishurrehman.cloud.pdfSQL Beginners anishurrehman.cloud.pdf
SQL Beginners anishurrehman.cloud.pdfAnishurRehman1
 

Ähnlich wie Sql performance vesl technologies (20)

Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
 
Sql coding-standard-sqlserver
Sql coding-standard-sqlserverSql coding-standard-sqlserver
Sql coding-standard-sqlserver
 
Basic sqlstatements
Basic sqlstatementsBasic sqlstatements
Basic sqlstatements
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
 
Using_PATSTAT_with_SQL_v2.8.pptx
Using_PATSTAT_with_SQL_v2.8.pptxUsing_PATSTAT_with_SQL_v2.8.pptx
Using_PATSTAT_with_SQL_v2.8.pptx
 
Basic sqlstatements
Basic sqlstatementsBasic sqlstatements
Basic sqlstatements
 
Advanced MySQL Query Optimizations
Advanced MySQL Query OptimizationsAdvanced MySQL Query Optimizations
Advanced MySQL Query Optimizations
 
Performance tuning
Performance tuningPerformance tuning
Performance tuning
 
MySQL Query And Index Tuning
MySQL Query And Index TuningMySQL Query And Index Tuning
MySQL Query And Index Tuning
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 
Sql query tuning or query optimization
Sql query tuning or query optimizationSql query tuning or query optimization
Sql query tuning or query optimization
 
Les18[1]Interacting with the Oracle Server
Les18[1]Interacting with  the Oracle ServerLes18[1]Interacting with  the Oracle Server
Les18[1]Interacting with the Oracle Server
 
Dbms
DbmsDbms
Dbms
 
Oracle notes
Oracle notesOracle notes
Oracle notes
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tips
 
TSQL Coding Guidelines
TSQL Coding GuidelinesTSQL Coding Guidelines
TSQL Coding Guidelines
 
Mysql Optimization
Mysql OptimizationMysql Optimization
Mysql Optimization
 
Assg2 b 19121033-converted
Assg2 b 19121033-convertedAssg2 b 19121033-converted
Assg2 b 19121033-converted
 
SQL Beginners anishurrehman.cloud.pdf
SQL Beginners anishurrehman.cloud.pdfSQL Beginners anishurrehman.cloud.pdf
SQL Beginners anishurrehman.cloud.pdf
 
Practical 03 (1).pptx
Practical 03 (1).pptxPractical 03 (1).pptx
Practical 03 (1).pptx
 

Mehr von Suresh Mishra

Fusion applications gl and ar suresh c-mishra
Fusion applications   gl and ar suresh c-mishraFusion applications   gl and ar suresh c-mishra
Fusion applications gl and ar suresh c-mishraSuresh Mishra
 
Fusion apps financials strategy - 20100721
Fusion apps   financials strategy - 20100721Fusion apps   financials strategy - 20100721
Fusion apps financials strategy - 20100721Suresh Mishra
 
Overview of fusion payables.v1
Overview of fusion payables.v1Overview of fusion payables.v1
Overview of fusion payables.v1Suresh Mishra
 
Fusion costing overview_high_level-final
Fusion costing overview_high_level-finalFusion costing overview_high_level-final
Fusion costing overview_high_level-finalSuresh Mishra
 
R12 purchasing presentation oa
R12 purchasing presentation oaR12 purchasing presentation oa
R12 purchasing presentation oaSuresh Mishra
 
R12 inventory features
R12 inventory featuresR12 inventory features
R12 inventory featuresSuresh Mishra
 
Debugging accounting r12
Debugging accounting r12Debugging accounting r12
Debugging accounting r12Suresh Mishra
 
Less14 3 e_loadmodule_4
Less14 3 e_loadmodule_4Less14 3 e_loadmodule_4
Less14 3 e_loadmodule_4Suresh Mishra
 
Less13 3 e_loadmodule_3
Less13 3 e_loadmodule_3Less13 3 e_loadmodule_3
Less13 3 e_loadmodule_3Suresh Mishra
 
Less12 3 e_loadmodule_2
Less12 3 e_loadmodule_2Less12 3 e_loadmodule_2
Less12 3 e_loadmodule_2Suresh Mishra
 
Less11 3 e_loadmodule_1
Less11 3 e_loadmodule_1Less11 3 e_loadmodule_1
Less11 3 e_loadmodule_1Suresh Mishra
 
Less10 2 e_testermodule_9
Less10 2 e_testermodule_9Less10 2 e_testermodule_9
Less10 2 e_testermodule_9Suresh Mishra
 
Less09 2 e_testermodule_8
Less09 2 e_testermodule_8Less09 2 e_testermodule_8
Less09 2 e_testermodule_8Suresh Mishra
 
Less08 2 e_testermodule_7
Less08 2 e_testermodule_7Less08 2 e_testermodule_7
Less08 2 e_testermodule_7Suresh Mishra
 
Less07 2 e_testermodule_6
Less07 2 e_testermodule_6Less07 2 e_testermodule_6
Less07 2 e_testermodule_6Suresh Mishra
 
Less06 2 e_testermodule_5
Less06 2 e_testermodule_5Less06 2 e_testermodule_5
Less06 2 e_testermodule_5Suresh Mishra
 
Less05 2 e_testermodule_4
Less05 2 e_testermodule_4Less05 2 e_testermodule_4
Less05 2 e_testermodule_4Suresh Mishra
 

Mehr von Suresh Mishra (20)

Fusion applications gl and ar suresh c-mishra
Fusion applications   gl and ar suresh c-mishraFusion applications   gl and ar suresh c-mishra
Fusion applications gl and ar suresh c-mishra
 
Fusion apps gl_001
Fusion apps gl_001Fusion apps gl_001
Fusion apps gl_001
 
Fusion apps financials strategy - 20100721
Fusion apps   financials strategy - 20100721Fusion apps   financials strategy - 20100721
Fusion apps financials strategy - 20100721
 
Overview of fusion payables.v1
Overview of fusion payables.v1Overview of fusion payables.v1
Overview of fusion payables.v1
 
Fusion costing overview_high_level-final
Fusion costing overview_high_level-finalFusion costing overview_high_level-final
Fusion costing overview_high_level-final
 
R12 purchasing presentation oa
R12 purchasing presentation oaR12 purchasing presentation oa
R12 purchasing presentation oa
 
R12 inventory features
R12 inventory featuresR12 inventory features
R12 inventory features
 
Oracle fixed assets
Oracle fixed assetsOracle fixed assets
Oracle fixed assets
 
Debugging accounting r12
Debugging accounting r12Debugging accounting r12
Debugging accounting r12
 
Oracle receivables
Oracle receivablesOracle receivables
Oracle receivables
 
Less14 3 e_loadmodule_4
Less14 3 e_loadmodule_4Less14 3 e_loadmodule_4
Less14 3 e_loadmodule_4
 
Less13 3 e_loadmodule_3
Less13 3 e_loadmodule_3Less13 3 e_loadmodule_3
Less13 3 e_loadmodule_3
 
Less12 3 e_loadmodule_2
Less12 3 e_loadmodule_2Less12 3 e_loadmodule_2
Less12 3 e_loadmodule_2
 
Less11 3 e_loadmodule_1
Less11 3 e_loadmodule_1Less11 3 e_loadmodule_1
Less11 3 e_loadmodule_1
 
Less10 2 e_testermodule_9
Less10 2 e_testermodule_9Less10 2 e_testermodule_9
Less10 2 e_testermodule_9
 
Less09 2 e_testermodule_8
Less09 2 e_testermodule_8Less09 2 e_testermodule_8
Less09 2 e_testermodule_8
 
Less08 2 e_testermodule_7
Less08 2 e_testermodule_7Less08 2 e_testermodule_7
Less08 2 e_testermodule_7
 
Less07 2 e_testermodule_6
Less07 2 e_testermodule_6Less07 2 e_testermodule_6
Less07 2 e_testermodule_6
 
Less06 2 e_testermodule_5
Less06 2 e_testermodule_5Less06 2 e_testermodule_5
Less06 2 e_testermodule_5
 
Less05 2 e_testermodule_4
Less05 2 e_testermodule_4Less05 2 e_testermodule_4
Less05 2 e_testermodule_4
 

Kürzlich hochgeladen

南新罕布什尔大学毕业证学位证成绩单-学历认证
南新罕布什尔大学毕业证学位证成绩单-学历认证南新罕布什尔大学毕业证学位证成绩单-学历认证
南新罕布什尔大学毕业证学位证成绩单-学历认证kbdhl05e
 
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...Authentic No 1 Amil Baba In Pakistan
 
Virtue ethics & Effective Altruism: What can EA learn from virtue ethics?
Virtue ethics & Effective Altruism: What can EA learn from virtue ethics?Virtue ethics & Effective Altruism: What can EA learn from virtue ethics?
Virtue ethics & Effective Altruism: What can EA learn from virtue ethics?Mikko Kangassalo
 
integrity in personal relationship (1).pdf
integrity in personal relationship (1).pdfintegrity in personal relationship (1).pdf
integrity in personal relationship (1).pdfAmitRout25
 
(南达科他州立大学毕业证学位证成绩单-永久存档)
(南达科他州立大学毕业证学位证成绩单-永久存档)(南达科他州立大学毕业证学位证成绩单-永久存档)
(南达科他州立大学毕业证学位证成绩单-永久存档)oannq
 
Module-2-Lesson-2-COMMUNICATION-AIDS-AND-STRATEGIES-USING-TOOLS-OF-TECHNOLOGY...
Module-2-Lesson-2-COMMUNICATION-AIDS-AND-STRATEGIES-USING-TOOLS-OF-TECHNOLOGY...Module-2-Lesson-2-COMMUNICATION-AIDS-AND-STRATEGIES-USING-TOOLS-OF-TECHNOLOGY...
Module-2-Lesson-2-COMMUNICATION-AIDS-AND-STRATEGIES-USING-TOOLS-OF-TECHNOLOGY...JeylaisaManabat1
 
Spiritual Life Quote from Shiva Negi
Spiritual Life Quote from Shiva Negi Spiritual Life Quote from Shiva Negi
Spiritual Life Quote from Shiva Negi OneDay18
 
Inspiring Through Words Power of Inspiration.pptx
Inspiring Through Words Power of Inspiration.pptxInspiring Through Words Power of Inspiration.pptx
Inspiring Through Words Power of Inspiration.pptxShubham Rawat
 

Kürzlich hochgeladen (8)

南新罕布什尔大学毕业证学位证成绩单-学历认证
南新罕布什尔大学毕业证学位证成绩单-学历认证南新罕布什尔大学毕业证学位证成绩单-学历认证
南新罕布什尔大学毕业证学位证成绩单-学历认证
 
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...
 
Virtue ethics & Effective Altruism: What can EA learn from virtue ethics?
Virtue ethics & Effective Altruism: What can EA learn from virtue ethics?Virtue ethics & Effective Altruism: What can EA learn from virtue ethics?
Virtue ethics & Effective Altruism: What can EA learn from virtue ethics?
 
integrity in personal relationship (1).pdf
integrity in personal relationship (1).pdfintegrity in personal relationship (1).pdf
integrity in personal relationship (1).pdf
 
(南达科他州立大学毕业证学位证成绩单-永久存档)
(南达科他州立大学毕业证学位证成绩单-永久存档)(南达科他州立大学毕业证学位证成绩单-永久存档)
(南达科他州立大学毕业证学位证成绩单-永久存档)
 
Module-2-Lesson-2-COMMUNICATION-AIDS-AND-STRATEGIES-USING-TOOLS-OF-TECHNOLOGY...
Module-2-Lesson-2-COMMUNICATION-AIDS-AND-STRATEGIES-USING-TOOLS-OF-TECHNOLOGY...Module-2-Lesson-2-COMMUNICATION-AIDS-AND-STRATEGIES-USING-TOOLS-OF-TECHNOLOGY...
Module-2-Lesson-2-COMMUNICATION-AIDS-AND-STRATEGIES-USING-TOOLS-OF-TECHNOLOGY...
 
Spiritual Life Quote from Shiva Negi
Spiritual Life Quote from Shiva Negi Spiritual Life Quote from Shiva Negi
Spiritual Life Quote from Shiva Negi
 
Inspiring Through Words Power of Inspiration.pptx
Inspiring Through Words Power of Inspiration.pptxInspiring Through Words Power of Inspiration.pptx
Inspiring Through Words Power of Inspiration.pptx
 

Sql performance vesl technologies

  • 1. Performance Tuning SQL and PL/SQL Presentation By P. S. Mukesh Yadav and Suresh C Mishra 11/10/2008 VESL TECHNOLOGIES LTD 1
  • 2. • Tips in SQL • Tips in PL/SQL 11/10/2008 Topics VESL TECHNOLOGIES LTD 2
  • 3. Tips in SQL • Limit the use of sub queries; often they can be replaced with a JOIN. • Use EXISTS and more specific conditions in the where clause instead of the DISTINCT clause to eliminate duplicates. • Never Mix Datatypes.Use character and number appropriately according to the usage. 11/10/2008 VESL TECHNOLOGIES LTD 3
  • 4. Example of First Tip… SELECT ename FROM emp WHERE deptno IN (SELECT deptno FROM emp WHERE comm = 10 AND sal>4000); SELECT ename FROM emp d WHERE EXISTS ( SELECT e.deptno FROM emp e WHERE 1=1 and e.deptno = d.deptno AND e.comm = 10 AND e.sal > 4000); SELECT ename FROM emp e,dept d WHERE e.deptno = d.deptno AND e.comm = 10 AND e.sal > 4000 11/10/2008 VESL TECHNOLOGIES LTD 4
  • 5. Example of Second Tip… Ex : SELECT ooha.order_type_id,ooha.order_ number FROM oe_order_headers_all ooha WHERE EXISTS (SELECT 1 FROM oe_transaction_types_tl ottt WHERE ooha.order_type_id = ottt.transaction_type_id); select distinct ooha.order_type_id,ooha. order_number from oe_order_headers_all ooha,oe_transaction_type s_tl ottt where ooha.order_type_id = ottt.transaction_type_id 11/10/2008 VESL TECHNOLOGIES LTD 5
  • 6. Tips in SQL Contd.. • Avoid the use of NOT IN.Instead, a NOT EXISTS sub query may run faster. • Use Minus Operator instead of Exists sub queries(Not in and Not Exists) results in faster execution plan. • Rewrite (negative(ex : not in)) sub queries as outer joins to improve performance. • Never use numbers in sorting. 11/10/2008 VESL TECHNOLOGIES LTD 6
  • 7. Contd…. Ex : SELECT * FROM EMP A WHERE DEPTNO NOT IN (SELECT DEPTNO FROM DEPT WHERE DEPTNO=A.DEPTNO) select * from ap_invoices_all where invoice_id not in(select invoice_id from ap_invoice_payments_all ) 11/10/2008 SELECT * FROM EMP A WHERE NOT EXISTS (SELECT 1 FROM DEPT WHERE DEPTNO=A.DEPTNO) SELECT aia.invoice_id FROM ap_invoices_all aia, ap_invoice_payments_all aipa WHERE aia.invoice_id = aipa.invoice_id(+); VESL TECHNOLOGIES LTD 7
  • 8. Contd…. Ex : select * from ap_invoices_all where invoice_id not in(select invoice_id from ap_invoice_payments_all ) SELECT * FROM ap_invoices_all aia WHERE NOT EXISTS (SELECT * FROM ap_invoice_payments_all WHERE invoice_id = aia.invoice_id) select * from ap_invoices_all where invoice_id in (select invoice_id from ap_invoices_all minus select invoice_id from ap_invoice_payments_all ) 11/10/2008 VESL TECHNOLOGIES LTD 8
  • 9. Tips in SQL Contd.. • Avoid running a query in a loop.Make use of UNION and execute it at the end of the loop. • Avoid the LIKE predicate.Always replace LIKE with = when appropriate. • Use LIKE rather than the SUBSTR function. • Avoid Where Column Like '%string'. • Consider Indexes for Max() and Min(). 11/10/2008 VESL TECHNOLOGIES LTD 9
  • 10. Contd…. • Ex : select * from dept where dname like 'RESEARCH' select ename from emp where ename like ‘X%’ 11/10/2008 select * from dept where dname = 'RESEARCH' select ename from emp where substr(ename,1,1) = ‘X’ VESL TECHNOLOGIES LTD 10
  • 11. Tips in SQL Contd.. • Use Decode and Case for complex aggregations. • Use WHERE instead of HAVING. • Use UNION ALL instead of UNION. • Prefer using GROUP BY only when any aggregate functions are present. • Avoid usage of ‘TO_CHAR‘ and use ‘TRUNC‘ instead. 11/10/2008 VESL TECHNOLOGIES LTD 11
  • 12. Contd…. • Ex : SELECT E_Name FROM Employees_Norway UNION SELECT E_Name FROM Employees_USA select * from emp where to_char(emp_joindate, ’mmddyyyy’) < to_char(sysdat e,’mmddyyyy’) 11/10/2008 Ex : SELECT E_Name FROM Employees_Norway UNION ALL SELECT E_Name FROM Employees_USA select * from emp where emp_joindate < trunc (sysdate) VESL TECHNOLOGIES LTD 12
  • 13. SELECT COUNT(*) FROM emp WHERE status = 'Y' AND emp_name LIKE 'SMITH%'; SELECT COUNT(*) FROM emp WHERE status = 'N' AND emp_name LIKE 'SMITH%'; Ex : SELECT DEPTID, SUM(SALARY) FROM EMP GROUP BY DEPTID HAVING DEPTID = 100; SELECT COUNT(DECODE(status, 'Y', 'X', NULL)) Y_count, COUNT(DECODE(status, 'N', 'X', NULL)) N_count FROM emp WHERE emp_name LIKE 'SMITH%'; 11/10/2008 SELECT DEPTID, SUM(SALARY) FROM EMP WHERE DEPTID = 100 GROUP BY DEPTID; VESL TECHNOLOGIES LTD 13
  • 14. Contd…. SELECT COUNT (*) FROM emp WHERE sal < 2000; SELECT COUNT (*) FROM emp WHERE sal BETWEEN 2000 AND 4000; SELECT COUNT (*) FROM emp WHERE sal>4000; SELECT COUNT (CASE WHEN sal < 2000 THEN 1 ELSE null END) count1, COUNT (CASE WHEN sal BETWEEN 2001 AND 4000 THEN 1 ELSE null END) count2, COUNT (CASE WHEN sal > 4000 THEN 1 ELSE null END) count3 FROM emp; 11/10/2008 VESL TECHNOLOGIES LTD 14
  • 15. Tips in SQL Contd.. • Use NVL to check against a number instead of the NULL value. • Use <= 99 and >= 1 instead of < 100 and > 0 for faster comparisons. • Avoid using functions on indexed columns. • Use SET TIMING ON to test the execution time of a slow query. • Use EXPLAIN PLAN to view the execution steps of a slow query. 11/10/2008 VESL TECHNOLOGIES LTD 15
  • 16. Contd…. • Ex : select cdl.* from cs_incidents_all_b ciab, jtf_tasks_b jtb, jtf_task_assignments jta, csf_debrief_headers cdh, csf_debrief_lines cdl where ciab.incident_id = jtb.source_object_id and jtb.task_id = jta.task_id and jta.task_assignment_id = cdh.task_assignment_id and cdh.debrief_header_id = cdl.debrief_header_id and ciab.incident_number = '1097852' 11/10/2008 VESL TECHNOLOGIES LTD 16
  • 17. Contd…. Operation SELECT STATEMENT Optimizer Mode=CHOOSE TABLE ACCESS BY INDEX ROWID NESTED LOOPS NESTED LOOPS NESTED LOOPS NESTED LOOPS TABLE ACCESS BY INDEX ROWID INDEX UNIQUE SCAN TABLE ACCESS FULL TABLE ACCESS BY INDEX ROWID INDEX RANGE SCAN TABLE ACCESS BY INDEX ROWID INDEX RANGE SCAN INDEX RANGE SCAN 11/10/2008 Object Name CSF_DEBRIEF_LINES CS_INCIDENTS_ALL_B CS_INCIDENTS_U2 JTF_TASKS_B JTF_TASK_ASSIGNMENTS JTF_TASK_ASSIGNMENTS_N2 CSF_DEBRIEF_HEADERS CSF_DEBRIEF_HEADERS_N1 CSF_DEBRIEF_LINES_N1 Rows Bytes 4 3 4 1 1 1 1 1 1 1 2 1 1 4 Cost Object Node In/Out PStart PStop 1134 3 1134 1131 1129 1126 4 2 10 1122 20 3 2 14 2 1 2 414 776 56 42 22 12 VESL TECHNOLOGIES LTD 17
  • 18. Contd…. Ex : select sys.resourceID, sys.client_version0 from dbo.v_R_System as sys where UPPER(netbios_name0) = 'COMPUTER' select sys.resourceID, sys.client_version0 from dbo.v_R_System as sys where netbios_name0 = 'COMPUTER’ 11/10/2008 VESL TECHNOLOGIES LTD 18
  • 19. Tips in SQL Contd.. • Remove unnecessary large full table scans. • Minimize outer joins and use them only if necessary. • Only use CHAR for data that must be an exact length such as phone numbers or gender identification fields because it pads variable length data with white-space that will cause string comparisons to fail. 11/10/2008 VESL TECHNOLOGIES LTD 19
  • 20. Tips in SQL Contd.. • Use an indexed column or primary key column when counting rows. • Avoid unnecessary sorting. • Always better to use base tables instead of views. • If a view is in a query,try to optimize the view first before optimizing the query. • Always place all the hard-coded values at the end of a query. 11/10/2008 VESL TECHNOLOGIES LTD 20
  • 21. Contd…. • Ex : select count(*) From emp 11/10/2008 select count(emp _id) from emp; VESL TECHNOLOGIES LTD 21
  • 22. Tips in SQL Contd.. • • • • • Using the indexes carefully. Position of Tables in the proper order. Sequence of Joins in the Where Clause. Put the queries as simple as possible. Avoid using of NOT when ever dealing with Indexed Columns. • Avoid usage of ! Operator use > instead. • Always use a column list in your INSERT statements. 11/10/2008 VESL TECHNOLOGIES LTD 22
  • 23. Contd…. Ex : select * from emp where emp_id != 007 INSERT INTO EuropeanCountries VALUES (1, 'Ireland') 11/10/2008 select * from emp where emp_id > 0 INSERT INTO EuropeanCountries (CountryID, CountryName) VALUES (1, 'England') VESL TECHNOLOGIES LTD 23
  • 24. Tips in SQL Contd.. • Make Use of rownum. • To Check Duplicate Data in a Query. • Use SQL standards and conventions to reduce parsing. • Make sure to do INSERT,UPDATE and DELETE operations prior checking the data with a SELECT statement with proper conditions in the where clause. • Recheck whether any joins are missing. 11/10/2008 VESL TECHNOLOGIES LTD 24
  • 25. Ex : Contd…. SELECT DOCUMENT_NO,COUNT(*) FROM documents GROUP BY DOCUMENT_NO HAVING COUNT(*) > 1; 11/10/2008 VESL TECHNOLOGIES LTD 25
  • 26. Tips in SQL Contd.. • Use Column Names instead of * in a SELECT Statement. • Use an index on the most queried columns in SELECT, WHERE, and JOIN statements. • Use BETWEEN instead of IN. • Make sure all joined tables have a matching join condition. • Make sure columns selected from multiple tables are dereferenced by an alias. 11/10/2008 VESL TECHNOLOGIES LTD 26
  • 27. Contd…. • Ex : SELECT empno FROM emp WHERE empno IN (508858, 508859, 508860, 508861,508862, 508863, 508864) 11/10/2008 SELECT empno FROM emp WHERE empno BETWEEN 508858 and 508864 VESL TECHNOLOGIES LTD 27
  • 28. Tips in SQL Contd.. • • • • • Call Functions in SQL Queries. Minimize Table Lookups in a Query. Use where instead of Order By. Never do a calculation on an indexed column. Give the names from _tl tables instead of hard-coding the id.Ex : Status • Add Who Columns for any new table created for easier archiving. 11/10/2008 VESL TECHNOLOGIES LTD 28
  • 29. Contd…. Ex : select tab_name from tables where tab_name = (select tab_name from tab_columns where version = 604) and db_ver = (select db_ver from tab_columns where version = 604) update emp set emp_cat = (select max(category) from emp_categories),sal = (select max(salrange) from emp_categories)where emp_dept = 20 11/10/2008 select tab_name from tables where (tab_name, db_ver) = (select tab_name,db_ver from tab_columns where version = 604) update emp set (emp_cat , sal )= (select max(category), max(salrange) from emp_categories) Where emp_dept = 20 VESL TECHNOLOGIES LTD 29
  • 30. Contd…. Ex : select * from emp where sal*12>25000 11/10/2008 select * from emp where sal>25000/12 VESL TECHNOLOGIES LTD 30
  • 31. Tips in SQL Contd.. • Use sub queries and joins instead of multiple separate queries. • Use >= instead of > • Use Union in place of OR for Indexed Columns. • Use the EXISTS clause or = operator instead of the IN clause whenever you need a single record. 11/10/2008 VESL TECHNOLOGIES LTD 31
  • 32. Contd…. Ex : select * from oe_order_headers_all where order_number>3 11/10/2008 select * from oe_order_headers_all where order_number>=4 VESL TECHNOLOGIES LTD 32
  • 33. Tips in SQL Contd.. • Avoid IS NULL & IS NOT NULL on Indexed Columns.Use >=0 if necessary. • Use a system generated ID as the primary key to avoid duplicates. • Use Optimizer Hints. • Generate a tkprof and observe the trace file. • Focus on critical transactions(Production Data).. 11/10/2008 VESL TECHNOLOGIES LTD 33
  • 34. Contd…. Ex : 11/10/2008 /*+index(t1) index(t2)……..index(tn)*/ /*+FULL(t1) FULL(t2)……FULL(tn)*/ /*+OPTIMIZER_GOAL=CHOOSE*/ /*+OPTIMIZER_GOAL=FIRST_ROWS*/ /*+ORDERED*/ /*+RULE*/ /*+OPTIMIZER_GOAL=ALL_ROWS*/ /*+OPTIMIZER_GOAL=ORDERED*/ /*+OPTIMIZER_GOAL=RULE*/ /*+USE_NL(t1,t2,t3,t4,t5,t6) ORDERED */ VESL TECHNOLOGIES LTD 34
  • 35. Tips in PL/SQL • • • • Break the piece of code into small blocks to achieve modularity. Grouping related logic as a single block. Use Packages for each major functionality. Avoid hard-coding. 11/10/2008 VESL TECHNOLOGIES LTD 35
  • 36. Tips in PL/SQL Contd.. • Follow PL/SQL Coding standards for procedures, variables, table types,rec types, functions and packages. • Encapsulate your SQL. • Avoid repeating the SQL in different places. • Exception Handling. • Usage of Bind Variables and Global Variables. • Make Function Calls efficiently. 11/10/2008 VESL TECHNOLOGIES LTD 36
  • 37. Tips in PL/SQL Contd.. • Use BULK COLLECT when there is huge amount of data to be fetched from a select query in a cursor. • Use FOR ALL. • Use Bulk Bind. • Reorder Conditional Tests to put the Least Expensive First. • Logic in a simplest way. • Usage of End Labels. • Avoid usage of mutating tables. 11/10/2008 VESL TECHNOLOGIES LTD 37
  • 38. • Ex : 11/10/2008 Contd…. CREATE OR REPLACE FUNCTION get_a_mess_o_emps (deptno_in IN dept.depno%TYPE) RETURN emplist_t IS emplist emplist_t := emplist_t(); TYPE numTab IS TABLE OF NUMBER; TYPE charTab IS TABLE OF VARCHAR2(12); TYPE dateTab IS TABLE OF DATE; enos numTab; names charTab; hdates dateTab; BEGIN SELECT empno, ename, hiredate BULK COLLECT INTO enos, names, hdates FROM emp WHERE deptno = deptno_in; emplist.EXTEND(enos.COUNT); FOR i IN enos.FIRST..enos.LAST LOOP emplist(i) := emp_t(enos(i), names(i), hiredates(i)); END LOOP; RETURN emplist; END; VESL TECHNOLOGIES LTD 38
  • 39. Contd…. • Ex : PROCEDURE reality_meets_dotcoms (deptlist dlist_t) IS BEGIN FORALL aDept IN deptlist.FIRST..deptlist.LAST DELETE emp WHERE deptno = deptlist(aDept); END; 11/10/2008 VESL TECHNOLOGIES LTD 39
  • 40. Tips in PL/SQL Contd.. • Use Anchored declaration during declaration of variables. • Reduce N/w traffic. • Make use of ‘user_’ tables or views in the custom packages. • Use Standard Public Apis. • Group related sub-programs in a Package. • Make use of Optimizer Hints. 11/10/2008 VESL TECHNOLOGIES LTD 40
  • 41. Tips in PL/SQL Contd.. • Make use of the apis being called in the standard forms of Oracle Applications. • Minimize Datatype Conversions. • Use of CASE statements, is a much better alternative to nested if-elsif statements. • Declare Local Variables – Oracle Datatype. • Make Loops as Efficient as Possible. 11/10/2008 VESL TECHNOLOGIES LTD 41
  • 42. Tips in PL/SQL Contd.. • Use of pls_integer to declare integers and binary_float or binary_double for floating point. • Replace and simplify IF statements with Boolean expressions. • Never exit from a FOR or WHILE loops. • Never declare the FOR loop index (either an integer or a record). • Minimize the function Calls. 11/10/2008 VESL TECHNOLOGIES LTD 42
  • 43. Contd…… • • • • Unit Testing. Debugging. Analysis on the Exceptions. Test Cases. 11/10/2008 VESL TECHNOLOGIES LTD 43