pvyhlidal 2014 sql tuning

100
SQL Tuning

Upload: ivan-pavlov

Post on 21-Jul-2016

23 views

Category:

Documents


1 download

DESCRIPTION

Introduction to SQL TuningIntroduction to OptimizerOptimize OperatorsInterpreting Execution plans

TRANSCRIPT

Page 1: Pvyhlidal 2014 SQL Tuning

SQL Tuning

Page 2: Pvyhlidal 2014 SQL Tuning

Agenda

● Introduction to SQL Tuning

● Introduction to Optimizer

● Optimize Operators

● Interpreting Execution plans

Page 3: Pvyhlidal 2014 SQL Tuning

Introduction to SQL tuning

Page 4: Pvyhlidal 2014 SQL Tuning

Introduction to SQL tuning

● Reasons for inefficient SQL performace

– Stale or missing optimizer statistics

– Missing access structures

– Suboptimal execution plan selection

– Poorly constructed SQL

Page 5: Pvyhlidal 2014 SQL Tuning

Introduction to SQL tuning

● Inefficient SQL examples

Page 6: Pvyhlidal 2014 SQL Tuning

Introduction to SQL tuning

Tuning tools overview

– SQL Tuning advisor

– SQL Tuning sets

– SQL Access Advisor

– SQL Performance Analyzer

– SQL Monitoring

– SQL Plan management

Page 7: Pvyhlidal 2014 SQL Tuning

Introduction to SQL tuning

Tuning tasks overview

– Identifying high-load SQL

– Gathering statistics

– Generating system statistics

– Rebuilding existing indexes

– Maintaining execution plans

– Creating new index strategies

Page 8: Pvyhlidal 2014 SQL Tuning

Introduction to SQL tuning Common mistakes on Customers Systems

– Bad connection management

– Bad use of cursors and the shared pool

– Excess of resources consuming SQL statements

– Use of nonstandard initialization parameters

– Poor database disk configuration

– Redo log setup problems

– Excessive serialization

– Inappropriate full table scans

– Large number of recursive SQL statements related to space management or parsing activity

– Deployment and migration errors

Page 9: Pvyhlidal 2014 SQL Tuning

Introduction to SQL tuning Proactive tuning methodology

– Simple design

– Data modeling

– Tables and indexes

– Using views

– Writing efficient SQL

– Cursor sharing

– Using bind variables

Page 10: Pvyhlidal 2014 SQL Tuning

Introduction to SQL tuning

● Simplicity in Application Design

– Simple tables

– Well-written SQL

– Indexing only as required

– Retrieving only required information

Page 11: Pvyhlidal 2014 SQL Tuning

Introduction to SQL tuning

● Data Modeling

– Accurately represent business practices

– Focus on the most frequent and important business

– transactions

– Use modeling tools

– Appropriately normalize data (OLTP versus DW)

Page 12: Pvyhlidal 2014 SQL Tuning

Introduction to SQL tuning

● Table design

Compromise between flexibility and performance:

● Principally normalize

● Selectively denormalize

– Use Oracle performance and management features:

● Default values

● Constraints

● Materialized views

● Clusters

● Partitioning

– Focus on business-critical tables

Page 13: Pvyhlidal 2014 SQL Tuning

Introduction to SQL tuning

● Index design

– Create indexes on the following

● Primary key (can be automatically created)

● Unique key (can be automatically created)

● Foreign keys (good candidates)

– Index data that is frequently queried (select list)

– Use SQL as a guide to index design

Page 14: Pvyhlidal 2014 SQL Tuning

Introduction to SQL tuning

● Using views

– Simplifies application design

– Is transparent to the developer

– Can cause suboptimal execution plans

Page 15: Pvyhlidal 2014 SQL Tuning

Introduction to SQL tuning

● SQL Execution Efficiency

– Good database connectivity

– Minimizing parsing

– Share cursors

– Using bind variables

Page 16: Pvyhlidal 2014 SQL Tuning

Introduction to SQL tuning

● Writing SQL to Share cursors

– Create generic code using the following:

● Stored procedures and packages

● Database triggers

● Any other library routines and procedures

– Write to format standards (improves readability)

● Case

● White space

● Comments

● Object references

● Bind variables

Page 17: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer

Page 18: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer

● Structured Query Language

Page 19: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer

● SQL Statement processing - overview

Page 20: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer

● SQL Statement processing – steps

1. Create a cursor

2. Parse the statement

3. Describe query result

4. Define query output

5. Bind variables

6. Parllelize the statement

7. Execute the statement

8. Fetch rows of query

9. Close the cursor

Page 21: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer

● 1. Create a Cursor

– A cursor is a handle or name for a private SQL area.

– It contains information for statement processing.

– It is created by a program interface call in expectation of a SQL statement.

– The cursor structure is independent of the SQL statement that it contains.

Page 22: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer

● 2. Parse the Statement

– Statement passed from the user process to the Oracle instance

– Parsed representation of SQL created and moved into the shared SQL area if there is no identical SQL in the shared SQL area

– Can be reused if identical SQL exists

Page 23: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer

● 3. Describe

– The describe step provides information about the select list items; it is relevant when entering dynamic queries through an OCI application

● 4. Define

– The define step defines location, size, and data type information required to store fetched values in variables

Page 24: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer

● 5. Bind

– Enables memory address to store data values

– Allows shared SQL even though bind values may change

● 6. Parallelize

– SELECT

– INSERT

– UPDATE

– MERGE

– DELETE

– CREATE

– ALTER

Page 25: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer

● 7. Execute

– Drives the SQL statement to produce the desired results

● 8. Fetch

– Into defined output variables

– Query results returned in table format

– Array fetch mechanism

● 9. Close

– Close the cursor

Page 26: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer SQL Statement Parsing : Overview

Page 27: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer Why do you need an Optimizer

Page 28: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer Why do you need an Optimizer

Page 29: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer Optimization during Hard Parse Operation

Page 30: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer Transformer: OR expansion example

Page 31: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer Transformer: Subquery unnesting example

Page 32: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer Transformer: View merging example

Page 33: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer Trasformer: Predicate pushing example

Page 34: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer Transformer: Transitivity example

Page 35: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer Cost-Based Optimizer

● Piece of code:

– Estimator

– Plan generator

● Estimator determines cost of optimization suggestions made by the plan generator:

– Cost: Optimizer’s best estimate of the number of standardized I/Os made to execute a particular statement optimization

● Plan generator:

– Tries out different statement optimization techniques

– Uses the estimator to cost each optimization suggestion

– Chooses the best optimization suggestion based on cost

– Generates an execution plan for best optimization

Page 36: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer Estimator: Selectivity

● Selectivity is the estimated proportion of a row set retrieved by a particular predicate or combination of predicates.

● It is expressed as a value between 0.0 and 1.0:

– High selectivity: Small proportion of rows

– Low selectivity: Big proportion of rows

● Selectivity computation:

– If no statistics: Use dynamic sampling

– If no histograms: Assume even distribution of rows

● Statistic information:

– DBA_TABLES and DBA_TAB_STATISTICS (NUM_ROWS)

– DBA_TAB_COL_STATISTICS (NUM_DISTINCT, DENSITY, HIGH/LOW_VALUE,...)

Page 37: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer Estimator: Cardinality

● Expected number of rows retrieved by a particular operation in the execution plan

● Vital figure to determine join, filters, and sort costs

● Simple example:

● SELECT days FROM courses WHERE dev_name = 'ANGEL';

– The number of distinct values in DEV_NAME is 203.

– The number of rows in COURSES (original cardinality) is 1018.

– Selectivity = 1/203 = 4.926*e-03

– Cardinality = (1/203)*1018 = 5.01 (rounded off to 6)

Page 38: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer Estimator: Cost

● Cost is the optimizer’s best estimate of the number of standardized I/Os it takes to execute a particular statement.

● Cost unit is a standardized single block random read:

– 1 cost unit = 1 SRds

● The cost formula combines three different costs units into standard cost units.

Page 39: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer Plan Generator

Page 40: Pvyhlidal 2014 SQL Tuning

Optimizer Operators

Page 41: Pvyhlidal 2014 SQL Tuning

Main Structures and Access Paths

Page 42: Pvyhlidal 2014 SQL Tuning

Full Table Scan

Page 43: Pvyhlidal 2014 SQL Tuning

Full Table Scans: Use Cases

Page 44: Pvyhlidal 2014 SQL Tuning

ROWID Scan

Page 45: Pvyhlidal 2014 SQL Tuning

Sample Table Scans

Page 46: Pvyhlidal 2014 SQL Tuning

Indexes: Overview

● Storage techniques:

– B*-tree indexes: The default and the most common

● Normal

● Function based: Precomputed value of a function or expression

● Index-organized table (IOT)

– Bitmap indexes

– Cluster indexes: Defined specifically for cluster

● Index attributes:

– Key compression

– Reverse key

– Ascending, descending

● Domain indexes: Specific to an application or cartridge

Page 47: Pvyhlidal 2014 SQL Tuning

Normal B*-tree Indexes

Page 48: Pvyhlidal 2014 SQL Tuning

Index Scans

Page 49: Pvyhlidal 2014 SQL Tuning

Index Unique Scan

Page 50: Pvyhlidal 2014 SQL Tuning

Index Range Scan

Page 51: Pvyhlidal 2014 SQL Tuning

Index Range Scan: Descending

Page 52: Pvyhlidal 2014 SQL Tuning

Descending Index Range Scan

Page 53: Pvyhlidal 2014 SQL Tuning

Index Range Scan: Function-Based

Page 54: Pvyhlidal 2014 SQL Tuning

Index Full Scan

Page 55: Pvyhlidal 2014 SQL Tuning

Index Fast Full Scan

Page 56: Pvyhlidal 2014 SQL Tuning

Index Skip Scan

Page 57: Pvyhlidal 2014 SQL Tuning

Index Skip Scan: Example

Page 58: Pvyhlidal 2014 SQL Tuning

Index Join Scan

Page 59: Pvyhlidal 2014 SQL Tuning

B*-tree Indexes and Nulls

Page 60: Pvyhlidal 2014 SQL Tuning

Using Indexes: Considering Nullable Columns

Page 61: Pvyhlidal 2014 SQL Tuning

Index-Organized Tables

Page 62: Pvyhlidal 2014 SQL Tuning

Index-Organized Table Scans

Page 63: Pvyhlidal 2014 SQL Tuning

Bitmap Indexes

Page 64: Pvyhlidal 2014 SQL Tuning

Bitmap Index Access: Examples

Page 65: Pvyhlidal 2014 SQL Tuning

Combining Bitmap Indexes: Examples

Page 66: Pvyhlidal 2014 SQL Tuning

Combining Bitmap Index Access Paths

Page 67: Pvyhlidal 2014 SQL Tuning

Bitmap Operations

Page 68: Pvyhlidal 2014 SQL Tuning

Bitmap Join Index

Page 69: Pvyhlidal 2014 SQL Tuning

Composite Indexes

Page 70: Pvyhlidal 2014 SQL Tuning

Invisible Index: Overview (11g new feature)

Page 71: Pvyhlidal 2014 SQL Tuning

Invisible Indexes: Examples

Page 72: Pvyhlidal 2014 SQL Tuning

Guidelines for Managing Indexes

Page 73: Pvyhlidal 2014 SQL Tuning

Investigating Index Usage

Page 74: Pvyhlidal 2014 SQL Tuning

Introduction to the Optimizer Optimizer Features and Oracle Database Releases

Page 75: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

Page 76: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

What Is an Execution Plan?

● The execution plan of a SQL statement is composed of small building blocks called row sources for serial execution plans.

● The combination of row sources for a statement is called the execution plan.

● By using parent-child relationships, the execution plan can be displayed in a tree-like structure (text or graphical).

Page 77: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

Where to find an Execution Plan?

● PLAN_TABLE (SQL Developer or SQL*Plus)

● V$SQL_PLAN (Library Cache)

● V$SQL_PLAN_MONITOR (11g)

● DBA_HIST_SQL_PLAN (AWR)

● STATS$SQL_PLAN (Statspack)

● SQL management base (SQL plan baselines)

● SQL tuning set

● Trace files generated by DBMS_MONITOR

● Event 10053 trace file

● Process state dump trace file since 10gR2

Page 78: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

Viewing Execution Plans

● The EXPLAIN PLAN command followed by:

– SELECT from PLAN_TABLE

– DBMS_XPLAN.DISPLAY()

● SQL*Plus Autotrace: SET AUTOTRACE ON

● DBMS_XPLAN.DISPLAY_CURSOR()

● DBMS_XPLAN.DISPLAY_AWR()

● DBMS_XPLAN.DISPLAY_SQLSET()

● DBMS_XPLAN.DISPLAY_SQL_PLAN_BASELINE()

Page 79: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

EXPLAIN PLAN command

● Generates an optimizer execution plan

● Stores the plan in PLAN_TABLE

● Does not execute the statement itself

Page 80: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

EXPLAIN PLAN command

Page 81: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

EXPLAIN PLAN command: Example

Page 82: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

PLAN TABLE

● Is automatically created to hold the EXPLAIN PLAN output.

● You can create your own using utlxplan.sql.

● Advantage: SQL is not executed

● Disadvantage: May not be the actual execution plan

● PLAN_TABLE is hierarchical.

● Hierarchy is established with the ID and PARENT_ID columns.

Page 83: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

PLAN TABLE : Typical

Page 84: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

PLAN TABLE : All

Page 85: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

PLAN TABLE : Advanced

Page 86: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

Using

- SQL Developer (freeware)

- PL/SQL Developer

- TOAD

- sqlplus

Page 87: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

AUTOTRACE

● Is a SQL*Plus and SQL Developer facility

● Was introduced with Oracle 7.3

● Needs a PLAN_TABLE

● Needs the PLUSTRACE role to retrieve statistics from some V$ views

● By default, produces the execution plan and statistics after running the query

● May not be the execution plan used by the optimizer when using bind peeking (recursive EXPLAIN PLAN)

Page 88: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

AUTOTRACE Syntax

Page 89: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

AUTOTRACE Examples

Page 90: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

AUTOTRACE Statistics

Page 91: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

Using the V$SQL_PLAN view

● V$SQL_PLAN provides a way of examining the execution plan for cursors that are still in the library cache.

● V$SQL_PLAN is very similar to PLAN_TABLE:

– PLAN_TABLE shows a theoretical plan that can be used if this statement were to be executed.

– V$SQL_PLAN contains the actual plan used.

● It contains the execution plan of every cursor in the library cache (including child).

● Link to V$SQL:

– ADDRESS, HASH_VALUE, and CHILD_NUMBER

Page 92: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

Links between important Dynamic Performace Views

Page 93: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

Querying V$SQL_PLAN

Page 94: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

Automatic Workload Repository (AWR)

● Collects, processes, and maintains performance statistics for problem-detection and self-tuning purposes

● Statistics include:

– Object statistics

– Some system and session statistics

– Active Session History (ASH) statistics

● Automatically generates snapshots of the performance data

● DBMS_WORKLOAD_REPOSITORY package

● Important AWR views:

– V$ACTIVE_SESSION_HISTORY, DBA_HIST_ACTIVE_SESS_HISTORY

– DBA_HIST_BASELINE DBA_HIST_DATABASE_INSTANCE

– DBA_HIST_SNAPSHOT, DBA_HIST_SQL_PLAN, DBA_HIST_WR_CONTROL

Page 95: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

Page 96: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

Example I.

Page 97: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

Example I.

Page 98: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

Example II.

Page 99: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

Example III.

Page 100: Pvyhlidal 2014 SQL Tuning

Interpreting Execution Plans

Reviewing the Execution Plan

● Drive from the table that has most selective filter.

● Look for the following:

– Driving table has the best filter

– Fewest number of rows are returned to the next step

– The join method is appropriate for the number of rows returned

– Views are correctly used

– Unintentional Cartesian products

– Tables accessed efficiently