76925976 SQL Tuning Oracle 10g

64

description

m

Transcript of 76925976 SQL Tuning Oracle 10g

Page 1: 76925976 SQL Tuning Oracle 10g
Page 2: 76925976 SQL Tuning Oracle 10g

SQL Tuning in Oracle 10g:The Do’s and Don’ts

Mughees A. MinhasGroup Product ManagerDatabase Manageability

Oracle Corp.

Page 3: 76925976 SQL Tuning Oracle 10g

OutlineIntroductionManual Tuning Challenges SQL Tuning & Access Advisor Overview SQL Tuning Advisor

– Automatic SQL Tuning– Usage scenarios– User interface

SQL Access Advisor– Usage scenarios– User interface

Tips, Do’s, Don’tsConclusion

Page 4: 76925976 SQL Tuning Oracle 10g

Introduction

Automatic SQL Tuning solution consists of– SQL Tuning Advisor– SQL Access Advisor

Provides comprehensive, automatic, and cost-effective solution for application tuningReduces SQL tuning time by up to 80%Reduces management cost

Page 5: 76925976 SQL Tuning Oracle 10g

Manual Tuning Challenges

Requires expertise in several domains– SQL optimization: adjust the execution plan– Access design: provide fast data access– SQL design: use appropriate SQL constructs

Time consuming– Each SQL statement is unique– Potentially large number of statements to tune

Never ending task– SQL workload always evolving– Plan regressions

Page 6: 76925976 SQL Tuning Oracle 10g

Manual Tuning ExamplePackaged App Tuning Scenario

Manual Tuning1. Get explain plan2. Examine query objects and their sizes3. Review and compare explain plan statistics with execution

statistics (stored in V$SQL view)4. Identify the problem, e.g., “first rows” issue because only recent

data is ever displayed despite large history being queried5. Contact application vendor6. Produce test case for vendor7. Get a patch with appropriate code modifications from the

vendor8. Install the patch in next maintenance cycle

Page 7: 76925976 SQL Tuning Oracle 10g

SQL Tuning & Access Advisor Overview

How do I tune my SQL

workload?

SQL Tuning & Access Advisors

Solution

Component of CBO

Provides implementation

script

No expertrequiredDBA

SQLWorkload

Page 8: 76925976 SQL Tuning Oracle 10g

SQL Tuning Advisor

Page 9: 76925976 SQL Tuning Oracle 10g

Oracle 10g Automates the SQL Tuning Process

I can do it for you !

SQL Tuning Advisor

DBAHigh-Load

SQL

ADDM

SQLWorkload

Page 10: 76925976 SQL Tuning Oracle 10g

Automatic SQL Tuning Overview

Add Missing Indexes

Modify SQL Constructs

Create a SQL Profile

Automatic Tuning Optimizer

SQL Structure Analysis

Access Path Analysis

SQL Profiling

Statistics Analysis

Gather Missing or Stale Statistics

DBA

SQL TuningRecommendations

SQL Tuning Advisor

Page 11: 76925976 SQL Tuning Oracle 10g

Automatic Tuning Optimizer (ATO)

It is the query optimizer running in tuning mode– Uses same plan generation process but performs

additional steps that require significantly more timeIt performs verification steps

– To validate statistics and its own estimatesUses dynamic sampling and partial executions to validate

It performs exploratory steps– To investigate the use of new indexes that could

provide significant speed-up– To analyze SQL constructs that led to expensive

plan operators

Page 12: 76925976 SQL Tuning Oracle 10g

Statistics Analysis

Add Missing Indexes

Modify SQL Constructs

Create a SQL Profile

Automatic Tuning Optimizer

SQL Structure Analysis

Access Path Analysis

SQL Profiling

Statistics Analysis

Gather Missing or Stale Statistics

DBA

SQL TuningRecommendations

SQL Tuning Advisor

Page 13: 76925976 SQL Tuning Oracle 10g

Statistics AnalysisMotivation

– Statistics are key input to the query optimizerTheir availability and accuracy is very important

In Oracle10g, the Automatic Statistics Collection maintains statistics up to date…

– But it may not be enabled or properly configured!

The ATO verifies statistics that it needs/uses– Generates auxiliary information to compensate for

missing or stale statistics– Generates recommendations to gather statistics

where appropriate

Page 14: 76925976 SQL Tuning Oracle 10g

SQL Profiling

Add Missing Indexes

Modify SQL Constructs

Create a SQL Profile

Automatic Tuning Optimizer

SQL Structure Analysis

Access Path Analysis

SQL Profiling

Statistics Analysis

Gather Missing or Stale Statistics

DBA

SQL TuningRecommendations

SQL Tuning Advisor

Page 15: 76925976 SQL Tuning Oracle 10g

SQL ProfilingMotivation

– Empower query optimizer to find better plan by gathering additional information on query behavior

The query optimizer has time constraints– Makes compromises while finding right plan

The ATO is allowed a lot more time– Uses the time to gather customized information

about the SQL statement, known as SQL Profile– Builds a SQL Profile and recommends it– Once implemented, SQL Profile is used by the

query optimizer to generate a well-tuned plan

Page 16: 76925976 SQL Tuning Oracle 10g

SQL ProfileContains auxiliary information collected by the ATO for a SQL statement

– Customized optimizer settingsBased on past execution history (e.g., first_rows vs. all_rows)

– Compensation for missing or stale statistics– Compensation for errors in optimizer estimates

Estimation errors occur due to data skews and correlations, complex filters and joins

Doesn’t require any change to the SQL text– Ideal for Packaged Apps

Persistence: Works across shutdowns & upgradesTransportable across databases (10.2)

Page 17: 76925976 SQL Tuning Oracle 10g

SQL Profiling Flow

Optimizer(Tuning Mode)

createsubmit

SQL Profiling

Optimizer(Normal Mode)

outputsubmit

SQL Profile

SQL TuningAdvisor

DatabaseUsers

Well-TunedPlan

After …

use

Page 18: 76925976 SQL Tuning Oracle 10g

Access Path Analysis

Add Missing Indexes

Modify SQL Constructs

Create a SQL Profile

Automatic Tuning Optimizer

SQL Structure Analysis

Access Path Analysis

SQL Profiling

Statistics Analysis

Gather Missing or Stale Statistics

DBA

SQL TuningRecommendations

SQL Tuning Advisor

Page 19: 76925976 SQL Tuning Oracle 10g

Access Path AnalysisMotivation

– Adding an index may significantly improve the performance of a SQL statement

Problem: A critical access path is missing– Index not created or mistakenly dropped

ATO explores the use of new indexes– Recommends index if major performance boost

provided– Also recommends running SQL Access Advisor to get

comprehensive index analysis for entire workloadSQL Access Advisor also uses this analysis mode

Page 20: 76925976 SQL Tuning Oracle 10g

SQL Structure Analysis

Add Missing Indexes

Modify SQL Constructs

Create a SQL Profile

Automatic Tuning Optimizer

SQL Structure Analysis

Access Path Analysis

SQL Profiling

Statistics Analysis

Gather Missing or Stale Statistics

DBA

SQL TuningRecommendations

SQL Tuning Advisor

Page 21: 76925976 SQL Tuning Oracle 10g

SQL Structure AnalysisMotivation

– Help application developers identify poorly written SQL statements

– Suggest restructuring of SQL for efficiencyProblem categories

– Semantic changes of SQL operators (e.g., use UNION ALL instead of UNION)

Subject to user acceptance of new result

– Syntactic changes to predicates on indexed columns (e.g., remove type mismatch in column = :bind)

– SQL design issues (e.g., add missing join predicate to eliminate a large Cartesian join)

Page 22: 76925976 SQL Tuning Oracle 10g

SQL Tuning Usage Scenarios

SQL Tuning Advisor

ADDM High-load SQL

Cursor Cache

AWR

SQL Tuning Set(STS)

User-defined

Filter / Rank

SQL Sources Manual Selection

Automatic Selection

AWR

Page 23: 76925976 SQL Tuning Oracle 10g

SQL Tuning Set (STS)Motivation

– Enable user to tune custom set of SQL statementsNew object in Oracle10g for capturing and managing SQL workloadStores SQL statements along with:

– Execution context: parsing user, bind values, etc.– Execution statistics: buffer gets, CPU time, elapse

time, number of executions, etc.Transportable across databases (10.2)Created from any SQL source

– AWR, cursor cache, user-defined workload, STS

Page 24: 76925976 SQL Tuning Oracle 10g

SQL Tuning Set Benefits

Allows selective, on-demand, custom SQL workload tuningSimplifies tuning of large number of SQL statementsIs persistentFacilitates workload capture/managementProvides a common infrastructure for dealing with SQL workloads

– Can be used as a source for different tuning tasks

Page 25: 76925976 SQL Tuning Oracle 10g

SQL Tuning Advisor User Interface

GUI: Enterprise ManagerLaunch SQL Tuning Advisor from a SQL Source page

– ADDM Finding page, or– Top SQL page, or– SQL Tuning Set (STS) page

View/Implement SQL Tuning Recommendations

Command line: DBMS_SQLTUNE package

Page 26: 76925976 SQL Tuning Oracle 10g

SQL Source: ADDM Finding

Page 27: 76925976 SQL Tuning Oracle 10g

SQL Source: Top SQL

Page 28: 76925976 SQL Tuning Oracle 10g

SQL Source: SQL Tuning Set

Page 29: 76925976 SQL Tuning Oracle 10g

Enterprise Manager Interface

Launch SQL Tuning Advisor from a SQL Source page

– ADDM Finding page, or– Top SQL page, or– SQL Tuning Set (STS) page

View/Implement SQL Tuning Recommendations

Page 30: 76925976 SQL Tuning Oracle 10g

SQL Tuning Recommendations — Overview

Page 31: 76925976 SQL Tuning Oracle 10g

DBMS_SQLTUNE PL/SQL Package

Contains API for SQL Tuning

– Create Tuning Task– Execute Tuning Task– Display Advisor

Recommendations– Drop Tuning Task

– Create STS– Populate STS– Query STS Contents– Drop STS

– Accept SQL Profile– Drop SQL Profile– Alter SQL Profile

Attribute

Tuning Task Management STS Management

SQL Profile Management

Page 32: 76925976 SQL Tuning Oracle 10g

Automatic vs. Manual SQL Tuning

Manual Tuning1. Get explain plan2. Examine query objects and their sizes3. Review and compare explain plan statistics with

execution statistics (stored in V$SQL view)4. Identify the problem, e.g., “first rows” issue because

only recent data is ever displayed despite large history being queried

5. Contact application vendor6. Produce test case for vendor7. Get a patch with appropriate code modifications from

the vendor8. Install the patch in next maintenance cycle

Automatic Tuning1. Run SQL Tuning

Advisor2. Implement

recommendations.

Page 33: 76925976 SQL Tuning Oracle 10g

SQL Access Advisor

Page 34: 76925976 SQL Tuning Oracle 10g

SQL Access Advisor

Workload

Indexes Materialized ViewsMaterialized View Logs

SQL Access Advisor

Page 35: 76925976 SQL Tuning Oracle 10g

SQL Access Advisor FeaturesDe-mystifies access structure design for optimal application performanceRecommends indexes, materialized views, and materialized view logs to create and/or drop for faster performanceAnalyzes entire workload and not just independent SQL statementsTakes into account impact of new access structures on DML operationsConsiders storage, creation and maintenance costs

Page 36: 76925976 SQL Tuning Oracle 10g

SQL Access Advisor FeaturesSimultaneously considers

– index solutions– materialized view solutions– combinations of both

Optimizes materialized views for– maximum query rewrite usage– fast refresh

Recommends materialized view logs for fast refreshCombines similar indexes into single indexImpact of new access structures on DML consideredConsiders storage, creation & maintenance costs

Page 37: 76925976 SQL Tuning Oracle 10g

Usage Scenarios

WorkloadUser Defined

Hypothetical

Cursor Cache

Filter Options

STS

SQL Access Advisor

Page 38: 76925976 SQL Tuning Oracle 10g

Filter Options

Don’t have to use the entire workloadFilter by

– Application or module name– Number of SQL statements– Queries during a specified time window– Username– Tables

must be in this listnot in this list

Page 39: 76925976 SQL Tuning Oracle 10g

SQL Access Advisor User Interface

GUI: Enterprise Manager– Launch SQL Access Advisor from Advisor Central– Select workload source– Set options

WorkloadRecommendationAdvanced

– Schedule job– Review job and submit– Monitor job– View Recommendations– Implement Recommendations

Command line: DBMS_ADVISOR package

Page 40: 76925976 SQL Tuning Oracle 10g

Launch SQL Access Advisor

Page 41: 76925976 SQL Tuning Oracle 10g

Select Workload Source

Page 42: 76925976 SQL Tuning Oracle 10g

Set Workload Options

Page 43: 76925976 SQL Tuning Oracle 10g

Set Recommendation Options

Page 44: 76925976 SQL Tuning Oracle 10g

Advanced Options

Page 45: 76925976 SQL Tuning Oracle 10g

Advanced Tuning Options

SQL statements will be tuned according to the resources they use

Page 46: 76925976 SQL Tuning Oracle 10g

Schedule Job

Page 47: 76925976 SQL Tuning Oracle 10g

Review & Submit

Page 48: 76925976 SQL Tuning Oracle 10g

Monitor the Job

Page 49: 76925976 SQL Tuning Oracle 10g

View Recommendations

Page 50: 76925976 SQL Tuning Oracle 10g

Implement Recommendations

Page 51: 76925976 SQL Tuning Oracle 10g

Tips, Do’s, Dont’s

Page 52: 76925976 SQL Tuning Oracle 10g

Capturing SQL Workload

Use cursor cache capture capability of STS– Run workload– Capture workload in STS simultaneously

DBMS_SQLTUNE.CAPTURE_CURSOR_CACHE_SQLSET( sqlset_name => ‘MY_STS’, time_limit => 3600, repeat_interval => 60,sqlset_owner => own);

Page 53: 76925976 SQL Tuning Oracle 10g

SQL Profile Cursor Sharing

Cursor sharing can be enforced at database or SQL level

– Database level: CURSOR_SHARING=FORCE– SQL level: Set SQL Profile attribute

FORCE_MATCH=TRUE

DBMS_SQLTUNE.ACCEPT_SQL_PROFILE (

task_name => 'my_sql_tuning_task',name => 'my_sql_profile',)force_match => TRUE);

Page 54: 76925976 SQL Tuning Oracle 10g

Tuning ConsiderationsResource consumption

– Running SQL advisors consumes CPU, I/O, memory and can affect system performance

Potential negative impact of recommendation– Implementing recommendations may impact system

negatively, e.g., optimizer stats refresh

• Before initiating tuning, answer the following questions• How much resources will be consumed by tuning activity?• Can the system spare resources needed for tuning?• How can the production system be shielded from possible

negative impact of tuning actions?

Page 55: 76925976 SQL Tuning Oracle 10g

Resource Consumption of SQL Advisors

SQL Tuning Advisor– Limited mode: Resource consumption minimal

Stats, index and SQL restructure analysis is cheapAverage is less than 1 second per SQL statement

– Comprehensive mode: Resource consumption may be significant

SQL Profiling can potentially consume non-trivial resourcesRoughly comparable to amount of resources/time consumed when executing SQL statement(s)

• SQL Access Advisor• Resource consumption depends on size of SQL workload• For small number of SQL, resource consumption not very high

Page 56: 76925976 SQL Tuning Oracle 10g

Tuning Options

Options– Direct tuning of live system– Remote tuning

Live system tuning– Run SQL Tuning Advisor in Comprehensive mode – Run SQL Tuning Advisor in Limited mode only if …

System does not have spare resources to tune SQL– Run SQL Access Advisor for few SQL at a time

Perform remote tuning, if …– Cumulative resources/time consumed by all SQL

statements being tuned significant– System cannot spare resources

Page 57: 76925976 SQL Tuning Oracle 10g

Live System Tuning Tips1. Ensure tables referenced in SQL have representative

optimizer stats2. Run SQL Access Advisor

– For individual SQL, set “Recommendation Type” to indexes– MV not suitable for tuning individual statements

3. Run SQL Tuning Advisor– Test profile before making it PUBLIC

DBMS_SQLTUNE.ACCEPT_SQL_PROFILE (task_name => ‘<tuning task name>’,category => ‘MY_CATEGORY’);

ALTER SESSION SET SQLTUNE_CATEGORY=‘MY_CATEGORY’ ;

– Once satisfied with results set category to DEFAULTDBMS_SQLTUNE.ALTER_SQL_PROFILE()

Page 58: 76925976 SQL Tuning Oracle 10g

Remote Tuning Tips

1. Move SQL to Test

Production System Test System

2. Tune SQL3. Apply recomm. on production

• Performed to shield production system from performance impact of running SQL advisors

Page 59: 76925976 SQL Tuning Oracle 10g

Remote Tuning Tips

Move SQL to test– Use Transportable STS

DBMS_SQLTUNE.XXX_STGTAB_SQLSET

Apply recommendation on production– Use Transportable SQL Profiles

DBMS_SQLTUNE.XXX_STGTAB_SQLPROF

Page 60: 76925976 SQL Tuning Oracle 10g

Automatic SQL Tuning Summary

SQL Access AdvisorAccess Path: Materialized View LogsSQL Access AdvisorAccess Path: Materialized Views

SQL Tuning/Access Advisor

Access Path: IndexesSQL Tuning AdvisorSQL Structure SQL Tuning AdvisorSQL ProfileSQL Tuning AdvisorStatistics

Performed ByAnalysis Types

SQL Tuning Advisor and SQL Access Advisortogether completely automate SQL tuning

Page 61: 76925976 SQL Tuning Oracle 10g

Conclusion

SQL Advisors help address critical SQL tuning challengesProvides targeted and automated tuningMakes possible comprehensive tuning of packaged applicationsEliminates need for highly skilled performance experts

Page 62: 76925976 SQL Tuning Oracle 10g

Thank You!

Page 63: 76925976 SQL Tuning Oracle 10g

Q U E S T I O N SQ U E S T I O N SA N S W E R SA N S W E R S

Page 64: 76925976 SQL Tuning Oracle 10g