Oracle DB-Tuning Essentials

Similar documents
Join Methods. Franck Pachot CERN

Oracle DB-Tuning Essentials

Suppose we need to get/retrieve the data from multiple columns which exists in multiple tables...then we use joins..

Interpreting Explain Plan Output. John Mullins

Query Optimization, part 2: query plans in practice

Real-World Performance Training SQL Performance

Databases - 4. Other relational operations and DDL. How to write RA expressions for dummies

DRAFT SOLUTION. Query 1(a) DATABASE MANAGEMENT SYSTEMS PRACTICE 1

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

Databases IIB: DBMS-Implementation Exercise Sheet 13

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

Real-World Performance Training SQL Introduction

Advanced Oracle SQL Tuning v3.0 by Tanel Poder

Hints (definition 1) on Hints (definition 2)

CS2 Current Technologies Lecture 3: SQL - Joins and Subqueries

Neil Chandler, Chandler Systems Oracle & SQL Server DBA

Objectives. After completing this lesson, you should be able to do the following:

Top 5 Issues that Cannot be Resolved by DBAs (other than missed bind variables)

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

When should an index be used?

Real-World Performance Training SQL Performance

@vmahawar. Agenda Topics Quiz Useful Links

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

Oracle Database 11gR2 Optimizer Insights

A Unit of SequelGate Innovative Technologies Pvt. Ltd. All Training Sessions are Completely Practical & Real-time

Join (SQL) - Wikipedia, the free encyclopedia

Databases - 3. Null, Cartesian Product and Join. Null Null is a value that we use when. Something will never have a value

Parallel Execution Plans

Databases - 3. Null, Cartesian Product and Join. Null Null is a value that we use when. Something will never have a value

Oracle Database 18c. Gentle introduction to Polymorphic Tables Functions with Common patterns and sample use cases

Advanced Database Systems

Cost Based Optimizer CBO: Configuration Roadmap

Query Processing and Query Optimization. Prof Monika Shah

Creating and Managing Tables Schedule: Timing Topic

RNDr. Michal Kopecký, Ph.D. Department of Software Engineering, Faculty of Mathematics and Physics, Charles University in Prague

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

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

Databases. Relational Model, Algebra and operations. How do we model and manipulate complex data structures inside a computer system? Until

Wolfgang Breitling.

Independent consultant. Oracle ACE Director. Member of OakTable Network. Available for consulting In-house workshops. Performance Troubleshooting

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

Outline. Database Tuning. Join Strategies Running Example. Outline. Index Tuning. Nikolaus Augsten. Unit 6 WS 2014/2015

Independent consultant. Oracle ACE Director. Member of OakTable Network. Available for consulting In-house workshops. Performance Troubleshooting

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

ORACLE TRAINING CURRICULUM. Relational Databases and Relational Database Management Systems

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

IT100: Oracle Administration

Advanced Oracle Performance Troubleshooting. Query Transformations Randolf Geist

Oracle Database 11g: SQL Tuning Workshop

SQL. Char (30) can store ram, ramji007 or 80- b

Data about data is database Select correct option: True False Partially True None of the Above

CS2 Current Technologies Note 1 CS2Bh

Querying Data with Transact SQL

Oracle PL/SQL - 12c & 11g [Basic PL/SQL & Advanced PL/SQL]

7. Query Processing and Optimization

ENHANCING DATABASE PERFORMANCE

SQL Tuning via Toad Tips for Optimizing SQL Performance

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Does Exadata Need Performance Tuning? Jože Senegačnik, Oracle ACE Director, Member of OakTable DbProf d.o.o. Ljubljana, Slovenia

Oracle 9i Application Development and Tuning

Exploring Best Practices and Guidelines for Tuning SQL Statements

Part III. Data Modelling. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 1

Parallel Execution with Oracle Database 18c Fundamentals ORACLE WHITE PAPER FEBRUARY 2018

Tuning Considerations for Different Applications Lesson 4

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

ORACLE VIEWS ORACLE VIEWS. Techgoeasy.com

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

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

Oracle SQL Tuning for Developers Workshop Student Guide - Volume I

Components of a DMBS

Overview of Query Processing. Evaluation of Relational Operations. Why Sort? Outline. Two-Way External Merge Sort. 2-Way Sort: Requires 3 Buffer Pages

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

Oracle Class VII More on Exception Sequence Triggers RowID & Rownum Views

Objectives. After completing this lesson, you should be able to do the following:

(Storage System) Access Methods Buffer Manager

QUERY OPTIMIZATION [CH 15]

Infrastructure at your Service. In-Memory-Pläne für den 12.2-Optimizer: Teuer oder billig?

Application Containers an Introduction

Oracle Database 11g: SQL Tuning Workshop. Student Guide

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course:

Oracle Database Performance Tuning, Benchmarks & Replication

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

Explaining the Explain Plan:

PASS4TEST. IT Certification Guaranteed, The Easy Way! We offer free update service for one year

1 SQL Structured Query Language

COPYRIGHTED MATERIAL. Using SQL. The Processing Cycle for SQL Statements

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

Column Stores vs. Row Stores How Different Are They Really?

SQL Structured Query Language Introduction

KillTest *KIJGT 3WCNKV[ $GVVGT 5GTXKEG Q&A NZZV ]]] QORRZKYZ IUS =K ULLKX LXKK [VJGZK YKX\OIK LUX UTK _KGX

Oracle Sql Tuning- A Framework

CSIT5300: Advanced Database Systems

King Fahd University of Petroleum and Minerals

Data Modeling and Databases Ch 10: Query Processing - Algorithms. Gustavo Alonso Systems Group Department of Computer Science ETH Zürich

Vijay Mahawar

Application Containers an Introduction

Scaling To Infinity: Making Star Transformations Sing. Thursday 15-November 2012 Tim Gorman

1 SQL Structured Query Language

Oracle 1Z0-054 Exam Questions and Answers (PDF) Oracle 1Z0-054 Exam Questions 1Z0-054 BrainDumps

Chapter 12: Query Processing

Transcription:

Infrastructure at your Service. Oracle DB-Tuning Essentials

Agenda 1. The DB server and the tuning environment 2. Objective, Tuning versus Troubleshooting, Cost Based Optimizer 3. Object statistics 4. Access paths I 5. Access paths II 6. Monitoring Performance I 7. Monitoring Performance II 8. Application setup Page 2

Access Path > Table Access > Index Access > Join Methods > Sorts and Hash > Hints > Parallel Query Page 3

Join Methods What is a join? Relational database > Stores entities in separate tables > Tables are usually stored physically separate > Queries require columns from several tables > We need to join tables Older syntax > As if we do a Cartesian product, and then filter with predicates ANSII syntax > Join condition is explicit select * from DEPT, EMP where DEPT.DEPTNO=EMP.DEPTNO select * from DEPT join EMP on DEPT.DEPTNO=EMP.DEPTNO > more powerful, more standard > unfortunately still some bugs with it > Don't mix the syntax in the same query! Page 4

Join Methods What is a join? https://www.codeproject.com/articles/33052/visual-representation-of-sql-joins Page 5

Join Methods Equi-join and Theta-joins Equi-join > The join condition is an equality select * from DEPT join EMP on DEPT.DEPTNO = EMP.DEPTNO Theta-join > The join condition is usually an inequality (e.g. <, >, >=) select * from EMP e join EMP n on n.hiredate >= e.hiredate Join cardinality > (1..1) always have one inner row matching each outer row (not null FK) > (0..1) may have one inner row matching each outer row (nullable FK) > (0..n) can increase the number of rows (up to m * n if Cartesian product) > Inner join (the default) returns only rows that matches, and all rows that match Page 6

Join Methods Outer-join, Semi-join and Anti-join Outer-join > Returns a row from the outer join side even when there s no match select * from DEPT left outer join EMP on DEPT.DEPTNO=EMP.DEPTNO select * from DEPT, EMP on DEPT.DEPTNO(+)=EMP.DEPTNO Semi-join > Only one row is returned even if several do match select * from DEPT where exists ( select * from EMP where DEPT.DEPTNO=EMP.DEPTNO ) select * from DEPT where deptno in ( select deptno from EMP E ) Anti-join > Return rows that do no match select * from DEPT where not exists ( select * from EMP where DEPT.DEPTNO=EMP.DEPTNO ) select * from DEPT where deptno not in ( select deptno from EMP E where deptno is not null ) Page 7

Join Methods Nested Loop Join Table EMPNO ENAME DEPTNO #1 7369 SMITH 20 #2 7499 ALLEN 30 outer outer DEPTNO DNAME Index #3 7521 WARD 30 #4 7566 JONES 20 outer outer inner inner 10 ACCOUNTING 20 RESEARCH 30 SALES 40 OPERATIONS Nested Loop Join > n rows from the outer table, access the inner table n times > Good when having efficient access to the inner table (index, cluster, partition) > Bad if too many rows from outer table EMPNO ENAME DNAME #1 7369 SMITH RESEARCH #2 7499 ALLEN SALES #3 7521 WARD SALES #4 7566 JONES RESEARCH Page 8

Join Methods Nested Loop Join + table access by rowid Table EMPNO ENAME DEPTNO #1 7369 SMITH 20 #2 7499 ALLEN 30 #3 7521 WARD 30 #4 7566 JONES 20 Table access by rowid Table access by rowid outer outer outer inner outer inner DEPTNO DNAME #1 30 SALES #2 40 OPERATIONS #3 20 RESEARCH #4 10 ACCOUNTING DEPTNO rowid 10 #4 20 #3 30 #1 40 #2 Index Table Nested Loop Join > Often requires to go to the table row by row > Very bad if too many rows > Index should be highly selective > and have a good clustering factor EMPNO ENAME DNAME #1 7369 SMITH RESEARCH #2 7499 ALLEN SALES #3 7521 WARD SALES #4 7566 JONES RESEARCH Page 9

Join Methods Nested Loop execution plan (10g) select * from DEPT join EMP using(deptno) where sal>=3000 ---------------------------------------------------------------------------- Id Operation Name Starts A-Rows Buffers ---------------------------------------------------------------------------- 0 SELECT STATEMENT 1 3 13 1 NESTED LOOPS 1 3 13 * 2 TABLE ACCESS FULL EMP 1 3 8 3 TABLE ACCESS BY INDEX ROWID DEPT 3 3 5 * 4 INDEX UNIQUE SCAN PK_DEPT 3 3 2 ---------------------------------------------------------------------------- Nested Loop > inner table (EMP) has 3 rows after applying the sal>=3000 predicate > for each of them the DEPT is accessed by a rowid Page 10

Join Methods Nested Loop execution plan (11g) select * from DEPT join EMP using(deptno) where sal>=3000 ---------------------------------------------------------------------------- Id Operation Name Starts A-Rows Buffers ---------------------------------------------------------------------------- 0 SELECT STATEMENT 1 3 13 1 NESTED LOOPS 1 3 13 2 NESTED LOOPS 1 3 10 * 3 TABLE ACCESS FULL EMP 1 3 8 * 4 INDEX UNIQUE SCAN PK_DEPT 3 3 2 5 TABLE ACCESS BY INDEX ROWID DEPT 3 3 3 ---------------------------------------------------------------------------- Join Batching > A first loop retrieves a vector of rowid > A second loop retrieves the rows by batch Page 11

Join Methods Sort Merge Join Table SORT EMPNO DNAME ENAME DEPTNO #1 7369 SMITH 20 #2 #4 7499 7566 ALLEN JONES 30 20 #3 #2 7521 7499 WARD ALLEN 30 #4 #3 7566 7521 JONES WARD 20 30 outer outer outer outer inner inner inner SORT Table DEPTNO DNAME 10 ACCOUNTING 20 RESEARCH 30 SALES 40 OPERATIONS Sort Merge Join > Requires sorted input > Good when we need a sorted result anyway > Replaces hash join for theta joins EMPNO ENAME DNAME #1 7369 SMITH RESEARCH #2 7499 ALLEN SALES #3 7521 WARD SALES #4 7566 JONES RESEARCH Page 12

Join Methods Sort Merge join select * from DEPT join EMP on (EMP.deptno between DEPT.deptno and DEPT.deptno+10 ) where sal>=3000 ------------------------------------------------------------------------- Id Operation Name Starts A-Rows Buffers OMem ------------------------------------------------------------------------- 0 SELECT STATEMENT 1 5 14 1 MERGE JOIN 1 5 14 2 SORT JOIN 1 3 7 2048 * 3 TABLE ACCESS FULL EMP 1 3 7 * 4 FILTER 3 5 7 * 5 SORT JOIN 3 5 7 2048 6 TABLE ACCESS FULL DEPT 1 4 7 ------------------------------------------------------------------------- Sort Merge join > Both sources are ordered, and then merged Page 13

Join Methods Sort Merge join select * from DEPT join EMP using(deptno) where sal>=3000 order by deptno --------------------------------------------------------------------- Id Operation Name Starts A-Rows --------------------------------------------------------------------- 0 SELECT STATEMENT 1 3 1 MERGE JOIN 1 3 * 2 TABLE ACCESS BY INDEX ROWID EMP 1 3 3 INDEX FULL SCAN EMP_DEPTNO 1 14 * 4 SORT JOIN 3 3 5 TABLE ACCESS FULL DEPT 1 4 --------------------------------------------------------------------- Sort Merge join without sorting > When the first row source is already sorted, no need to sort again > The second source will always need a buffer Page 14

Join Methods Hash Join Table EMPNO ENAME DEPTNO #1 7369 SMITH 20 #2 7499 ALLEN 30 outer outer HASH DEPTNO DNAME Table #3 7521 WARD 30 #4 7566 JONES 20 outer outer inner inner 10 ACCOUNTING 20 RESEARCH 30 SALES 40 OPERATIONS Hash Join > Builds a hash table of the smaller input > Longer to get the first result row > But faster access later > Better when the smaller table fits in memory EMPNO ENAME DNAME #1 7369 SMITH RESEARCH #2 7499 ALLEN SALES #3 7521 WARD SALES #4 7566 JONES RESEARCH Page 15

Join Methods Hash Join select * from DEPT join EMP using(deptno) ----------------------------------------------------------------------- Id Operation Name Starts A-Rows Buffers OMem ----------------------------------------------------------------------- 0 SELECT STATEMENT 1 14 15 * 1 HASH JOIN 1 14 15 1321K 2 TABLE ACCESS FULL DEPT 1 4 7 3 TABLE ACCESS FULL EMP 1 14 8 ----------------------------------------------------------------------- Hash Join > Builds a hash table with the inner row source > Probes it from the outer row source The order of tables can be swapped > The smaller one is chosen to be the hash table Page 16

Join Methods Merge Join Cartesian Table EMPNO ENAME DEPTNO #1 7369 SMITH 20 #2 7499 ALLEN 30 outer outer inner BUFFER DEPTNO DNAME Table #3 7521 WARD 30 #4 7566 JONES 20 outer outer 10 ACCOUNTING 20 RESEARCH 30 SALES 40 OPERATIONS Merge Join Cartesian > only between very small rowsources > When most of the rows matches > But disastrous if misestimate rows=1 EMPNO ENAME DNAME #1 7369 SMITH RESEARCH #2 7499 ALLEN SALES #3 7521 WARD SALES #4 7566 JONES RESEARCH Page 17

Join Methods Merge join Cartesian select /*+ leading(dept EMP) use_merge_cartesian(emp) */ * from DEPT join EMP using(deptno) where sal>3000 ------------------------------------------------------------------------- Id Operation Name Starts A-Rows Buffers OMem ------------------------------------------------------------------------- 0 SELECT STATEMENT 1 15 1 MERGE JOIN CARTESIAN 1 1 15 2 TABLE ACCESS FULL DEPT 1 4 8 3 BUFFER SORT 4 1 7 2048 * 4 TABLE ACCESS FULL EMP 1 1 7 ------------------------------------------------------------------------- Merge Join Cartesian > When most of rows matches > Buffers the outer table to avoid several full scan > The BUFFER SORT is only a buffer, not a sort Page 18

Join Methods Exercise Check number of lines from SALES and CUSTOMERS SQL> connect demo/demo SQL> select table_name,num_rows,blocks from user_tables where table_name in('sales','customers'); Run a join with different number of rows SQL> set autotrace on SQL> select count(*),min(cust_year_of_birth),max(cust_year_of_birth) from SALES join CUSTOMERS using(cust_id) where amount>10; SQL> select count(*),min(cust_year_of_birth),max(cust_year_of_birth) from SALES join CUSTOMERS using(cust_id) where prod_id=20 and quantity>1; Page 19

Join Methods Adaptive Plans The execution plan contains two different paths > Oracle evaluates an inflection point and will choose during the execution > New operation statistics collector will buffer rows during table scan Nested Loop Hash Join Stats collector Table scan Index scan Table scan TABLE 1 TABLE 2 Page 20

Join Methods Adaptive Plans If the number of rows is less than the inflection point > Oracle executes the default plan > All needed rows have been read and they are sent to the next operation Nested Loop Hash Join Stats collector Table scan Index scan Table scan TABLE 1 TABLE 2 Page 21

Join Methods Adaptive Plans If the number of rows is bigger than an inflection point > Oracle switches to the alternative plan > Statistics collector is disabled and rows are sent to the next operation Nested Loop Hash Join Stats collector Table scan Index scan Table scan TABLE 1 TABLE 2 Page 22

Join Methods Adaptive Plans Plan is adaptive only on first execution Two adaptive methods > Join method > Parallel distribution New information with dbms_xplan package > Displays a note when execution plan is adaptive Note ----- - this is an adaptive plan New option to display adaptive steps in execution plans SQL> select * from table ( dbms_xplan.display(format=>'last ADAPTIVE')); Page 23

Join Methods Demo: Adaptive Plans Objective > Display the default execution plan > Display full execution plan including adaptive steps > Execute the statement > Display the executed execution plan Statement used for the exercise > Schema HR SQL> select distinct DEPARTMENT_NAME from DEPARTMENTS join EMPLOYEES using(department_id) where LOCATION_ID like '%0' and SALARY>2000; > A lot of location ids end with 0 but the optimizer is not aware of it. Page 24

Access Path > Table Access > Index Access > Join Methods > Sorts and Hash > Hints > Parallel Query Page 25

Sorts and Hash Workarea for ORDER BY, GROUP BY and Hash Joins Some operations need to buffer data > Sort rows (ORDER BY, Index creation) > Deduplicate rows (DISTINCT) > Build hash tables (HASH JOIN) > The memory is private to the process: PGA > When larger than memory swap to tempfiles workarea_size_policy=auto > The workarea will be allocated, Oracle try s to keep the sum of all workareas around pga_aggregate_target > Limited by internal values (pga limited to 200MB per process) workarea_size_policy=manual > The workarea is defined by sort_area_size, hash_area_size Page 26

Sorts and Hash Workarea (optimal) 106 107 101 108 buffer 106 107 101 108 sort 101 106 107 108 When all the rows (workarea) fit in memory (PGA) > They are sorted in memory only (optimal) If they do not fit: > Buffers will be stored in tempfiles, then chunks are merged (onepass) Merge itself needs space, if not enough memory: > The merge put new (larger) chunks in tempfile (multipass) Page 27

Sorts and Hash Workarea (one-pass) 106 107 101 108 104 104 105 102 107 100 103 109 108 102 buffer buffer buffer buffer 106 107 101 108 104 104 105 102 107 100 103 109 108 102 Sort to tempfile Sort to tempfile Sort to tempfile Sort to tempfile 101 106 107 108 102 104 104 105 100 107 103 103 107 109 102 108 101 102 100 102 106 104 104 103 sort sort 100 101 102 102 103 104 104 106 Page 28

Sorts and Hash Workarea (multi-pass) 106 107 101 108 104 104 105 102 107 100 103 109 108 102 103 105 1 st pass Sort to tempfile 101 106 107 108 102 104 104 105 100 107 103 103 104 109 102 108 103 105 108 106 104 104 103 103 105 104 Sort to tempfile Sort to tempfile The merge put new (larger) chunks in tempfile which are merged again To be avoided (set larger PGA target) 2 nd pass 103 104 104 106 103 105 104 sort sort sort sort 100 101 102 102 103 103 103 104 104 105 105 105 106 107 107 108 108 109 Page 29

Sorts and Hash How to estimate the size? Explain Plan shows the estimated workarea usage: SQL> explain plan for select * from demo.test order by a; SQL> select * from table(dbms_xplan.display); ----------------------------------------------------------- Id Operation Name Rows Bytes TempSpc ----------------------------------------------------------- 0 SELECT STATEMENT 1000 3907K 1 SORT ORDER BY 1000 3907K 4008K 2 TABLE ACCESS FULL TEST 1000 3907K ----------------------------------------------------------- Frequent small sorts/hash > Should be optimal Large sorts/hash > Can be done in 1 pass (batch, reporting) > Multipass or high tempfile usage is probably sign of poor execution plan > Think about partitioning, parallel query, manual sort_area_size Page 30

Sorts and Hash How to view the size? For current execution: > V$SQL_WORKAREA_ACTIVE For recent execution: > dbms_xplan.display_cursor(format=> memstat last ) --------------------------------------- OMem 1Mem Used-Mem Used-Tmp --------------------------------------- 4500K 893K 509K (1) 4096 --------------------------------------- Note that Used-Tmp is in KB instead of bytes (bug not fixed yet) For past execution, or for failed statements: > SQL Monitoring or ASH (TEMP_SPACE_ALLOCATED) Page 31

Sorts and Hash Workarea size recommendation Recommended value > Workarea_policy=auto at system level > Good for OLTP > Still good for large volume if Parallel Query is used Workarea_policy=manual > When we can allocate 1 our 2 GB > At session level only, and controlling the total number of sessions > Set sort_area_size and hash_area_size as needed Always be sure that enough physical RAM is available Page 32

Sorts and Hash Exercise: manual workarea size 1. Run the following: SQL> set linesize 1000 pagesize 1000 SQL> alter session set current_schema=sh; SQL> alter session set statistics_level=all; SQL> select * from ( select SALES.*,row_number()over(order by amount_sold) rn from SALES ) where rn=1e6; SQL> select * from table( dbms_xplan.display_cursor(format=>'allstats last') ); > What is the size of the workarea? 2. Run it with different manual workarea sizes: SQL> alter session set workarea_size_policy=manual sort_area_size=200000000; SQL> alter session set workarea_size_policy=manual sort_area_size=2000000; SQL> alter session set workarea_size_policy=manual sort_area_size=200000; Page 33

Access Path > Table Access > Index Access > Join Methods > Sorts and Hash > Hints > Parallel Query Page 34

Hints Why do we need hints? SQL is a declarative language > We specify only WHAT we want, not the way to do it > The optimizer chooses HOW to get it Hints are specified as special comments /*+ */ > Specify a special behavior ( APPEND, PARALLEL ) > Specify additional behavior ( RESULT_CACHE, MONITOR ) > Change parameters at query level ( FIRST_ROWS, DYNAMIC_SAMPLING ) > Test a different execution plan ( joins, access, transformations ) > Least resort to workaround a bad CBO choice Hint behavior change through versions: Document then, re-evaluate them at each upgrade Page 35

Hints LEADING and joins The CBO evaluates joins one after the other > Don t specify a join method without the table order > USE_HASH(EMP) means use Hash Join if joining to EMP but you can still have a Nested Loop joining EMP to DEPT > LEADING(DEPT EMP) USE_HASH(EMP) means: start with DEPT and join to EMP using Hash Join method but the CBO will still choose which table is the hashed one > LEADING(DEPT EMP) USE_HASH(EMP) NO_SWAP_JOIN_INPUTS(EMP) if you want to be sure to have DEPT as the hash table. > LEADING(DEPT EMP) USE_HASH(EMP) SWAP_JOIN_INPUTS(EMP) if you want to be sure to have EMP as the hash table. Page 36

Hints Explain Plan Explain Plan shows the hints SQL> select * from table(dbms_xplan.display_cursor(format=>'+outline')); Outline Data ------------- /*+ BEGIN_OUTLINE_DATA IGNORE_OPTIM_EMBEDDED_HINTS OPTIMIZER_FEATURES_ENABLE('11.2.0.4') DB_VERSION('11.2.0.4') ALL_ROWS OUTLINE_LEAF(@"SEL$1") FULL(@"SEL$1" "EMP"@"SEL$1") INDEX(@"SEL$1" "DEPT"@"SEL$1" ("DEPT"."DEPTNO")) LEADING(@"SEL$1" "EMP"@"SEL$1" "DEPT"@"SEL$1") USE_NL(@"SEL$1" "DEPT"@"SEL$1") NLJ_BATCHING(@"SEL$1" "DEPT"@"SEL$1") END_OUTLINE_DATA */ Page 37

Hints Explain Plan Explain Plan shows the hints ------------------------------------------------ Id Operation Name ------------------------------------------------ 0 SELECT STATEMENT 1 NESTED LOOPS 2 * 3 NESTED LOOPS TABLE ACCESS FULL EMP * 4 INDEX UNIQUE SCAN PK_DEPT 5 TABLE ACCESS BY INDEX ROWID DEPT ------------------------------------------------ FULL(@"SEL$1" "EMP"@"SEL$1") INDEX(@"SEL$1" "DEPT"@"SEL$1" ("DEPT"."DEPTNO")) LEADING(@"SEL$1" "EMP"@"SEL$1" "DEPT"@"SEL$1") USE_NL(@"SEL$1" "DEPT"@"SEL$1") Page 38

Hints Few important hints FIRST_ROWS(n) > Optimize to get first rows quickly (pagination) often favors nested loops and index access RESULT_CACHE > Enables result caching at statement level APPEND > Inserts after High Water Mark direct path, don t reuse free space, locks the table DRIVING_SITE > Determines where the joins are done when involving db links When DML, the driving site is always where the DML is done Page 39

Hints Exercise: Hinting Joins 1. Run the following SQL> connect / as sysdba SQL> alter session set current_schema=scott statistics_level=all; SQL> select * from dept join emp using(deptno) join bonus using(ename); > What is the join method chosen? 2. Add hints to the query in order to get 1. Full scan EMP 2. For each employee, get the DEPT by index 3. For each employee get to the BONUS by hash (the hashed table being BONUS) Solution: the plan hash value should be 135669890 3. Show all the hints with the +outline format Page 40

Hints Exercise: First Rows 1. Run the following 2. Then run the same with FIRST_ROWS(10) hint > What is the difference? > Which is estimated to be faster? > Which is actually faster? 3. Compare the two following syntax: > What is the difference? SQL> connect demo/demo SQL> set autotrace trace SQL> select * from customers order by cust_year_of_birth; SQL> select * from (select * from customers order by cust_year_of_birth) where rownum<=10; SQL> select * from customers order by cust_year_of_birth fetch first 10 rows only; Page 41

Access Path > Table Access > Index Access > Join Methods > Sorts and Hash > Hints > Parallel Query Page 42

Parallel Query Introduction In serial, all the work is done by your session process > You use at most one CPU > Today, servers have more CPUs, but not faster ones In parallel your session process is the coordinator > Divides the job (e.g. divides full table scan in chunks) > Use instance parallel processes (P00n) to do part of the job > Do the serial operations > Returns the result Parallel processes communicate with others or with coordinator > May have 2 sets: producers and consumers > PX SEND and PX RECEIVE Page 43

Parallel Query Full Table Scan Coordinator process: ----------------------------------------------------------------------- Id Operation Name Starts E-Rows A-Rows ----------------------------------------------------------------------- 0 SELECT STATEMENT 1 1 1 SORT AGGREGATE 1 1 1 2 PX COORDINATOR 1 8 3 PX SEND QC (RANDOM) :TQ10000 0 1 0 4 SORT AGGREGATE 0 1 0 5 PX BLOCK ITERATOR 0 55500 0 * 6 TABLE ACCESS FULL CUSTOMERS 0 55500 0 ----------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 6 - access(:z>=:z AND :Z<=:Z) > allstats last shows only the last execution and this is only the coordinator statistics. Operations done by parallel processes are with starts=0 Page 44

Parallel Query Full Table Scan Parallel processes: ----------------------------------------------------------------------- Id Operation Name Starts E-Rows A-Rows ----------------------------------------------------------------------- 0 SELECT STATEMENT 1 1 1 SORT AGGREGATE 1 1 1 2 PX COORDINATOR 1 8 3 PX SEND QC (RANDOM) :TQ10000 0 1 0 4 SORT AGGREGATE 8 1 8 5 PX BLOCK ITERATOR 8 55500 55500 * 6 TABLE ACCESS FULL CUSTOMERS 77 55500 55500 ----------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 6 - access(:z>=:z AND :Z<=:Z) > allstats sums all process statistics: > 1 coordinator doing steps 0 to 2 > 8 parallel processes, each doing doing steps 3 to 6 > 77 chunks (ranges of rowid) processed by 8 processes Page 45

Parallel Query Demo: PX Full Table Scan 1. Run the following query 2. Then run in parallel, with DOP = 8 > At query level with /*+ PARALLEL(CUSTOMERS 8) */ hint > Or alter table CUSTOMERS parallel 8 > Or alter session force parallel query parallel 8 3. Show execution plan SQL> connect demo/demo SQL> select max(cust_first_name) from customers; SQL> select * from table( dbms_xplan.display_cursor(format=>'basic allstats -note') ); Page 46

Parallel Query Joins When joining a table that is read in parallel > Rows from one set (producer) must be sent to one other set (consumer) > Each set has a DOP, so with DOP=8 we may have 16 processes Distribution can be > Broadcast a small table > Distribute a large table: more messaging but less reads New feature in 12c > The choice is adaptive at execution time Page 47

Parallel Query Hybrid Distribution > We see that the DOP is 4 > There are 4 rows coming from DEPT > 16 rows has been received by the consumer > Conclusion: broadcast happened Page 48

Access Path Core Message The optimizer chooses the access path > But we have to provide the right structures, and accurate statistics > Hints are not a long-term solution and must be documented A bad join can change the response time to 1000x > Adaptive join helps, but doesn t solve all issues Indexing must be done at design time > Too many indexes is overhead for inserts, updates, deletes > Too few probably miss some efficient access path for queries Any questions? Please do ask. Page 49