1 The Optimizer How ORACLE optimizes SQL statements David Konopnicky 1997, Revised by Mordo Shalom 2004
David Konopnicki –1997, MS 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.
David Konopnicki –1997, MS 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.
David Konopnicki –1997, MS 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)
David Konopnicki –1997, MS 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
David Konopnicki –1997, MS 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)
David Konopnicki –1997, MS The explain plan command
David Konopnicki –1997, MS 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.
David Konopnicki –1997, MS 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
David Konopnicki –1997, MS SQL Processing Architecture
David Konopnicki –1997, MS Optimizer Architecture
David Konopnicki –1997, MS 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.
David Konopnicki –1997, MS 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
David Konopnicki –1997, MS Evaluating...(cont) ä Transitivity: Select * From emp, dept Where emp.deptno = 20 and emp.deptno = dept.deptno dept.deptno=20
David Konopnicki –1997, MS Estimator’s Tools ä Selectivity ä Cardinality
David Konopnicki –1997, MS Transforming statements ä OR’s into Compound Queries: SELECT * FROM EMP WHERE job = ‘Clerk’ OR deptno = 10 UNION ALL SELECT * FROM EMP WHERE deptno = 10
David Konopnicki –1997, MS Optimizing Complex Statement ä Transform the complex statement in a join statement and optimize the join statement ä Optimize the complex statement as it is
David Konopnicki –1997, MS 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
David Konopnicki –1997, MS The corresponding execution plan
David Konopnicki –1997, MS 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);
David Konopnicki –1997, MS Transforming statements that access views ä Merge the view’s query into the statement ä Predicate Pushing ä Then optimize the resulting statement.
David Konopnicki –1997, MS 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;
David Konopnicki –1997, MS Complex View Merging if the view’s query contains(and enabled by parameter): ä Set operator ä Group by ä DISTINCT ä Group Function
David Konopnicki –1997, MS 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
David Konopnicki –1997, MS The execution plan
David Konopnicki –1997, MS Predicate Pushing ä View: CREATE VIEW emp AS SELECT * FROM emp1 emp1UNION SELECT * FROM emp2; emp2; ä Statement: SELECT empno,ename FROM emp WHERE deptno=20;
David Konopnicki –1997, MS We will execute... SELECT * FROM emp1 WHERE deptno=20 UNION SELECT * FROM emp2 WHERE deptno=20
David Konopnicki –1997, MS The execution plan
David Konopnicki –1997, MS 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.
David Konopnicki –1997, MS 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
David Konopnicki –1997, MS Execution Plan