Code
Review
Optimize
SQL
Predeployment
Scheduled
Code Review
Benchmark
test
& SQL Scan
US
Development
Team
HK
Development
Team
Team
Review
Version
Control
QA: Regression
Testing and
Scalability/
Performance
Tuning
Management
Reporting
(Team Coding)
Manager
ZHA/SHA
Development
Team
Bad Code or Problematic SQL
Detected:
Return to Development
Code
Review
Successful
Producti
on
Database
Challenges
Problems
A Constantly-changing Environment
Hardware
Disk space
RAM
Network
CPU
Operating System settings
Database Server parameter settings
Database Design
Indexes on Database Tables
SQL statement
90%
60%
Performance Problems
According to
industrial
experts
60% of the
databases
performance
problems are
caused by
applications
Others
Application
Good
Very Bad
Very Good
(sometime for
simple SQL)
Why is it so hard?
You must know
SQL
Internal rewrite
& generation of
multiple
execution plans
Plan 1 cost=1000
Plan 2 cost=3000
Plan 1
Plan 2
Plan 3
Plan 4
How accurate is
the cost estimation?
Plan 3 cost=4000
Plan 4 cost= 500
Execution
Limitations of Optimizer
Path from table A to table B: which means that we open table A, looking
at each row to then use an index to search for matching rows in table B:
500,000 operations
Full
Table
Scan
A
8500 operations
B
Index Scan
B
Full Table
Scan
Index
Scan
A
Replace
Select * from A
where A.CITY in
(select B.City from B)
With
Joins
Select the smallest table
and/or smallest result set first.
SELECT * FROM A,B
WHERE A.STATE = CA
AND B.CITY =CONCORD
Avoid Views
For some complex SQL(Group function, Set
function), Views SQL is normally cannot be
merged to your upper layer SQL.
So, Oracle normally processes the view first,
creates a temp table which does not have
any indexes, and you cannot tune the view
because Oracle always handles the view in
the same manner.
Explain Plan
Indexed Fields
Indexed Fields
If you want the index used, dont
perform an operation on the field.
Replace
SELECT * from A
where SALARY +1000 = :NEWSALARY
with
SELECT * from A
where SALARY = :NEWSALARY -1000
Indexed Fields
Index will not be used when a function
is used.
SELECT * from A
where substr(name, 1, 3) = 'Wil'
Indexed Fields
WHERE clause
Avoid using
Indexed Fields
Sometimes DO disable the index
SELECT * FROM A
WHERE SALARY + 0 = '10000'
AND DEPT = 'IT'
SELECT * FROM A
WHERE EMP_SEX || '' = 'm'
Indexed Fields
Do not have default value set to NULL.
If it is a number field and lowest value is 0,
then:
Replace
SELECT * FROM A
WHERE NUMBER IS NOT NULL
with
Index Fields
Replace Outer Join with Union.
If both A.State and B.State have a unique indexed:
Replace
Optimization Hints
Learn to use optimization hints
Rule
First_Row
All_Rows
Ordered
Use_Hash
Use_Concat
No_Expand
Parallel
Driving_Site
Star
Star_Transformation
Index_Combine
Merge
No_Merge
ORDER BY clause
Use concatenated index.
If A table is indexed with lastname, firstname.
Replace
SELECT * FROM A
WHERE lastname = 'Smith'
ORDER BY firstname
With
SELECT * FROM A
WHERE lastname = 'Smith'
ORDER BY lastname, firstname
Use IN Sub-query
SELECT * FROM A
WHERE A.CITY IN
(SELECT B.CITY FROM B)
A.CITY is not indexed and B.CITY is indexed, and B has more rows than A.
SELECT * FROM A
WHERE EXISTS
(SELECT CITY FROM B
WHERE A.CITY|| = B.CITY)
A.CITY is indexed and B.CITY is indexed, and table B has more rows
than A.
Variables Problem
select * from Employee
where
(Emp_id>:range_lower or :range_lower is null)
and (A.key<:range_upper or :range_upper is null)
To enable index range search with a input range boundaries.
select * from Employee
where
Emp_id>=nvl(:range_lower,min , Emp_id)
and A.key<=nvl(:range_upper, A.key)
Transitivity
Select * from A,B,C
where A.key=B.key
and B.key=C.key
and C.key=text
To make Oracles optimizer more intelligent by increasing the transitivity(as
Oracles Optimizer cannot do very complicated transitivity improvement
internally)
select * from A
where (A.key1,A.key2) in
(select B.key1,B.key2 from B
minus
select A.key1,A.key2 from A)
SQL2
SQL1
Number of Records
Thank You