Sei sulla pagina 1di 5

What is an Optimizer?

Query Optimization It is the component of a database


management system that attempts to select
the most efficient way to execute a query.
What is an Optimizer?
It determines the most efficient way to
How Optimizer works?
execute a SQL statement.
Optimization Statements
The access plan describes which tables to
scan, which index, if any, to use for each
table, which joins strategy to use and to what
order the tables are read.

*Property of STI K0019 *Property of STI K0019

1 _________________________ 2 __________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________

How Optimizer Works? Optimization of Statements

1. The optimizer generates a set of potential plans


for the SQL statement based on available
access paths and hints. Avoid the OR Operator
2. The optimizer estimates the cost of each plan
based on statistics in the data dictionary for the SQLwill not use an index if the
data distribution and storage characteristics of
condition in a WHERE clause
the tables, indexes and partitions accessed by
the statement. contains the OR operator in most
cases.
3. The optimizer compares the costs of the plans
and chooses the one with the lowest cost.

*Property of STI K0019 *Property of STI K0019

3 _________________________ 4 __________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
Optimization of Statements Optimization of Statements

Example:

Optimized Statement:
SELECT NAME, INITIALS
FROM PLAYERS
SELECT NAME, INITIALS
WHERE PLAYERNO = 6
FROM PLAYERS
OR PLAYERNO = 83
WHERE PLAYERNO IN (6, 83, 44)
OR PLAYERNO = 44

*Property of STI K0019 *Property of STI K0019

5 _________________________ 6 __________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________

Optimization of Statements Optimization of Statements

Avoid the HAVING Clause Example:

It is recommended to place as SELECT PLAYERNO, COUNT(*)


many conditions as possible in the FROM PENALTIES
WHERE clause and as few as
GROUP BY PLAYERNO
possible in the HAVING clause.
HAVING PLAYERNO >= 50

*Property of STI K0019 *Property of STI K0019

7 _________________________ 8 __________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
Optimization of Statements Optimization of Statements

Optimized Statement: Make the SELECT Clause as Small as


Possible
SELECT PLAYERNO, COUNT(*)
FROM PENALTIES Avoid
using unnecessary columns
GROUP BY PLAYERNO because it slows the processing
speed of the query.
HAVING PLAYERNO >= 50

*Property of STI K0019 *Property of STI K0019

9 _________________________ 10 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________

Optimization of Statements Optimization of Statements

Example:
SELECT PLAYERNO, NAME Avoid DISTINCT
FROM PLAYERS
WHERE EXISTS SpecifyingDISTINCT in the SELECT
(SELECT '101' clause removes the duplicate rows
from the result.
FROM PENALTIES
WHERE
PENALTIES.PLAYERNO =
PLAYERS.PLAYERNO)
*Property of STI K0019 *Property of STI K0019

11 ________________________ 12 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
Optimization of Statements Optimization of Statements

Example:
Avoid Unnecessary Use of the UNION
SELECT DISTINCT MATCHNO, Operator
NAME
FROM MATCHES, PLAYERS
The
UNION operator must only be
WHERE MATCHES.PLAYERNO = used if required and relevant.
PLAYERS.PLAYERNO

*Property of STI K0019 *Property of STI K0019

13 ________________________ 14 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________

Optimization of Statements Optimization of Statements

Example:
SELECT MATCHNO, WON - LOST Isolate Columns in Conditions
FROM MATCHES
When an index is defined on a
WHERE WON >= LOST
column that occurs in a calculation
UNION or scalar function, that index will not
SELECT MATCHNO, LOST - WON be used.
FROM MATCHES
WHERE WON < LOST

*Property of STI K0019 *Property of STI K0019

15 ________________________ 16 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
Optimization of Statements Optimization of Statements

Example: Optimized Statement:


SELECT * SELECT *
FROM PLAYERS FROM PLAYERS
WHERE JOINED + 10 = 2009 WHERE JOINED = 1999

*Property of STI K0019 *Property of STI K0019

17 ________________________ 18 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________

Optimization of Statements Optimization of Statements

Example:
Largest Table Last SELECT * FROM
PLAYERS, TEAMS
In
formulating joins, it is possible that
the sequence of the tables in the Optimized Statement:
FROM clause can affect the SELECT * FROM
processing speed.
TEAMS, PLAYERS

*Property of STI K0019 *Property of STI K0019

19 ________________________ 20 _________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________
___________________________ ___________________________

Potrebbero piacerti anche