Download presentation
Presentation is loading. Please wait.
1
1 The Optimizer How ORACLE optimizes SQL statements David Konopnicky 1997, Revised by Mordo Shalom 2004
2
David Konopnicki –1997, MS 2004 2 What is optimization? ä Whenever a DML statement is issued, ORACLE must determine how to execute it. ä There may be different ways to execute the statement. ä The optimizer’s job is to choose one of these ways.
3
David Konopnicki –1997, MS 2004 3 Execution Plans ä To execute a DML statement, ORACLE may have to perform many steps. ä Each step may: ä retrieve rows from the DB. ä prepare rows for the user in some way.
4
David Konopnicki –1997, MS 2004 4 Example SELECT ename,job,sal,dname FROM emp,dept WHERE emp.deptno=dept.deptno AND NOT EXISTS (SELECT * FROM salgrade WHERE emp.sal BETWEEN lowsal AND hisal)
5
David Konopnicki –1997, MS 2004 5 An Execution Plan Physical access to the DB (Access Paths) Read all the rows For each emp, use the deptno value to search the index. It returns a ROWID Use the ROWID to find the row Join rows from emp and dept Filter the results
6
David Konopnicki –1997, MS 2004 6 Order of Execution Get all the rows and return them, one at a time to step 2 For each emp... Find the ROWID of the dept Find the row Join the rows Select the rows Return the row (or not)
7
David Konopnicki –1997, MS 2004 7 The explain plan command
8
David Konopnicki –1997, MS 2004 8 Two approaches to optimization ä Rule-based: Choose an execution plan based on the access path available and choose the access path using a heuristic ranking. ä Cost-based ä Generate a set of possible access paths. ä Evaluate the cost of each access path based on the data distribution and statistics. ä Choose the plan with the smallest cost.
9
David Konopnicki –1997, MS 2004 9 How the optimization is done ä Evaluation of expression and conditions ä Statement transformation ä View merging ä Choice: rule-based or cost-based ä Choice of access paths ä Choice of join orders ä Choice of join operation
10
David Konopnicki –1997, MS 2004 10 SQL Processing Architecture
11
David Konopnicki –1997, MS 2004 11 Optimizer Architecture
12
David Konopnicki –1997, MS 2004 12 Choosing an Optimization Approach and Goal OPTIMIZER_MODE : ä CHOOSE: If there are some statistics then cost- based else rule-based. ä ALL_ROWS (best throughput) ä RULE: rule-based approach (for backward compatibility) ä FIRST_ROWS_n (best response time) ä Hints in the query.
13
David Konopnicki –1997, MS 2004 13 Evaluating Expressions and conditions ä Constants: ä sal > 24000/12 sal > 2000 ä sal*12 > 2000 No evaluation ä LIKE: ä ename LIKE ‘SMITH’ ä ename = ‘SMITH’ (if variable length) ä BETWEEN: ä sal BETWEEN 2000 AND 3000 ä sal >= 2000 AND sal = 2000 AND sal <= 3000
14
David Konopnicki –1997, MS 2004 14 Evaluating...(cont) ä Transitivity: Select * From emp, dept Where emp.deptno = 20 and emp.deptno = dept.deptno dept.deptno=20
15
David Konopnicki –1997, MS 2004 15 Estimator’s Tools ä Selectivity ä Cardinality
16
David Konopnicki –1997, MS 2004 16 Transforming statements ä OR’s into Compound Queries: SELECT * FROM EMP WHERE job = ‘Clerk’ OR deptno = 10 UNION ALL SELECT * FROM EMP WHERE deptno = 10
17
David Konopnicki –1997, MS 2004 17 Optimizing Complex Statement ä Transform the complex statement in a join statement and optimize the join statement ä Optimize the complex statement as it is
18
David Konopnicki –1997, MS 2004 18 Example ä SELECT * FROM accounts FROM accounts WHERE custno IN (SELECT custno FROM customers) WHERE custno IN (SELECT custno FROM customers) ä SELECT accounts.* FROM accounts,customers FROM accounts,customers WHERE account.custno = customers.custno WHERE account.custno = customers.custno customer.custno must be a primary key
19
David Konopnicki –1997, MS 2004 19 The corresponding execution plan
20
David Konopnicki –1997, MS 2004 20 If it cannot be transformed ä Optimize and execute the query and the subqueries independently. ä For example: SELECT * FROM accounts WHERE accounts.balance > ( SELECT AVG(balance) FROM accounts);
21
David Konopnicki –1997, MS 2004 21 Transforming statements that access views ä Merge the view’s query into the statement ä Predicate Pushing ä Then optimize the resulting statement.
22
David Konopnicki –1997, MS 2004 22 Merging the view’s query ä View: CREATE VIEW emp_10 AS SELECT * FROM emp WHERE deptno = 10; ä Statement: SELECT empno FROM emp_10 WHERE empno > 7800 SELECT empno FROM emp WHERE empno>7800 and deptno = 10;
23
David Konopnicki –1997, MS 2004 23 Complex View Merging if the view’s query contains(and enabled by parameter): ä Set operator ä Group by ä DISTINCT ä Group Function
24
David Konopnicki –1997, MS 2004 24 Example ä View: CREATE VIEW group AS SELECT AVG(sal) avg_sal, AVG(sal) avg_sal, MIN(sal) min_sal, MIN(sal) min_sal, MAX(sal) max_sal MAX(sal) max_sal FROM emp GROUP BY deptno; ä Statement: SELECT * FROM group WHERE deptno=10 Select AVG(sal) avg_sal, MIN(sal) min_sal, MAX(sal) max_sal FROM emp WHERE deptno = 10 GROUP BY deptno
25
David Konopnicki –1997, MS 2004 25 The execution plan
26
David Konopnicki –1997, MS 2004 26 Predicate Pushing ä View: CREATE VIEW emp AS SELECT * FROM emp1 emp1UNION SELECT * FROM emp2; emp2; ä Statement: SELECT empno,ename FROM emp WHERE deptno=20;
27
David Konopnicki –1997, MS 2004 27 We will execute... SELECT * FROM emp1 WHERE deptno=20 UNION SELECT * FROM emp2 WHERE deptno=20
28
David Konopnicki –1997, MS 2004 28 The execution plan
29
David Konopnicki –1997, MS 2004 29 Optimizing other statements that access views ä ORACLE cannot always merge views’ queries and statements. ä In these cases, ORACLE issues the view’s query, collects the rows and then access this set of rows with the original statement as thought it was a table.
30
David Konopnicki –1997, MS 2004 30 Example ä View: CREATE VIEW group AS SELECT deptno, AVG(sal) avg_sal, MIN(sal) min_sal, MAX(sal) max_sal FROM emp GROUP BY deptno ä Statement: SELECT group.deptno, avg_sal, min_sal, max_sal, dname,loc FROM group,dept WHERE group.deptno=dept.deptno
31
David Konopnicki –1997, MS 2004 31 Execution Plan
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.