Explaining the Explain Plan:

Similar documents
Oracle Database 11gR2 Optimizer Insights

The Oracle Optimizer Explain the Explain Plan O R A C L E W H I T E P A P E R A P R I L

<Insert Picture Here> Inside the Oracle Database 11g Optimizer Removing the black magic

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

20 Essential Oracle SQL and PL/SQL Tuning Tips. John Mullins

Answer: Reduce the amount of work Oracle needs to do to return the desired result.

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Database Foundations. 6-9 Joining Tables Using JOIN. Copyright 2014, Oracle and/or its affiliates. All rights reserved.

CBO SQL TRANSFORMER Document describes a few examples of transformations made by CBO.

Oracle DB-Tuning Essentials

Copyright 2012, Oracle and/or its affiliates. All rights reserved.

Oracle Database 11g: SQL Tuning Workshop. Student Guide

Oracle SQL Tuning for Developers Workshop Student Guide - Volume I

Oracle 1Z Oracle Database 11g Performance Tuning.

KORA. RDBMS Concepts II

Oracle Optimizer: What s New in Oracle Database 12c? Maria Colgan Master Product Manager

Oracle Database 11g: SQL Tuning Workshop

Top 7 Plan Stability Pitfalls & How to Avoid Them. Neil Chandler Chandler Systems Ltd UK

Query Optimizer, Who Influences & How it works ++ optimization techniques

SQL Gone Wild: Taming Bad SQL the Easy Way (or the Hard Way) Sergey Koltakov Product Manager, Database Manageability

Join, Sub queries and set operators

Interpreting Explain Plan Output. John Mullins

D B M G Data Base and Data Mining Group of Politecnico di Torino

Joining tables. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

<Insert Picture Here> DBA s New Best Friend: Advanced SQL Tuning Features of Oracle Database 11g

Oracle Database Performance Tuning, Benchmarks & Replication

Tuning SQL without the Tuning Pack. John Larkin JP Morgan Chase

Oracle Database 12c: SQL Tuning for Developers

Join Methods. Franck Pachot CERN

Real-World Performance Training SQL Performance

Retrieving Data from Multiple Tables

Intermediate SQL: Aggregated Data, Joins and Set Operators

Optimizer with Oracle Database 12c Release 2 O R A C L E W H I T E P A P E R J U N E

GIFT Department of Computing Science. CS-217/224: Database Systems. Lab-5 Manual. Displaying Data from Multiple Tables - SQL Joins

Dan Hotka Author/Instructor Oracle Ace Director.

Oracle 9i Application Development and Tuning

Advanced Oracle Performance Troubleshooting. Query Transformations Randolf Geist

Adaptive Optimization. Presented by: Kerry Osborne Red Gate Webinar, Nov. 2013

Triangle SQL Server User Group Adaptive Query Processing with Azure SQL DB and SQL Server 2017

Database Programming with SQL

Oracle9i: SQL Tuning Workshop

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Real-World Performance Training SQL Performance

Five Things You Might Not Know About Oracle Database

Query Optimizer MySQL vs. PostgreSQL

Architettura Database Oracle

Firebird in 2011/2012: Development Review

CMSC424: Programming Project

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Query Optimizer MySQL vs. PostgreSQL

Database Programming with PL/SQL

Advanced Oracle SQL Tuning v3.0 by Tanel Poder

Introduction to Query Processing and Query Optimization Techniques. Copyright 2011 Ramez Elmasri and Shamkant Navathe

Exploring Best Practices and Guidelines for Tuning SQL Statements

Oracle. Exam Questions 1Z Oracle Database 11g Release 2: SQL Tuning Exam. Version:Demo

7. Query Processing and Optimization

Oracle Database In-Memory By Example

Institute of Aga. Microsoft SQL Server LECTURER NIYAZ M. SALIH

Query Optimization, part 2: query plans in practice

Relational Database Index Design and the Optimizers

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

Série n 6 Bis : Ateliers SQL Data Modeler (Oracle)

HR Database. Sample Output from TechWriter 2007 for Databases

optimization process (see Optimization process, CBO) RBO, 25 Cursor cache, 41, 42 child, 42 parent, 42

Programming Languages and Techniques (CIS120e)

Parser: SQL parse tree

ORACLE TRAINING CURRICULUM. Relational Databases and Relational Database Management Systems

Sql Server Syllabus. Overview

On Building Integrated and Distributed Database Systems

DB2 9 for z/os Selected Query Performance Enhancements

Join (SQL) - Wikipedia, the free encyclopedia

Physical Design. Elena Baralis, Silvia Chiusano Politecnico di Torino. Phases of database design D B M G. Database Management Systems. Pag.

Oracle ASM Cluster File System (ACFS) 12c Release 2. Ricardo Gonzalez Acuna, Oracle ASM Cluster File System (ACFS) Product Management 31 st May 2017

Review. Relational Query Optimization. Query Optimization Overview (cont) Query Optimization Overview. Cost-based Query Sub-System

.. Spring 2008 CSC 468: DBMS Implementation Alexander Dekhtyar..

Advanced Database Systems

Query Processing and Query Optimization. Prof Monika Shah

OpenWorld 2015 Oracle Par22oning

Adaptive

Kathleen Durant PhD Northeastern University CS Indexes

Tuna Helper Proven Process for SQL Tuning. Dean Richards Senior DBA, Confio Software

Cost Based Optimizer CBO: Configuration Roadmap

Chapter 12: Query Processing

T-SQL Performance Issues What you should know to avoid, find and resolve them. Claire Mora

DumpsKing. Latest exam dumps & reliable dumps VCE & valid certification king

Announcement. Reading Material. Overview of Query Evaluation. Overview of Query Evaluation. Overview of Query Evaluation 9/26/17

Query Optimization Overview

What happens. 376a. Database Design. Execution strategy. Query conversion. Next. Two types of techniques

Algorithms for Query Processing and Optimization. 0. Introduction to Query Processing (1)

Chapter 19 Query Optimization

PERFORMANCE TUNING TRAINING IN BANGALORE

QUERY OPTIMIZATION [CH 15]

Review -Chapter 4. Review -Chapter 5

Ensuring Optimal Performance. Vivek Sharma. 3 rd November 2012 Sangam 2012

Outline. Database Management and Tuning. Outline. Join Strategies Running Example. Index Tuning. Johann Gamper. Unit 6 April 12, 2012

Database Programming with SQL

Administriva. CS 133: Databases. General Themes. Goals for Today. Fall 2018 Lec 11 10/11 Query Evaluation Prof. Beth Trushkowsky

Optimizer Challenges in a Multi-Tenant World

Oracle Database 11g: Performance Tuning DBA Release 2

Database Programming - Section 3. Instructor Guide

Transcription:

Explaining the Explain Plan: Interpre'ng Execu'on Plans for SQL Statements Maria Colgan Master Product Manager June 2017 @SQLMaria

Safe Harbor Statement The following is intended to outline our general product direcqon. It is intended for informaqon purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or funcqonality, and should not be relied upon in making purchasing decisions. The development, release, and Qming of any features or funcqonality described for Oracle s products remains at the sole discreqon of Oracle. 2

Agenda 1 2 3 4 5 What is an execuqon plan How to generate a plan What is a good plan for the OpQmizer Understanding execuqon plans ExecuQon plan examples

What Happens when a SQL statement is issued? User SQL Execution 4 1 Oracle Database 2 Syntax Check SemanQc Check Shared Pool check Parsing Shared Pool Library Cache Shared SQL Area C1 C2 Cn 3 Query Transformation Plan Generator Code Generator Optimizer Cost Estimator

What is an execuqon plan? ExecuQon plans show the detailed steps necessary to execute a SQL statement These steps are expressed as a set of database operators that consumes and produces rows The order of the operators and their implementaqon is decided by the opqmizer using a combinaqon of query transformaqons and physical opqmizaqon techniques The display is commonly shown in a tabular format, but a plan is in fact treeshaped

What is an execuqon plan? Query: SELECT prod_category, avg(amount_sold) FROM sales s, products p WHERE p.prod_id = s.prod_id GROUP BY prod_category; Tabular representaqon of plan Tree-shaped representaqon of plan GROUP BY HASH JOIN TABLE ACCESS PRODUCTS TABLE ACCESS SALES

Agenda 1 2 3 4 5 What is an execuqon plan How to generate a plan What is a good plan for the OpQmizer Understanding execuqon plans ExecuQon plan examples

Many Ways to View an ExecuQon Plan Autotrace SQL Monitor SQL Developer TKPROF..But there are actually only 2 ways to generate one 8

How to generate an execuqon plan Two methods for looking at the execuqon plan 1. EXPLAIN PLAN command Displays an execuqon plan for a SQL statement without actually execuqng the statement 2. V$SQL_PLAN A dicqonary view introduced in Oracle 9i that shows the execuqon plan for a SQL statement that has been compiled into a cursor in the cursor cache Under certain condiqons the plan shown with EXPLAIN PLAN can be different from the plan shown using V$SQL_PLAN

How to generate an execuqon plan EXPLAIN PLAN command & dbms_xplan.display funcqon SQL> EXPLAIN PLAN FOR SELECT p.prod_name, avg(s.amount_sold) FROM WHERE sales s, products p p.prod_id = s.prod_id GROUP BY p.prod_name; SQL> SELECT * FROM table(dbms_xplan.display('plan_table',null,'basic'));

How to generate an execuqon plan Generate & display execuqon plan for last SQL statements executed in session SQL> SELECT p.prod_name, avg(s.amount_sold) FROM sales s, products p WHERE p.prod_id = s.prod_id GROUP BY p.prod_name; SQL> SELECT * FROM table(dbms_xplan.display_cursor(null,null,'basic'));

DBMS_XPLAN parameters DBMS_XPLAN.DISPLAY takes 3 parameters plan table name (default 'PLAN_TABLE'), statement_id (default null), format (default 'TYPICAL') DBMS_XPLAN.DISPLAY_CURSOR takes 3 parameters SQL_ID (default last statement executed in this session), Child number (default 0), format (default 'TYPICAL') Format* is highly customizable - Basic,Typical, All AddiQonal low level parameters show more detail *More informaqon on formatng on OpQmizer blog

Agenda 1 2 3 4 5 What is an execuqon plan How to generate a plan What is a good plan for the OpQmizer Understanding execuqon plans ExecuQon plan examples

What s a good plan for the OpQmizer? The OpQmizer has two different goals Serial execuqon: It s all about cost The cheaper, the beuer Parallel execuqon: it s all about performance The faster, the beuer Two fundamental quesqons: What is cost What is performance

What is cost? A magically number the opqmizer makes up? Resources required to execute a SQL statement? EsQmate of how long it will take to execute a statement? Actual DefiniQon Cost represents units of work or resources used OpQmizer uses CPU & IO as units of work EsQmate of amount of CPU & disk I/Os, used to perform an operaqon Cost is an internal Oracle measurement & should be used for comparison purposes only

What is performance? Getng as many queries completed as possible? Getng fastest possible elapsed Qme using the fewest resources? Getng the best concurrency rate? Actual DefiniQon Performance is fastest possible response Qme for a query Goal is to complete the query as quickly as possible OpQmizer does not focus on resources needed to execute the plan

Agenda 1 2 3 4 5 What is an execuqon plan How to generate a plan What is a good plan for the opqmizer Understanding execuqon plans Cardinality Access paths Join methods Join order ExecuQon plan examples

Cardinality What is it? EsQmate of number rows that will be returned by each operaqon How does the OpQmizer Determine it? Cardinality for a single column equality predicate = total num of rows num of dis'nct values For example: A table has 100 rows, a column has 10 disqnct values => cardinality=10 rows More complicated predicates have more complicated cardinality calculaqon Why should you care? It influences everything! Access method, Join type, Join Order etc

IdenQfying cardinality in an execuqon plan Cardinality - esqmated # of rows returned Determine correct cardinality using a SELECT COUNT(*) from each table applying any WHERE Clause predicates belonging to that table

Checking cardinality esqmates SELECT /*+ gather_plan_sta's'cs */ p.prod_name, SUM(s.quanQty_sold) FROM sales s, products p WHERE s.prod_id =p.prod_id GROUP By p.prod_name ; SELECT * FROM table ( DBMS_XPLAN.DISPLAY_CURSOR(FORMAT=>'ALLSTATS LAST'));

Checking cardinality esqmates SELECT * FROM table ( DBMS_XPLAN.DISPLAY_CURSOR(FORMAT=>'ALLSTATS LAST')); Compare esqmated number of rows returned with actual rows returned

Checking cardinality esqmates for PE SELECT * FROM table ( DBMS_XPLAN.DISPLAY_CURSOR(FORMAT=>'ALLSTATS LAST')); Note: a lot of the data is zero in the A-rows column because we only show last executed cursor which is the QC. Need to use ALLSTATS ALL to see info on all parallel server cursors

Checking cardinality esqmates for PE SELECT * FROM table ( DBMS_XPLAN.DISPLAY_CURSOR(FORMAT=>'ALLSTATS ALL'));

Check cardinality using SQL Monitor Easiest way to compare the esqmated number of rows returned with actual rows returned

SoluQons to incorrect cardinality esqmates Cause Stale or missing staqsqcs Data Skew MulQple single column predicates on a table FuncQon wrapped column MulQple columns used in a join Complicated expression containing columns from mulqple tables Solu'on DBMS_STATS Create a histogram Create a column group using DBMS_STATS.CREATE_EXTENDED_STATS Create staqsqcs on the funct wrapped column using DBMS_STATS.CREATE_EXTENDED_STATS Create a column group on join columns using DBMS_STATS.CREATE_EXTENDED_STAT Use dynamic sampling level 4 or higher

Agenda 1 2 3 4 5 What is an execuqon plan How to generate a plan What is a good plan for the opqmizer Understanding execuqon plans Cardinality Access paths Join methods Join order ExecuQon plan examples

Access paths Getng the data Access Path Full table scan Table access by Rowid Index unique scan Index range scan Index skip scan Full index scan Fast full index scan Index joins Bitmap indexes Explana'on Reads all rows from table & filters out those that do not meet the where clause predicates. Used when no index, DOP set etc Rowid specifies the datafile & data block containing the row and the locaqon of the row in that block. Used if rowid supplied by index or in where clause Only one row will be returned. Used when stmt contains an equality where clause predicate on a UNIQUE or a PRIMARY KEY column, which guarantees that only a single row is accessed Accesses adjacent index entries returns ROWID values Used with equality on non-unique indexes or range predicate on unique index (<.>, between etc) Skips the leading edge of the index & uses the rest Advantageous if there are few disqnct values in the leading column and many disqnct values in the non-leading column Processes all leaf blocks of an index, but only enough branch blocks to find 1 st leaf block. Used when all necessary columns are in index & order by clause matches index struct or if sort merge join is done Scans all blocks in index used to replace a FTS when all necessary columns are in the index. Using mulq-block IO & can going parallel Hash join of several indexes that together contain all the table columns that are referenced in the query. Wont eliminate a sort operaqon uses a bitmap for key values and a mapping funcqon that converts each bit posiqon to a rowid. Can efficiently merge indexes that correspond to several condiqons in a WHERE clause

IdenQfying access paths in an execuqon plan Look in OperaQon secqon to see how an object is being accessed If the wrong access method is being used check cardinality, join order

Access path example 1 What plan would you expect for this query? Table countries contains 10K rows & has a primary key on country_id SELECT country_id, name FROM countries WHERE country_id in ('AU','FR','IE );

Access path example 2 What plan would you expect for this query? Table countries contains 10K rows & has a primary key on country_id SELECT country_id, name FROM countries WHERE country_id between 'AU' and 'IE';

Access path example 3 What plan would you expect for this query? Table countries contains 10K rows & has a primary key on country_id SELECT country_id, name FROM countries WHERE name = USA';

Common access path issues Issue Uses a table scan instead of index Picks wrong index Cause DOP on table but not index, value of MBRC Stale or missing staqsqcs Cost of full index access is cheaper than index look up followed by table access Picks index that matches most # of column

Agenda 1 2 3 4 5 What is an execuqon plan How to generate a plan What is a good plan for the opqmizer Understanding execuqon plans Cardinality Access paths Join methods Join order ExecuQon plan examples

Join methods Join Methods Nested Loops joins Hash Joins Sort Merge joins Explanation For every row in the outer table, Oracle accesses all the rows in the inner table Useful when joining small subsets of data and there is an efficient way to access the second table (index look up) The smaller of two tables is scan and resulting rows are used to build a hash table on the join key in memory. The larger table is then scan, join column of the resulting rows are hashed and the values used to probing the hash table to find the matching rows. Useful for larger tables & if equality predicate Consists of two steps: 1. Sort join operation: Both the inputs are sorted on the join key. 2. Merge join operation: The sorted lists are merged together. Useful when the join condition between two tables is an inequality condition

Join types Join Type Cartesian Joins Outer Joins Explanation Joins every row from one data source with every row from the other data source, creating the Cartesian Product of the two sets. Only good if tables are very small. Only choice if there is no join condition specified in query Returns all rows that satisfy the join condition and also returns all of the rows from the table without the (+) for which no rows from the other table satisfy the join condition

IdenQfying join methods in an execuqon plan Look in the OperaQon secqon to check the right join type is used If wrong join type is used check stmt is wriuen correctly & cardinality esqmates

Join method example 1 What join method would you expect for this query? SELECT e.last_name, e.salary, d.department_name FROM hr.employees e, hr.departments d WHERE d.departments_name IN ('MarkeQng,'Sales') AND e.department_id=d.department_id; Employees has 107 rows Departments has 27 rows Foreign key relaqonship between Employees and Departments on dept_id

Join method example 1 What join method would you expect for this query? SELECT e.last_name, e.salary, d.department_name FROM hr.employees e, hr.departments d WHERE d.departments_name IN ('MarkeQng,'Sales') AND e.department_id=d.department_id;

Join method example 2 What join method would you expect for this query? SELECT o.customer_id, l.unit_price * l.quanqty FROM oe.orders o,oe.order_items l WHERE l.order_id = o.order_id; Orders has 105 rows Order Items has 665 rows

Join method example 2 What join method would you expect for this query? SELECT o.customer_id, l.unit_price * l.quanqty FROM oe.orders o,oe.order_items l WHERE l.order_id = o.order_id; Orders has 105 rows Order Items has 665 rows

Join type example 1 What join type should be use for this query? SELECT o.order_id,0.order_date,e.name FROM oe.orders o, hr.employees e; Orders has 105 rows Employees has 107 rows

Join type example 1 What join type should be use for this query? SELECT o.order_id,0.order_date,e.name FROM oe.orders o, hr.employees e;

Join type example 2 Cartesian product not always bad Chosen when the number of rows being joined is low Commonly used to join mulqple small dimensions to one large fact table PRODUCTS s.prod_id=p.prod_id s.cust_id=c.cust_id CUSTOMERS s.promo_id=c.promo_id PROMOTIONS

Join type example 2 Cartesian product not always bad By joining the three small dimension tables together first we minimize the number of rows being carried through the joins

Join type example 3 What join type should be use for this query? SELECT d.department_id,e.emp_id FROM hr.employees e FULL OUTER JOIN hr.departments d ON e.department_id = d.department_id ORDER BY d.department_id; Employees has 107 rows Departments has 27 rows Foreign key relaqonship between Employees and Departments on dept_id

Join type example 3 What join type should be use for this query? SELECT d.department_id,e.emp_id FROM hr.employees e FULL OUTER JOIN hr.departments d ON e.department_id = d.department_id ORDER BY d.department_id;

Join type example 4 What join type should be use for this query? SELECT s.quanqty_sold FROM sales s, customers c WHERE s.cust_id =c.cust_id; Sales table has 960 Rows Customer table has 55,500 rows Customer has a primary key created on cust_id Sales has a foreign key created on cust_id

Join type example 4 What join type should be use for this query? SELECT s.quanqty_sold FROM sales s, customers c WHERE s.cust_id =c.cust_id; No join is needed Table elimination transformation Optimizer realizes that the join to customers tables is redundant as no columns are selected Presence of primary foreign key relationship means we can remove table

What causes wrong join method to be selected Issue Nested loop selected instead of hash join Hash join selected instead of nested loop Cartesian Joins Cause Cardinality estimate on the left side is under estimated triggers Nested loop to be selected In case of a hash join the Optimizer doesn t taken into consideration the benefit of caching. Rows on the left come in a clustered fashion or (ordered) so the probe in Cardinality underestimation

Agenda 1 2 3 4 5 What is an execuqon plan How to generate a plan What is a good plan for the opqmizer Understanding execuqon plans Cardinality Access paths Join methods Join order ExecuQon plan examples

Join order The order in which the tables are join in a mulq table statement Ideally start with the table that will eliminate the most rows Strongly affected by the access paths available Some basic rules Joins guaranteed to produce at most one row always go first Joins between two row sources that have only one row each When outer joins are used the table with the outer join operator must come a er the other table in the predicate If view merging is not possible all tables in the view will be joined before joining to the tables outside the view

IdenQfying join order in an execuqon plan 1 2 3 4 5 Want to start with the table that reduce the result set the most If the join order is not correct, check the staqsqcs, cardinality & access methods

Finding the join order for complex SQL It can be hard to determine Join Order for Complex SQL statements but it is easily visible in the outline data of plan SELECT * FROM table(dbms_xplan.display_cursor(format=> TYPICAL +outline ); The leading hint tells you the join order

What causes the wrong join order Causes Incorrect single table cardinality estimates Incorrect join cardinality estimates

Agenda 1 2 3 4 5 What is an execuqon plan How to generate a plan What is a good plan for the opqmizer Understanding execuqon plans ExecuQon plan examples

Example SQL Statement Find all the employees who make as much or more than their manager SELECT e1.last_name, e1.job_qtle, e1.total_comp FROM (SELECT e.manager_id, e.last_name, j.job_qtle, e.salary+(e.salary+e.commission_pct) total_comp FROM employees e, jobs j, departments d WHERE d.department_name = 'Sales' AND e.department_id = d.department_id AND e.job_id = j.job_id ) e1, (SELECT e.employee_id, e.salary+(e.salary+e.commission_pct) tc FROM employees e, departments d WHERE d.department_name = Sales' AND e.department_id = d.department_id ) e2 WHERE e1.manager_id = e2.employee_id AND e1.total_comp >= e2.tc;

Is it a good execuqon plan? 1. Is the esqmated number of rows being returned accurate? 3.Are the access method correct? 2. Are the cardinality esqmates accurate? Means no stats gathered strong indicator this won t be best possible plan

Example cont d execuqon plan 5 4 1 2 3 4. Are the right join methods being used? 5. Is the join order correct? Is the table that eliminates the most rows accessed first?

What does the plan tree look like? NESTED LOOP NESTED LOOP INDEX UNIQUE SCAN TABLE ACCESS JOBS HASH JOIN INDEX UNIQUE SCAN TABLE ACCESS DEPARTMENT MERGE JOIN CARTESIAN TABLE ACCESS EMPLOYEES TABLE ACCESS DEPARTMENT TABLE ACCESS EMPLOYEES

SoluQon 1. Only 1 row is actually returned and the cost is 4 lower now 5. The join order has changed 5 4 3 1 2 4. Join methods have changed to be all NL 3. Access methods have changed for some tables 2. CardinaliQes are correct and with each join number of rows reduced

What does the plan tree look like? NESTED LOOP NESTED LOOP INDEX UNIQUE SCAN TABLE ACCESS DEPARTMENT NESTED LOOP INDEX UNIQUE SCAN - TABLE ACCESS JOBS NESTED LOOP INDEX RANGE SCAN - TABLE ACCESS EMPLOYEES TABLE ACCESS DEPARTMENT INDEX RANGE SCAN TABLE ACCESS EMPLOYEES

AddiQonal Resources Join the Conversa'on hups://twiuer.com/sqlmaria hups://blogs.oracle.com/opqmizer/ hups://sqlmaria.com hups://www.facebook.com/oracledatabase hups://www.facebook.com/sqlmaria Related White Papers Explain the Explain Plan Understanding OpQmizer StaQsQcs Best PracQces for Gathering OpQmizer StaQsQcs What to expect from the OpQmizer in 12c What to expect from the OpQmizer in 11g Related Websites hup://www.oracle.com/technetwork/database/ database-technologies/query-opqmizaqon/overview/ index.html 62