SQL Tuning
SQL Tuning
Presented to NAM
12-Feb-2016
By Kuldeep Singh
Introduction
Providing a Framework
Introduction
Join order
Join method
Access method
Challenges
Vendor Applications
Never Ending
More Memory
Software Solution
Identify End-to-End
Business Aspects
Technical Information
Understand Relationships
End-to-End Process
V$SESSION_WAIT
SID
USERNAME
SQL_ID
PROGRAM
MODULE
ACTION
PLAN_HASH_VALUE
ROW_WAIT_OBJ#
SID
EVENT
P1, P1RAW, P2, P2RAW, P3, P3RAW
STATE (WAITING, WAITED)
Oracle 10g added this info to V$SESSION
V$SQL
V$SQLAREA
V$SQL_PLAN
SQL_ID
SQL_ID
SQL_ID
SQL_FULLTEXT
EXECUTIONS
PLAN_HASH_VALUE
PARSE_CALLS
BUFFER_GETS
DISK_READS
DBA_OBJECTS
OBJECT_ID
OBJECT_NAME
OBJECT_TYPE
SQL Statement 1
SQL Statement 2
Executed 1 time
Gather - Metrics
Locking / Blocking
Execution Plan
V$SQL_PLAN
DBMS_XPLAN
Bind Values
V$SQL_BIND_CAPTURE
Tracing
ERD
Execution Plan
-----------------------------------------------------------------------------| Id
| Operation
| Name
| Rows
| Bytes | Cost
-----------------------------------------------------------------------------|
0 | SELECT STATEMENT
95 |
1 |
1 |
167 |
95 |
2 |
1 |
138 |
94 |
3 |
7 |
357 |
87 |
4 |
| VW_SQ_1
201 |
7035 |
87 |
|*
5 |
6 |
201 |
3417 |
87 |
|*
7 |
|*
8 |
| REGISTRATION | 80000 |
1328K|
76 |
|*
9 |
|* 10 |
|* 11 |
|
12 |
|* 13 |
NESTED LOOPS
NESTED LOOPS
NESTED LOOPS
VIEW
FILTER
HASH GROUP BY
FILTER
TABLE ACCESS FULL
INDEX UNIQUE SCAN
| SYS_C0036920 |
1 |
16 |
0 |
1 |
87 |
1 |
| SYS_C0036919 |
1 |
0 |
1 |
29 |
1 |
| SYS_C0036918 |
1 |
0 |
------------------------------------------------------------------------------
5 - filter((MAX("SIGNUP_DATE")>=TRUNC(SYSDATE@!-1) AND
MAX("SIGNUP_DATE")<=TRUNC(SYSDATE@!)))
7 - filter(TRUNC(SYSDATE@!-1)<=TRUNC(SYSDATE@!))
8 - filter("CANCELLED"='N')
9 - access("R1"."STUDENT_ID"="STUDENT_ID" AND "R1"."CLASS_ID"="CLASS_ID" AND
"SIGNUP_DATE"="VW_COL_1")
filter(("SIGNUP_DATE">=TRUNC(SYSDATE@!-1) AND "SIGNUP_DATE"<=TRUNC(SYSDATE@!)))
10 - filter(UPPER("C"."NAME)='SQL TUNING')
11 - access("CLASS_ID"="C"."CLASS_ID")
13 - access("S"."STUDENT_ID"="STUDENT_ID")
Response time
Service time
time
jobs
Access Methods
optimizer_index_caching
optimizer_index_cost_adj
db_file_multiblock_read_count
Hints
Full
Index
Resource contention
Block contention
Lock contention
static data
small tables
How to index
The Optimizer
An optimizer determines the best way to execute the SQL
query. Oracle has two optimizers.
RULE
CHOOSE
ALL_ROWS
For Query:
Select /*+ <mode> */ first, last from students;
Select /*+ FIRST_ROWS */ first, last from students;
Select /*+ FIRST_ROWS(50) */ first, last from students;
Select /*+ RULE */ first, last from students;
Select /*+ CHOOSE */ first, last from students;
Select /*+ ALL_ROWS */ first, last from students;
Index Decision
Should the Index Be used?
- what is table size
- what is column(s) selectivity
(if you are selecting >20%
dont bother with the index)
Is the optimizer making the correct decision?
Join Order
Small tables or Tables with High Selectivity should be joined before large
tables or tables with low selectivity.
Monitor
Tuning is iterative
Thank you