SQL Tuning
SQL Tuning
SQL Tuning
Javier Durango
Topics
1 Performance Tuning -
When?
3 Execution Plans
1 WHEN TO START
TUNING
Tuning During the Life Cycle
• Tuning is of two types:
• Proactive (Make it better, so it
will not break)
• Test scenarios.
• Find the problem areas. Proactive
• Resolve the problem.
• Reactive (Wait until it breaks;
then fix it)
• Monitor production application.
• Tune issues as needed.
Reactive
Application Design and Development
The application can be tuned, even
in the design and development
phases, by building and tuning test
cases:
• Check normalization against major
functions.
• Check data structures against
access times.
• Look at the points where processes
are serialized.
• Tune the major reports.
• Tune the high-volume processes.
• Quantity of SQL statements issued.
the most efficient
SQL statement is
the one that
is never executed
2 SQL TUNING
SQL Processing Phases
Open Parse
Bind
Execute
Close Fetch
SQL Tuning
• Single execution
• Concurrent execution
SQL Tuning
• Single execution
• Queries
• Bulk Inserts
• Bulk Updates
• Bulk Deletes
• Concurrent execution
SQL Tuning
• Single execution
• Queries
• Execution plan
• Bulk Inserts
• Bulk Updates
• Bulk Deletes
• Concurrent execution
SQL Tuning
• Single execution
• Queries
• Bulk Inserts
• Context switching
• Find available block
• Index balancing
• Bulk Updates
• Bulk Deletes
• Concurrent execution
Context Switching
PL/SQL Engine
BEGIN
procedural Procedural
PL/SQL FOR i IN … LOOP
Statement
Block Executor INSERT ...
SQL and/or
UPDATE ...
and/or
DELETE ...
SQL Statement END LOOP;
Executor END;
SQL Engine
Insert – Context
switching
• Use insert – select whenever
possible
• Use forall insert when
working with pl/sql
collections
Insert – Find available blocks
Regular insert: Direct-path insert:
available available
blocks blocks
HWM HWM
HWM
Insert – Find available
blocks
• Use append hint for insert –
select operations
• Use append-values hint in
forall insert operations
Insert – Index maintenance
Index Maintenance
• Remove unnecessary indexes DROP INDEX emp_email;
Block header
Growth
Free space
Row data
Row chaining and migration
Migration
Chaining
Index Table
Row chaining and
migration
• Plan ahead for tables where a
big percentage of rows could
grow considerably in size
• Create tables with bigger
block free space
• DBAs can create tablespaces
with a bigger block size
SQL Tuning
• Single execution
• Queries
• Bulk Inserts
• Bulk Updates
• Bulk Deletes
• Execution Plan
• Index balancing
• Concurrent execution
SQL Tuning
• Single execution
• Concurrent execution
• Parsing time
• Locking
Shared SQL Areas
Cursor for Cursor for
SELECT statement 2 SELECT statement 1
• Correct:
execute immediate
'select * from orders where order_id = :i'
into order_rec
using i;
SQL Processing Phases
Open Parse
Bind
Execute
Close Fetch
SQL Tuning
• Single execution
• Concurrent execution
• Parsing time
• Locking
SQL Tuning: Locking
• Though it is very unusual to face performance issues due to row
locking, poor application design can lead to lock waits:
3 EXECUTION
PLAN
Execution Plan
• Series of steps executed to
find rows for a single query,
update or delete operation
• Usually executed sequentially
• Can be parallelized
SQL Processing Phases
Open Parse
Bind
Execute
Close Fetch
Execution Plan
• There are not good or bad plans
• There are not good or bad steps
• Given a specific scenario, a plan can be optimal or suboptimal
-----------------------------------------------------------------------------------------
|Id | Operation |Name |Rows |Bytes |Cost | TQ |IN -OUT|PQ Distrib|
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 41 | 1066 | 4 | | | |
| 1 | PX COORDINATOR | | | | | | | |
| 2 | PX SEND QC (RANDOM) |:TQ10001 | 41 | 1066 | 4 |Q1,01 | P->S |QC (RAND) |
| 3 | SORT GROUP BY | | 41 | 1066 | 4 |Q1,01 | PCWP | |
| 4 | PX RECEIVE | | 41 | 1066 | 4 |Q1,01 | PCWP | |
| 5 | PX SEND HASH |:TQ10000 | 41 | 1066 | 4 |Q1,00 | P ->P | HASH |
| 6 | SORT GROUP BY | | 41 | 1066 | 4 |Q1,00 | PCWP | |
| 7 | PX BLOCK ITERATOR | | 41 | 1066 | 1 |Q1,00 | PCWC | |
| 8 | TABLE ACCESS FULL|EMP2 | 41 | 1066 | 1 |Q1,00 | PCWP | |
-----------------------------------------------------------------------------------------
How does the optimizer build an execution
plan?
Types of Execution Plan Steps
• Data retrieval
• Joins and anti joins
• Sorts
• Temp table transformation
Types of Execution Plan Steps
• Data retrieval
• Table access
• Index access
• Joins and anti joins
• Sorts
• Temp table transformation
Types of Execution Plan Steps
• Data retrieval
• Table access
• Full table scan
• Table access by index rowid
• Index access
• Joins and anti joins
• Sorts
• Temp table transformation
Full Table Scan
• The DB reads all “used” blocks
up to HWM
• It can use multiblock reads
• In Exadata environments it can
“offload” the processing of the
step to the cell storage
Table access by index rowid
• Involves 2 separate readings,
Indexed
access on table
one for the index and then for
the table
• It may be one of the fastest
way to access a single row in a
large table ROWID
Types of Execution Plan Steps
• Data retrieval
• Table access
• Index access
• Index unique scan
• Index range scan
• Index fast full scan
• Joins and anti joins
• Sorts
• Temp table transformation
Index Unique Scan
• This scan returns, at most, a
Accessing a unique
index
single rowid.
• Oracle performs a unique scan
if a statement contains a
UNIQUE or a PRIMARY KEY
constraint that guarantees that ROWID
only a single row is accessed.
Column C
select order_id from orders Column B
Column A
Types of Execution Plan Steps
• Data retrieval
• Joins and anti joins
• Nested loop
• Sort-merge
• Hash join
• Sorts
• Temp table transformation
Nested Loop Join
• One of the two tables is SELECT c.cust_id, co.country_name
defined as the outer table (or FROM customers c
the driving table). INNER JOIN countries co
• The other table is called the ON (c.country_id = co.id);
inner table.
• For each row in the outer
table, all matching rows in the
inner table are retrieved.
• Used when few rows have a
good driving condition.
Nested Loop Join
• One of the two tables is SELECT c.cust_id, co.country_name
defined as the outer table (or FROM customers c
the driving table). INNER JOIN countries co
• The other table is called the ON (c.country_id = co.id);
inner table.
• For each row in the outer
table, all matching rows in the
inner table are retrieved.
• Used when few rows have a
good driving condition.
Sort-Merge Join
Basic Execution Plan:
• The rows from each row MERGE (JOIN)
source are sorted on the join SORT (JOIN)
TABLE ACCESS (…) OF table_A
predicate columns. SORT (JOIN)
TABLE ACCESS (…) OF table_B
• The two sorted row sources
are then merged and returned
as the resulting row source.
• Used when inputs are already
sorted, or sorts are required for
other operations.
Sort-Merge Join
Basic Execution Plan:
• The rows from each row MERGE (JOIN)
source are sorted on the join SORT (JOIN)
TABLE ACCESS (…) OF table_A
predicate columns. SORT (JOIN)
TABLE ACCESS (…) OF table_B
• The two sorted row sources
are then merged and returned
as the resulting row source.
• Used when inputs are already
sorted, or sorts are required for
other operations.
Hash Join
• Both tables are split into as
many partitions as required,
using a full table scan.
• For each partition pair, a hash
table is built in memory on the
smallest partition.
• The other partition is used to
probe the hash table.
Hash Join
• Both tables are split into as
many partitions as required,
using a full table scan.
• For each partition pair, a hash
table is built in memory on the
smallest partition.
• The other partition is used to
probe the hash table.
Types of Execution Plan Steps
• Data retrieval
• Joins and anti joins
• Sorts
• Temp table transformation
Sort Operations
• Sort unique (distinct, union)
SELECT
• Sort aggregate (count) DISTINCT last_name, first_name
• Sort group by FROM customers
WHERE full_name LIKE :b1;
• Hash group by
Plan
• Sort join -----------------------------------
• Sort order by SELECT STATEMENT
SORT UNIQUE
• Sort order by stopkey TABLE ACCESS BY INDEX ROWID
INDEX RANGE SCAN
Sort Operations SELECT *
FROM customers c
• Sort unique (distinct, union) WHERE c.creation_date > SYSDATE-30
• Sort aggregate (count) UNION
SELECT *
• Sort group by FROM customers c
WHERE c.balance < 5000;
• Hash group by
• Sort join Plan
-----------------------------------
• Sort order by SELECT STATEMENT
SORT UNIQUE
• Sort order by stopkey UNION
TABLE ACCESS FULL
TABLE ACCESS FULL
Sort Operations
• Sort unique (distinct, union)
SELECT
• Sort aggregate (count, sum) SUM ( d.revenue_amount )
• Sort group by FROM order_details d
WHERE d.customer_id = :b1;
• Hash group by
Plan
• Sort join -----------------------------------
• Sort order by SELECT STATEMENT
SORT AGGREGATE
• Sort order by stopkey TABLE ACCESS BY INDEX ROWID
INDEX RANGE SCAN
Sort Operations
• Sort unique (distinct, union)
SELECT
• Sort aggregate (count, sum) item_id, SUM(d.revenue_amount)
• Sort group by FROM order_details d
WHERE order_id > :b1
• Hash group by GROUP BY item_id;
Thank you.