1 The Optimizer How ORACLE optimizes SQL statements David Konopnicky 1997, Revised by Mordo Shalom 2004.

Slides:



Advertisements
Similar presentations
Oracle 10g & 11g for Dev Virtual Columns DML error logging
Advertisements

1 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
12-1 Copyright  Oracle Corporation, All rights reserved. What Is a View? EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
Bogdan Shishedjiev SQL1 SQL Reminder of SQL. Bogdan Shishedjiev SQL 2 Subsets of language Data definition language (DDL) –Domain definition –Schema definition.
Copyright  Oracle Corporation, All rights reserved. 6 Subqueries.
Set operators (UNION, UNION ALL, MINUS, INTERSECT) [SQL]
Copyright  Oracle Corporation, All rights reserved. 6 Writing Correlated Subqueries.
Agenda Overview of the optimizer How SQL is executed Identifying statements that need tuning Explain Plan Modifying the plan.
CS263 Lecture 19 Query Optimisation.  Motivation for Query Optimisation  Phases of Query Processing  Query Trees  RA Transformation Rules  Heuristic.
David Konopnicki Choosing Access Path ä The basic methods. ä The access paths and when they are available. ä How the optimizer chooses among the.
V 1.0 OE NIK PHP+SQL 11 (SQL 4) Views Rownum/LIMIT Examples.
Session 3: SQL (B): Parts 3 & 4 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Relational Database Performance CSCI 6442 Copyright 2013, David C. Roberts, all rights reserved.
Copyright  Oracle Corporation, All rights reserved. I Introduction.
Oracle Database Administration Lecture 6 Indexes, Optimizer, Hints.
Access Path Selection in a Relational Database Management System Selinger et al.
Database Management 9. course. Execution of queries.
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
1 Chapter 7 Optimizing the Optimizer. 2 The Oracle Optimizer is… About query optimization Is a sophisticated set of algorithms Choosing the fastest approach.
Set operators The set operaotrs combine the result of two or more component queries into one result. queries containing set operators are called compound.
Lecture 8 Database Theory & Practice (2) : The Relational Data Model UFCEKG-20-2 Data, Schemas & Applications.
Displaying Data from Multiple Tables (Join). EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS.
11-1 Improve response time of interactive programs. Improve batch throughput. To ensure scalability of applications load vs. performance. Reduce system.
MSc IT UFIE8K-15-M Data Management Prakash Chatterjee Room 3P16
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Cursors These slides are licensed under.
Subqueries.
1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1.
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)
PL SQL Block Structures. What is PL SQL A good way to get acquainted with PL/SQL is to look at a sample program. PL/SQL combines the data manipulating.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
11 Copyright © Oracle Corporation, All rights reserved. Creating Views.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
Copyright س Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
Module 4 Database SQL Tuning Section 3 Application Performance.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
9-1 © Prentice Hall, 2007 Topic 9: Physical Database Design Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich,
Copyright س Oracle Corporation, All rights reserved. I Introduction.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relational State Assertions These slides.
6 Subqueries. 6-2 Objectives At the end of this lesson, you should be able to: Describe the types of problems that subqueries can solve Define subqueries.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Grouping These slides are licensed under.
Creating Views Database Systems Objectives Explain the concept of a view. Create simple and complex views. Retrieve data through a view. Alter the.
David Konopnicki –1997, Rev. MS Optimizing Join Statements To choose an execution plan for a join statement, the optimizer must choose: ä Access.
1 Presentation Outline SQL Writing Process SQL Standards Using Indexes The Optimizer FROM, WHERE Clauses EXPLAIN SQL Trace Sub-Selects and Joins Tips and.
Optimization and Administartion of a Database Management Systems Krystian Zieja.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
Oracle9i Developer: PL/SQL Programming Chapter 11 Performance Tuning.
Chapter 13: Query Processing
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Copyright  Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
Displaying Data from Multiple Tables. Objectives After completing this lesson, you should be able to do the following: –Write SELECT statements to access.
4 Displaying Data from Multiple Tables. 4-2 Objectives At the end of this lesson, you should be able to: Write SELECT statements to access data from more.
SQL Tuning.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Choosing Access Path The basic methods.
Interpreting Execution Plans
Writing Correlated Subqueries
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Akshay Tomar Prateek Singh Lohchubh
Lecture 16 : The Relational Data Model
Subqueries Schedule: Timing Topic 25 minutes Lecture
Lecture 16 : The Relational Data Model
Introduction to the Optimizer
Subqueries Schedule: Timing Topic 25 minutes Lecture
Database Programming Using Oracle 11g
Presentation transcript:

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