Oracle SQL Tuning: Presented by Akin S Walter-Johnson Ms Principal Peerlabs, Inc
Oracle SQL Tuning: Presented by Akin S Walter-Johnson Ms Principal Peerlabs, Inc
com
SCOPE
How data is accessed and reconstituted
joins
Inform the user on how identify problems
with SQL
Repair of SQL
Tuning can occur at 2 levels
Server
SQL level
( DBA)
( User)
IMPORTANCE OF TUNING
Reduce response time for SQL processing
To find a more efficient way to process
workload
Improve search time by using indexes
Join data efficiently between 2 or more
tables
HOW TO TUNE
Review the access path, Join methods and
index usage
Test response through SQPLUS directly
( May mask performance )
Test response through an Application front end
( Usually takes longer )
Test response through a web interface
OVERVIEW OF SQL
PROCESSING
OVERVIEW OF SQL
PROCESSING
The Parser checks both syntax and semantic analysis of
SQL statement
Optimizer determines the most efficient way of producing
the result of the query also known as the EXPLAIN
PLAN. How best to get the data.
Oracle Optimizer types ( Cost Based and Rule Based )
CBO based Optimizer uses cost associated with each execution
requires you to analyze objects for statistics
RULE based Optimizer internal rules ( not encouraged by oracle)
SETTING OPTIMIZER
SERVER Level by DBA in parameter file (init.ora)
CLIENT Level SQLPLUS command < alter session set
optimizer_mode=choose>
STATEMENT Level using hints
a. select /*+RULE */ * from dual ;
b. select /*+ CHOOSE */ * from dual ;
Order of Precedence
SERVER->CLIENT->STATEMENT
Users can set both client and statement
To use CBO you need to analyze the tables (see Analyze
objects)
The Optimizer is the brain behind the process of returning data to user it
needs to make the following choices.
OPTIMIZER APPROACH
ACCESS PATH
JOIN ORDER
JOIN METHOD
Choice of optimizer approaches
CBO or RULE
Choice of Access Paths ( How data is Scanned )
Use an index if not reading all records ( faster)
Read or scan all records
Choice of Join Orders
Determine which table to join first when you have more than two tables in
an SQL
Choice of Join Methods
Determine how to join the tables ( Merge, Sort, Hash )
Create Table
Create Index
Review tables
Review indexes
Oracle Index
Binary tree Structure with entries know as ROWID
Left nodes contain key and rowid
ROWID is internal and points to direct location of
record on disk
ROWID is fasted way to reach a record.
SQL> Select rowid, id, name from mytable ;
OPTIMIZER ACCESS by
MULTIPLE UNIQUE SCAN
Optimizer will search for ROWID in the
statement
Concatenate all records into one row set
Combining all rows selected by the unique
scan into I row set
OPTIMIZER ACCESS by
MULTIPLE UNIQUE SCAN
Multiple Unique Scan
Nested Loop
Nested Loop
Good when you expect a small number of rows back
Good for Small driving table so not Good if driving table is
large
Good when Index on B exist ( will perform poor when no
index on B )
Good if you want to quickly returns data to the screen
( ONLINE USERS )
HINT select
SELECT /*+ ORDERED USE_NL(DEPT) to get first row
faster */
EMPNO, ENAME, DEPT.DEPTNO
FROM EMP, DEPT
WHERE EMP.DEPTNO = DEPT.DEPTNO ;
HINT
SELECT /*+use_hash(emp, dept )*/
EMPNO, ENAME, DEPT.DEPTNO
FROM EMP, DEPT
WHERE EMP.DEPTNO =
DEPT.DEPTNO ;
Query 1
SELECT info
FROM taba a, tabb b, tabc c
WHERE
a.key1 = b.key1
AND a.key2 = c.key2
AND a.acol BETWEEN 100 AND 200
AND b.bcol BETWEEN 10000 AND 20000
AND c.ccol BETWEEN 10000 AND 20000
Query 2
SELECT info
FROM taba a, tabb b, tabc c
WHERE
a.acol BETWEEN 100 AND 200
AND b.bcol BETWEEN 10000 AND 20000
AND c.ccol BETWEEN 10000 AND 20000
AND a.key1 = b.key1
AND a.key2 = c.key2;
Query3
SELECT info
FROM taba a, tabb b, tabc c
WHERE
b.bcol BETWEEN 10000 AND 20000
AND c.ccol BETWEEN 10000 AND 20000
AND a.acol BETWEEN 100 AND 200
AND a.key1 = b.key1
AND a.key2 = c.key2;
INDEX TUNING