1 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

Slides:



Advertisements
Similar presentations
Refreshing Materialized Views
Advertisements

10 Copyright © 2005, Oracle. All rights reserved. Dimensions.
12 Copyright © 2005, Oracle. All rights reserved. Query Rewrite.
1.
Tuning Oracle SQL The Basics of Efficient SQLThe Basics of Efficient SQL Common Sense Indexing The Optimizer –Making SQL Efficient Finding Problem Queries.
Tuning: overview Rewrite SQL (Leccotech)Leccotech Create Index Redefine Main memory structures (SGA in Oracle) Change the Block Size Materialized Views,
Presented By Akin S Walter-Johnson Ms Principal PeerLabs, Inc
SQL Tuning Briefing Null is not equal to null but null is null.
CHAPTER OBJECTIVE: NORMALIZATION THE SNOWFLAKE SCHEMA.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
BY LECTURER/ AISHA DAWOOD DW Lab # 4 Overview of Extraction, Transformation, and Loading.
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Your customer as a segment of one That changes every second! Hein Van Der Merwe Chief.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 1.
What Happens when a SQL statement is issued?
Query Optimization CS634 Lecture 12, Mar 12, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Oracle SQL Developer Tips & Tricks Jeff Smith
INTRODUCTION TO ORACLE Lynnwood Brown System Managers LLC Performance And Tuning – Lecture 7 Copyright System Managers LLC 2007 all rights reserved.
CS263 Lecture 19 Query Optimisation.  Motivation for Query Optimisation  Phases of Query Processing  Query Trees  RA Transformation Rules  Heuristic.
Introduction to Structured Query Language (SQL)
David Konopnicki Choosing Access Path ä The basic methods. ä The access paths and when they are available. ä How the optimizer chooses among the.
1 The Optimizer How ORACLE optimizes SQL statements David Konopnicky 1997, Revised by Mordo Shalom 2004.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 11 Database Performance Tuning and Query Optimization.
Database Systems More SQL Database Design -- More SQL1.
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Oracle SQL Developer What’s New in Version 4.1 Jeff Smith
Introduction to Structured Query Language (SQL)
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 2 Hidden Gems of APEX David Gale Software Engineer Oracle Application Express November,
The Safe Harbor The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated.
Access Path Selection in a Relation Database Management System (summarized in section 2)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. JD Edwards Summit The Newest JDE Module – Rental Management Joel Sandberg Sales Consultant.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 1 Preview of Oracle Database 12 c In-Memory Option Thomas Kyte
1Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8 Reporting from Contract.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Oracle Database Administration Lecture 6 Indexes, Optimizer, Hints.
Physical Database Design & Performance. Optimizing for Query Performance For DBs with high retrieval traffic as compared to maintenance traffic, optimizing.
Access Path Selection in a Relational Database Management System Selinger et al.
Database Management 9. course. Execution of queries.
1Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8 Contract Management.
RBO RIP George Lumpkin Director Product Management Oracle Corporation Session id:
11-1 Improve response time of interactive programs. Improve batch throughput. To ensure scalability of applications load vs. performance. Reduce system.
Star Transformations Tony Hasler, UKOUG Birmingham 2012 Tony Hasler, Anvil Computer Services Ltd.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Oracle tuning: a tutorial Saikat Chakraborty. Introduction In this session we will try to learn how to write optimized SQL statements in Oracle 8i We.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
1. When things go wrong: how to find SQL error Sveta Smirnova Principle Technical Support Engineer, Oracle.
Module 4 Database SQL Tuning Section 3 Application Performance.
Physical Database Design Purpose- translate the logical description of data into the technical specifications for storing and retrieving data Goal - create.
Chapter 5 Index and Clustering
DB Tuning : Chapter 10. Optimizer Center for E-Business Technology Seoul National University Seoul, Korea 이상근 Intelligent Database Systems Lab School of.
Sorting and Joining.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Oracle9i Developer: PL/SQL Programming Chapter 11 Performance Tuning.
Lecture 15: Query Optimization. Very Big Picture Usually, there are many possible query execution plans. The optimizer is trying to chose a good one.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 1.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 1.
Closing the Query Processing Loop in Oracle 11g Allison Lee, Mohamed Zait.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Tuning Transact-SQL Queries
Relational Database Design
SQL Tuning.
Choosing Access Path The basic methods.
Database Performance Tuning and Query Optimization
Oracle Analytic Views Enhance BI Applications and Simplify Development
OpenWorld How to Prepare Data from Business Intelligence Cloud Service
Enhance BI Applications and Simplify Development
Chapter 11 Database Performance Tuning and Query Optimization
Introduction to the Optimizer
Query Transformations
Presentation transcript:

1 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

2 The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

3 Copyright © 2011, Oracle and/or its affiliates. All rights reserved. Oracle Optimizer One Day Workshop Thomas Kyte

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 4 Quick History of Oracle Optimizer CBO

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 5 What happens when a SQL statement is issued? User Library Cache Shared SQL Area Shared Pool CnC1C2 … 3 Optimizer Oracle Database Code Generator 1 Syntax Check Semantic Check Shared Pool check 2 Parsing 4 SQL Execution

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 6 “ In the beginning there were rules …”

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 7 Rule Based Optimizer  Rule Based Optimizer (RBO) is a heuristic based Optimizer – Uses a ranked list of access paths (rules) – 17 possible access paths – Lower ranked access paths assumed to operate more efficiently  Plans chosen based on access paths available and their rank – If multiple access paths existed path with the lowest rank chosen  Only very simple physical optimizations done automatically – OR Expansion: multiple OR predicates rewritten as UNION ALL Oracle Version 6 and earlier

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 8 Only way to influence RBO was to change the SQL text Concatenating an empty string to the column prevents the index from being used Famous tricks to work around RBO Got an index access but want a full table scan SELECT count(*) FROM emp WHERE ename || ’’ = ‘SMITH ’; SELECT count(*) FROM emp WHERE ename || ’’ = ‘SMITH ’;

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 9 Dawn of a new era: “.. there is cost.. “ 1992

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 10 Cost Based Optimizer  Database features become more and more complex – Partitioning – Parallel execution  No easy way to extend Rule Based Optimizer to accommodate so many additional access paths  Having only one plan per statement regardless of the objects size or structure was no longer the best approach Oracle 7 - dawn of a new era Optimizer must evolve to become cost based

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 11 Cost Based Optimizer  Initial design based on IBM research paper – Access Path Selection in a Relational Database Management System (1979) Access Path Selection in a Relational Database Management System  Approach outlined in the paper was – Multiple execution plans generated for a statement – Estimated cost is computed for each plan – Optimizer selects the plan with the lowest estimated cost Oracle 7 - dawn of a new era

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 12 Understanding how the Optimizer works Query Transformation Rewrite query text to allow it to be processed more efficiently Query Transformation Rewrite query text to allow it to be processed more efficiently Plan Generator Multiple plans are generated for each SQL, using different access paths and join types. Each plan is costed and plan with the lowest cost is used. Plan Generator Multiple plans are generated for each SQL, using different access paths and join types. Each plan is costed and plan with the lowest cost is used. Cost Estimator Cost is an estimate of the amount of CPU and the number of disk I/Os, used to perform an operation Cost Estimator Cost is an estimate of the amount of CPU and the number of disk I/Os, used to perform an operation Optimizer Statistics Schema definitions

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 13 Optimizer Transformations  Translate statements into semantically equivalent SQL that can be processed more efficiently  Initial transformations were heuristic based – Applied to SQL statements based on their structural properties only  Predominately cost based now  Transformations include – Subquery Unnesting – View Merging – OR Expansion – Star transformation

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 14 Subquery Unnesting SELECT C.cust_last_name, C.country_id FROM customers C WHERE Exists(SELECT null FROM sales S WHERE C.cust_id=S.cust_id AND s.quantity_sold > 1000); SELECT C.cust_last_name, C.country_id FROM customers C WHERE Exists(SELECT null FROM sales S WHERE C.cust_id=S.cust_id AND s.quantity_sold > 1000); A correlated subquery is one that refers to a column from a table outside the subquery In this case C.cust_id is referenced in the subquery Without subquery unnesting the correlated subquery must be evaluated for each row in the Customers tables

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 15 Transformation rewrites the EXISTS subquery to an ANY subquery ANY subquery is no longer correlated Any subquery returns a set of cust_id if any match the predicate will return true Subquery Unnesting SELECT C.cust_last_name, C.country_id FROM customers C WHERE C.cust_id = ANY(SELECT S.cust_id FROM sales S WHERE S.quantity_sold > 1000); SELECT C.cust_last_name, C.country_id FROM customers C WHERE C.cust_id = ANY(SELECT S.cust_id FROM sales S WHERE S.quantity_sold > 1000); Query gets transformed as follows

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 16 Transformation allows subquery to be evaluated as a SEMI join Subquery returns a set of cust_id and those cust_id are joined to the customers table via a SEMI Hash Join Subquery Unnesting

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 17 Complex view merging refers to the merging of group by and distinct views Allows the optimizer to consider additional join orders and access paths Group-by/distinct operations can be delayed until after the joins have been evaluated Complex View Merging Create View cust_prod_totals_v as Select sum(s.quantity_sold) total, s.cust_id, s.prod_id from sales s group by s.cust_id, s.prod_id; Select c.cust_id, c.cust_first_name, c.cust_last_name from customers c, cust_prod_totals_v v, products p where c.country_id = 'US' and c.cust_id = v.cust_id and v.total > 100 and v.prod_id = p.prod_id and p.prod_name = 'T3 Faux Fur-Trimmed Sweater'; Create View cust_prod_totals_v as Select sum(s.quantity_sold) total, s.cust_id, s.prod_id from sales s group by s.cust_id, s.prod_id; Select c.cust_id, c.cust_first_name, c.cust_last_name from customers c, cust_prod_totals_v v, products p where c.country_id = 'US' and c.cust_id = v.cust_id and v.total > 100 and v.prod_id = p.prod_id and p.prod_name = 'T3 Faux Fur-Trimmed Sweater';

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 18 After transformation group by operation occurs after Sales is joined to Products and Customers Number of rows in group by greatly reduced after join May not always be best to delay the group by / distinct operation so transformation is cost based* *Compare the cost of the best plan with and without the transformation Complex View Merging Query gets transformed as follows select c.cust_id, cust_first_name, cust_last_name from customers c, products p, sales s where c.country_id = 'US' and c.cust_id = s.cust_id and s.prod_id = p.prod_id and p.prod_name = 'T3 Faux Fur-Trimme…’ group by s.cust_id, s.prod_id, p.rowid, c.rowid, c.cust_last_name, c.cust_first_name, c.cust_id having sum(s.quantity_sold) > 100; select c.cust_id, cust_first_name, cust_last_name from customers c, products p, sales s where c.country_id = 'US' and c.cust_id = s.cust_id and s.prod_id = p.prod_id and p.prod_name = 'T3 Faux Fur-Trimme…’ group by s.cust_id, s.prod_id, p.rowid, c.rowid, c.cust_last_name, c.cust_first_name, c.cust_id having sum(s.quantity_sold) > 100;

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 19 Transforms queries that contain OR predicates into the form of a UNION ALL query of two or more branches Without the transformation Optimizer treats OR predicates as a single unit Can’t use index on either column OR Expansion SELECT * FROM products WHERE prod_category ='Photo' OR prod_subcategory ='Camera Media'; SELECT * FROM products WHERE prod_category ='Photo' OR prod_subcategory ='Camera Media';

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 20 The transformation adds an LNNVL() function to the second branch in order to avoid duplicates being generated across branches The LNNVL function returns TRUE, if the predicate evaluates to FALSE or if the predicate involves NULL; otherwise it will return FALSE Query gets transformed as follows OR Expansion SELECT * FROM products WHERE prod_subcategory ='Camera Media‘ UNION ALL SELECT * FROM products WHERE prod_category ='Photo' AND lnnvl(prod_subcategory = 'Camera Media'); *lnnvl(true) is FALSE, lnnvl(false||null) is TRUE SELECT * FROM products WHERE prod_subcategory ='Camera Media‘ UNION ALL SELECT * FROM products WHERE prod_category ='Photo' AND lnnvl(prod_subcategory = 'Camera Media'); *lnnvl(true) is FALSE, lnnvl(false||null) is TRUE

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 21 Transformation allows an index access to be considered for each branch of the UNION ALL OR Expansion

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 22 Star Query Transformation Sales Fact Table Products Dimension Time Dimension Geography Dimension Suppliers Dimension Customer s Dimension Cost-based* transformation designed to execute star queries more efficiently Relies on bitmap indexes on foreign key columns to access rows in the fact table Controlled by parameter STAR_TRANSFORMATION_ENABLED Star Schema - one or more large fact table and many smaller dimension tables *Compares the cost of the best plan with and without the transformation

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 23 Traditionally a star query only defines predicates on the dimension tables No efficient way to access rows in the fact table By rewriting the query new access paths become available on the fact table Star Query Transformation SELECT * FROM fact, d1, d2, d3 WHERE fact.c1 = d1.c1 AND fact.c2 = d2.c1 AND fact.c3 = d3.c1 AND d1.c2 IN (1, 2, 3, 4) AND d2.c2 < 100 AND d3.c2 = 35; SELECT * FROM fact, d1, d2, d3 WHERE fact.c1 = d1.c1 AND fact.c2 = d2.c1 AND fact.c3 = d3.c1 AND d1.c2 IN (1, 2, 3, 4) AND d2.c2 < 100 AND d3.c2 = 35;

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 24 Converts original query to include 3 sub-queries on the fact Fact table accessed first via bitmap index and then joins out to dimension tables Result of sub-queries may be saved in temp tables Star Query Transformation Query gets transformed as follows SELECT * FROM fact, d1, d2, d3 … exactly as before … AND fact.c1 IN (SELECT d1.c1 FROM d1 WHERE d1.c2 IN (1, 2, 3, 4) ) AND fact.c2 IN (select d2.c1 FROM d2 WHERE d2.c2 < 100) AND fact.c3 IN (SELECT d3.c1 FROM d3 WHERE d3.c2 = 35) SELECT * FROM fact, d1, d2, d3 … exactly as before … AND fact.c1 IN (SELECT d1.c1 FROM d1 WHERE d1.c2 IN (1, 2, 3, 4) ) AND fact.c2 IN (select d2.c1 FROM d2 WHERE d2.c2 < 100) AND fact.c3 IN (SELECT d3.c1 FROM d3 WHERE d3.c2 = 35)

Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 25 Understanding how the Optimizer works Query Transformation Rewrite query text to allow it to be processed more efficiently Query Transformation Rewrite query text to allow it to be processed more efficiently Plan Generator Multiple plans are generated for each SQL, using different access paths and join types. Each plan is costed and plan with the lowest cost is used. Plan Generator Multiple plans are generated for each SQL, using different access paths and join types. Each plan is costed and plan with the lowest cost is used. Cost Estimator Cost is an estimate of the amount of CPU and the number of disk I/Os, used to perform an operation Cost Estimator Cost is an estimate of the amount of CPU and the number of disk I/Os, used to perform an operation Optimizer Statistics Schema definitions Controlled by OPTIMIZER_FEATURES_ENABLED

26 Copyright © 2011, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 8 Q&A