OracleSQLTuning 1
OracleSQLTuning 1
Filtering
WHERE
ORDER BY
GROUP BY
Filtering
WHERE
ORDER BY
Match indexes
Exact hits (equality)
SELECT * FROM division WHERE
1
WHERE division_id
division_id == 1;
The Basics
of
Efficient
SQL
by SELECT
SELECT
Filtering
WHERE
ORDER BY
GROUP BY
SELECT state, COUNT(state) FROM division GROUP
GROUP BY
by indexes
SELECT division_id FROM division ORDER BY
division_id;
The Basics
of
Efficient
SQL
by SELECT
SELECT
SELECT division_id
division_id FROM division ORDER BY
division_id;
Filtering
WHERE
ORDER BY
GROUP BY
SELECT state, COUNT(state) FROM division GROUP
GROUP
GROUP BY
SELECT DISTINCT(state)
DISTINCT(state) FROM division ORDER BY
state;
by indexes
SELECT division_id
division_id FROM division ORDER BY
division_id;
Filtering
WHERE
ORDER BY SELECT
often
depends on query and index complexity
use WHERE
may need ORDER BY using composite indexes
GROUP BY
not HAVING
Use WHERE
not HAVING
Filtering
WHERE
ORDER BY SELECT
often
depends on query and index complexity
use WHERE
may need ORDER BY using composite indexes
GROUP BY
not HAVING
Use WHERE
not HAVING
avoid using
DECODE
CASE expressions
set operators (UNION)
Use sequences
Use equality (=) or range scans (>)
avoid negatives (!=, NOT)
avoid LIKE
How to index
single column surrogate sequences
dont override PK and FKs
avoid nullable columns
reverse key
surrogate keys
High insertion rates
not DW
Oracle RAC
The Optimizer
Is intelligent
better with simple queries
Is usually correct
Nothing is set in stone
Verify SQL code efficiency
use EXPLAIN PLAN
SET AUTOTRACE ON EXPLAIN
$ORACLE_HOME/rdbms/admin/utlxplan.sql
The Optimizer
Everything cost based
rule based is redundant
Maintain statistics
Dynamic sampling
OPTIMIZER_DYNAMIC_SAMPLING
Set TIMED_STATISTICS
Histograms
maintain as for statistics
use for unevenly distributed indexes
The Optimizer
Tables
full Table scans
small static tables
reading most of the rows
over 10% for the Optimizer
ROWID scans
The Optimizer
Indexes
index unique scan
index range scan
reverse order index range scan
The Optimizer
Joins
nested loop join
most efficient
row sets both small
one large and one small row set
one sorted
hash join
both large with little difference
temporary hash table generated
The Optimizer
More on joins
sort merge join
inefficient
both rows sets sorted then merge sorted
semi joins
bitmap joins
star queries
Cartesian joins
outer joins (nested, hash or sort merge)
The Optimizer
Hints can change things
influence the optimizer
CURSOR_SHARING
configuration parameter
FORCE (OLTP)
EXACT (DW)
FIRST or ALL_ROWS
DYNAMIC_SAMPLING
The Optimizer
More on hints
change index scans
INDEX_ASC, INDEX_DESC
INDEX_FFS
INDEX_JOIN (join indexes)
INDEX_SS_ASC or INDEX_SS_DESC
NO_INDEX, NO_INDEX_FFS or NO_INDEX_SS
change joins
can change join type and influence with
parameters
parallel SQL
EXPLAIN PLAN
SET AUTOTRACE ON EXPLAIN
$ORACLE_HOME/rdbms/admin/utlxplan.sql
executions
parsing
sorting
disk and buffer reads
fetching
V$SQL
optimizer cost
CPU time
elapsed time
Wait Event
Interface
Performance overview
Drilldown
More drilldown
TopSQL
EXPLAIN PLAN
Use
Use the
the HELP
HELP FILES
FILES in
in
Oracle
Oracle
Enterprise
Use theEnterprise
help files
Manager
Manager