View
215
Download
0
Embed Size (px)
Citation preview
11
The OptimizerThe Optimizer
How ORACLE optimizes SQL How ORACLE optimizes SQL statementsstatements
David Konopnicky 1997, Revised by Mordo Shalom 2004David Konopnicky 1997, Revised by Mordo Shalom 2004
22
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
What is optimization?What is optimization?
Whenever a DML statement is issued, Whenever a DML statement is issued, ORACLE must determine how to execute ORACLE must determine how to execute it.it.
There may be different ways to execute the There may be different ways to execute the statement.statement.
The The optimizeroptimizer’s job is to choose one of ’s job is to choose one of these ways.these ways.
33
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
Execution PlansExecution Plans
To execute a DML statement, ORACLE To execute a DML statement, ORACLE may have to perform many steps.may have to perform many steps.
Each step may:Each step may: retrieve rows from the DB.retrieve rows from the DB. prepare rows for the user in some way.prepare rows for the user in some way.
44
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
ExampleExample
SELECT ename,job,sal,dnameSELECT ename,job,sal,dname
FROM emp,deptFROM emp,dept
WHERE emp.deptno=dept.deptnoWHERE emp.deptno=dept.deptno
AND NOT EXISTS AND NOT EXISTS
(SELECT * (SELECT *
FROM salgradeFROM salgrade
WHERE emp.sal BETWEEN WHERE emp.sal BETWEEN
lowsal AND hisal)lowsal AND hisal)
55
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
An Execution PlanAn Execution Plan
3T A B L E A C C E S S (F U L L)
e m p
5IN D E X (U N IQ U E S C A N )
p k_ de p tno
4T A B L E A C C E S S (B Y R O W ID )
d ep t
2N E S T E D L O O P S
6T A B L E A C C E S S (F U L L)
sa lg ra de
1F IL T E R
Physical accessto the DB(Access Paths)
Read allthe rows
For each emp, usethe deptno value to
search the index.It returns a ROWID
Use the ROWIDto find the row
Join rowsfrom empand dept
Filter the results
66
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
Order of ExecutionOrder of Execution
3T A B L E A C C E S S (F U L L)
e m p
5IN D E X (U N IQ U E S C A N )
p k_ de p tno
4T A B L E A C C E S S (B Y R O W ID )
d ep t
2N E S T E D L O O P S
6T A B L E A C C E S S (F U L L)
sa lg ra de
1F IL T E R
Get all the rows andreturn them, one at atime to step 2
For each emp...
Find the ROWID of the dept
Find the rowFind the row
Join the rows
Select the rows
Return the row(or not)
77
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
The explain plan commandThe explain plan command
ID Operation Options Object_name1 Filter
2 Nested loops
3 Table access Full EMP4 Table access By Rowid DEPT5 Index Unique scan pk_dep tno
6 Filter Full S A L G R A D E
88
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
Two approaches to optimizationTwo approaches to optimization
Rule-based:Rule-based:
Choose an execution Choose an execution plan based on the plan based on the access path available access path available and choose the access and choose the access path using a heuristic path using a heuristic ranking.ranking.
Cost-basedCost-based Generate a set of Generate a set of
possible access paths.possible access paths. Evaluate the cost of Evaluate the cost of
each access path based each access path based on the data distribution on the data distribution and statistics.and statistics.
Choose the plan with Choose the plan with the smallest cost.the smallest cost.
99
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
How the optimization is doneHow the optimization is done
Evaluation of expression and conditionsEvaluation of expression and conditions Statement transformationStatement transformation View mergingView merging Choice: rule-based or cost-basedChoice: rule-based or cost-based Choice of access pathsChoice of access paths Choice of join ordersChoice of join orders Choice of join operationChoice of join operation
1010
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
SQL Processing ArchitectureSQL Processing Architecture
1111
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
Optimizer ArchitectureOptimizer Architecture
1212
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
Choosing an Optimization Approach and GoalChoosing an Optimization Approach and Goal
OPTIMIZER_MODEOPTIMIZER_MODE:: CHOOSE: If there are some statistics then cost-CHOOSE: If there are some statistics then cost-
based else rule-based.based else rule-based. ALL_ROWS (best throughput)ALL_ROWS (best throughput) RULE: rule-based approach (for backward RULE: rule-based approach (for backward
compatibility)compatibility) FIRST_ROWS_n (best response time)FIRST_ROWS_n (best response time)
Hints in the query.Hints in the query.
1313
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
Evaluating Expressions and conditionsEvaluating Expressions and conditions
Constants: Constants: sal > 24000/12 sal > 24000/12 sal > 2000 sal > 2000 sal*12 > 2000 sal*12 > 2000 No evaluation No evaluation
LIKE:LIKE: ename LIKE ‘SMITH’ ename LIKE ‘SMITH’ ename = ‘SMITH’ (if variable length)ename = ‘SMITH’ (if variable length)
BETWEEN:BETWEEN: sal BETWEEN 2000 AND 3000sal BETWEEN 2000 AND 3000 sal >= 2000 AND sal <= 3000sal >= 2000 AND sal <= 3000
1414
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
Evaluating...(cont)Evaluating...(cont)
Transitivity:Transitivity:
Select *Select *
From emp, deptFrom emp, dept
Where emp.deptno = 20 andWhere emp.deptno = 20 and
emp.deptno = dept.deptnoemp.deptno = dept.deptno
dept.deptno=20
1515
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
Estimator’s ToolsEstimator’s Tools
SelectivitySelectivity CardinalityCardinality
1616
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
Transforming statementsTransforming statements
OR’s into Compound Queries:OR’s into Compound Queries:
SELECT * SELECT *
FROM EMPFROM EMP
WHERE job = ‘Clerk’WHERE job = ‘Clerk’
OR deptno = 10OR deptno = 10
UNION ALL SELECT * FROM EMP WHERE deptno = 10
1717
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
Optimizing Complex Statement Optimizing Complex Statement
Transform the complex statement in a join Transform the complex statement in a join statement and optimize the join statementstatement and optimize the join statement
Optimize the complex statement as it isOptimize the complex statement as it is
1818
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
ExampleExample SELECT *SELECT *
FROM accounts FROM accounts
WHERE custno IN (SELECT custno FROM WHERE custno IN (SELECT custno FROM
customers)customers)
SELECT accounts.*SELECT accounts.*
FROM accounts,customersFROM accounts,customers
WHERE account.custno = WHERE account.custno =
customers.custnocustomers.custno
customer.custnomust be a primary key
1919
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
The corresponding execution planThe corresponding execution plan
2T A B L E A C C E S S (F U L L)
a ccou n ts
3IN D E X (U N IQ U E S C A N )
p k_ cu s tom e rs
1N e ste d L oo ps
2020
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
If it cannot be transformedIf it cannot be transformed
Optimize and execute the query and the Optimize and execute the query and the subqueries independently.subqueries independently.
For example:For example:
SELECT *SELECT *
FROM accountsFROM accounts
WHERE accounts.balance > (WHERE accounts.balance > (
SELECT AVG(balance) FROM SELECT AVG(balance) FROM accounts);accounts);
2121
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
Transforming statements that access viewsTransforming statements that access views
Merge the view’s query into the statementMerge the view’s query into the statement Predicate PushingPredicate Pushing Then optimize the resulting statement.Then optimize the resulting statement.
2222
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
Merging the view’s queryMerging the view’s query
View:View:
CREATE VIEW CREATE VIEW emp_10emp_10
AS SELECT * AS SELECT *
FROM empFROM emp
WHERE deptno = 10;WHERE deptno = 10;
Statement:Statement:
SELECT empnoSELECT empno
FROM emp_10FROM emp_10
WHERE empno > 7800WHERE empno > 7800
SELECT empnoFROM empWHERE empno>7800and deptno = 10;
2323
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
Complex View MergingComplex View Merging
if the view’s query contains(and enabled by if the view’s query contains(and enabled by parameter):parameter):
Set operatorSet operator Group byGroup by DISTINCTDISTINCT Group FunctionGroup Function
2424
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
ExampleExample
View:View:
CREATE VIEW groupCREATE VIEW group
AS SELECTAS SELECT
AVG(sal) avg_sal,AVG(sal) avg_sal,
MIN(sal) min_sal,MIN(sal) min_sal,
MAX(sal) max_salMAX(sal) max_sal
FROM empFROM emp
GROUP BY deptno;GROUP BY deptno;
Statement:Statement:
SELECT * FROMSELECT * FROM
groupgroup
WHERE deptno=10WHERE deptno=10Select AVG(sal) avg_sal,MIN(sal) min_sal,MAX(sal) max_salFROM empWHERE deptno = 10 GROUP BYdeptno
2525
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
The execution planThe execution plan
4 IN D E X (R A N G E S C A N )e m p _ de p tn o ind ex
3 T A B L E A C C E S S (B Y R O W ID )e m p
2 S O R T (G R O U P B Y)
1 V IE Wg roup
2626
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
Predicate PushingPredicate Pushing
View:View:
CREATE VIEW empCREATE VIEW emp
AS AS
SELECT * FROMSELECT * FROM
emp1emp1
UNIONUNION
SELECT * FROMSELECT * FROM
emp2;emp2;
Statement:Statement:
SELECT empno,enameSELECT empno,ename
FROM empFROM emp
WHERE deptno=20;WHERE deptno=20;
2727
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
We will execute...We will execute...
SELECT * FROM emp1 WHERE deptno=20SELECT * FROM emp1 WHERE deptno=20
UNIONUNION
SELECT * FROM emp2 WHERE deptno=20SELECT * FROM emp2 WHERE deptno=20
2828
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
The execution planThe execution plan
4 T A B L E A C C E S S (F U L L)e m p1
5 T A B L E A C C E S S (F U L L)e m p2
3U N IO N A LL
2S O R T (U N IQ U E )
1V IE We m p
2929
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
Optimizing other statements that access viewsOptimizing other statements that access views
ORACLE cannot always merge views’ ORACLE cannot always merge views’ queries and statements.queries and statements.
In these cases, ORACLE issues the view’s In these cases, ORACLE issues the view’s query, collects the rows and then access this query, collects the rows and then access this set of rows with the original statement as set of rows with the original statement as thought it was a table.thought it was a table.
3030
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
ExampleExample
View:View:CREATE VIEW groupCREATE VIEW group
AS SELECT deptno,AS SELECT deptno,
AVG(sal) avg_sal,AVG(sal) avg_sal,
MIN(sal) min_sal,MIN(sal) min_sal,
MAX(sal) max_salMAX(sal) max_sal
FROM empFROM emp
GROUP BY deptnoGROUP BY deptno
Statement:Statement:SELECT group.deptno,SELECT group.deptno,
avg_sal, min_sal, max_sal,avg_sal, min_sal, max_sal,
dname,locdname,loc
FROM group,deptFROM group,dept
WHERE WHERE group.deptno=dept.deptnogroup.deptno=dept.deptno
3131
David Konopnicki –1997, MS 2004David Konopnicki –1997, MS 2004
Execution PlanExecution Plan
4T A B L E A C C E S S (F U L L)
e m p
3S O R T (G R O U P B Y )
2V IE Wg roup
6IN D E X (U N IQ U E S C A N )
p k_d ep t
5T A B L E A C C E S S (B Y R O W ID )
d ep t
1N E S T E D L O O P S