+ All Categories
Home > Documents > 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database...

1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database...

Date post: 30-Mar-2015
Category:
Upload: bethany-colledge
View: 223 times
Download: 0 times
Share this document with a friend
Popular Tags:
38
1
Transcript
Page 1: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

1

Page 2: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

2

SQL Tuning for Smarties, Dummies and Everyone in Between

Jagan AthreyaDirector, Database Manageability, Oracle

Arup NandaSenior Director, Database Architecture, Starwood Hotels and Resorts

Novices

Page 3: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

3

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.

Page 4: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

4

Outline

• SQL Tuning Challenges

• SQL Tuning Solutions – New Feature Overview

• Problem Root Causes and their Solutions

• Preventing SQL Problems

• Q & A

Page 5: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

5

SQL Tuning ChallengesReal-world DBA and Development Teams

• DBA team– Mostly average, some superstars– Superstars take most of the burden – over-stretched

• Development staff– Mostly non-Oracle skills – Java, C++– Usually considers the DB as a “black box”– Writing efficient queries, troubleshooting performance issues

is delegated to DBAs

Page 6: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

6

SQL Tuning ChallengesProduction Performance

• Situation:– Query from hell pops up– Brings the database to its knees– DBA is blamed for the failure

• Response– DBA: “Developer should be taking care of this.”– Developer: “Why is the DBA not aware of this problem?”– Manager: “DBA will review all queries and approve them.”

• Challenge– What is the most efficient way to manage this process?

Page 7: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

7

SQL Tuning ChallengesChange Causing Problems

• Situation– New SQL statements added as part of application patch

deployment– Database upgrades– Database patching

• Response– Users: “How will the application perform after the changes?”– DBA: “How do I ensure that our SLA remains intact after the

changes are rolled out?”

• Challenge– How to reduce business risk while absorbing new

technologies?

Page 8: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

8

SQL Tuning ChallengesOptimizer Statistics Management

• Situation– Data in Production has evolved over time. Have the optimizer

statistics stayed current?

• ResponseDBA:– Will statistics refresh break something?– What will happen if we don’t collect?– How often should I collect the statistics ?– What happens when you collect a new set?

• Challenge– What is the recommended strategy for managing optimizer

statistics to ensure the best performance?

Page 9: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

9

SQL Tuning ChallengesBad Plans – Diagnosis and Resolution

• No time to find the root cause. How to prevent this from recurring?• Bind variables: How do you prevent bad plans based

on choice of bind variables?• How to diagnose a bad plan– 10053 trace, endless pouring over traces– Wrongly constructed predicates

• How to fix a bad plan – Hints? change of code?– Baselines vs. SQL Profiles– Pick out a single SQL or a bunch from the shared pool

Page 10: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

10

Outline

• SQL Tuning Challenges

• SQL Tuning Solutions – New Feature Overview

• Problem Root Causes and their Solutions

• Preventing SQL Problems

• Q & A

Page 11: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

11

• Automatically monitors long running SQL

• Enabled out-of-the-box with no performance overhead

• Monitors each SQL execution

• Exposes monitoring statistics– Global execution level– Plan operation level– Parallel Execution level

• Guides tuning efforts

Real-Time SQL MonitoringLooking Inside SQL Execution

Page 12: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

12

• PL/SQL monitoring including associated high load SQL monitored recursively• Exadata aware I/O performance monitoring and associated metric data• Capture rich metadata such as bind values, session details e.g. user,

program, client_id and error codes and error messages• Save as Active Report for rich interactive offline analysis

New capabilities in SQL MonitoringNew in Oracle Database 11g Release 2

Page 13: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

13

DEMO

Page 14: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

14

Application TuningAutomatic SQL Tuning

• Automatic SQL Tuning• Identifies high-load SQL from AWR• Tunes SQL using SQL Profiles• Implements greatly improved SQL plans (optional)

• Performance benefit of advice provided• SQL Profiling tunes execution plan without changing SQL text• Enables transparent tuning for packaged applications

ApplicationsApplications

SQL

High-Load

Packaged Apps +SQL Profile

Customizable Apps + SQL Advice

Customizable Apps + Indexes & MVs + Partitions

Well-Tuned SQL

Automatic Tuning Optimizer

Page 15: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

15

Automatic SQL TuningNew in Oracle Database 11g Release 2

• SQL Tuning Advisor • NEW: Identifies alternate execution plans using real-time and historical

performance data• NEW: Recommends parallel profile if it will improve SQL performance

significantly (2x or more)

SQL Profiling

Statistics Analysis

Access Path Analysis

SQL Restructure Analysis

Alternative Plan Analysis

Parallel Query Analysis

Automatic Tuning Optimizer

AdministratorAdministrator

SQL

ComprehensiveSQL Tuning

Recommendations

Gather Missing or Stale Statistics

Create a SQL Profile

Add Missing Access Structures

Modify SQL Constructs

Adopt AlternativeExecution Plan

Create Parallel SQL Profile

SQL

SQL Tuning Advisor

Page 16: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

16

SQL Tuning for DevelopersIntegration with Visual Studio

• Introduced in Oracle Developer Tools for Visual Studio Release 11.1.0.7.20• Oracle Performance Analyzer

– Tune running applications with the help of ADDM

• Query Window– Tune individual SQL statements with STA

• Server Explorer– Manage AWR snapshots and ADDM tasks

Page 17: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

17

Agenda

• SQL Tuning Challenges

• SQL Tuning Solutions – New Feature Overview

• Problem Root Causes and their Solutions

• Preventing SQL Problems

• Q & A

Page 18: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

18

What makes SQL go bad?Root Causes of Poor SQL Performance 1. Optimizer statistics issues

a. Stale/Missing statisticsb. Incomplete statisticsc. Improper optimizer configurationd. Upgraded database: new optimizere. Changing statisticsf. Rapidly changing data

2. Application Issuesa. Missing access structuresb. Poorly written SQL statements

3. Cursor sharing issues a. Bind-sensitive SQL with bind peekingb. Literal usage

4. Resource and contention issuesa. Hardware resource crunchb. Contention (row lock contention, block update contention)c. Data fragmentation

5. Parallelism issuesa. Not parallelized (no scaling to large data)b. Improperly parallelized (partially parallelized, skews)

Page 19: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

19

What makes SQL go bad?Root Causes of Poor SQL Performance

1. Optimizer statistics issuesa. Stale/Missing statistics

b. Incomplete statistics

c. Improper optimizer configuration

d. Upgraded database: new optimizer

e. Changing statistics

f. Rapidly changing data2. Application Issues3. Cursor sharing issues 4. Resource and contention issues5. Parallelism issues

Page 20: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

20

Oracle Optimizer Statistics

Inaccurate statistics Suboptimal Plans

CBO

Optimizer Statistics

• Table Statistics

• Column Statistics

• Index Statistics

• Partition Statistics

• System Statistics

Page 21: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

21

Oracle Optimizer StatisticsPreventing SQL Regressions

• Automatic Statistics Collection Job (stale or missing)• Out-of-the box, runs in maintenance window• Configuration can be changed (at table

level)• Gathers statistics on user and dictionary

objects

• Uses new collection algorithm with accuracy of compute and speed faster than sampling of 10%

• Incrementally maintains statistics for partitioned tables – very efficient • Set DBMS_STATS.SET_GLOBAL_PREFS

Nightly

Novice Mode

Page 22: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

22

Oracle Optimizer StatisticsPreventing SQL Regressions

• Extended Statistics• Extended Optimizer Statistics provides a mechanism to collect statistics on a group of related columns:

• Function-Based Statistics• Multi-Column Statistics

• Full integration into existing statistics framework• Automatically maintained with column statistics DBMS_STATS.CREATE_EXTENDED_STATS

• Pending Statistics• Allows validation of statistics before publishing• Disabled by default• To enable, set table/schema PUBLISH setting to FALSE DBMS_STATS.SET_TABLE_PREFS('SH','CUSTOMERS','PUBLISH','false')

• To use for validation

ALTER SESSION SET optimizer_pending_statistics = TRUE; • Publish after successful verification

Expert Mode

Page 23: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

23

1. Optimizer statistics issues

2. Application Issuesa. Missing access structures

b. Poorly written SQL statements3. Cursor sharing issues 4. Resource and contention issues5. Parallelism issues

What makes SQL go bad?Root Causes of Poor SQL Performance

Page 24: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

24

Identify performance problems using ADDMAutomatic Database Diagnostic Monitor

• Provides database and cluster-wide performance diagnostic

• Throughput centric - Focus on reducing time ‘DB time’

• Identifies top SQL:• Shows SQL impact• Frequency of

occurrence

• Pinpoints root cause:– SQL stmts waiting for

Row Lock waits– SQL stmts not shared

Novice Mode

Page 25: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

25

Identify High Load SQL Using Top Activity

Performance Page• Identify Top SQL by DB Time:

• CPU

• I/O

• Non-idle waits

• Different Levels of Analysis• Historical analysis

• AWR data

• Performance Page

• Real-time analysis• ASH data• More granular analysis• Enables identification of

transient problem SQL• Top Activity Page

• Tune using SQL Tuning Advisor

Top Activity

Novice Mode

Page 26: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

26

Advanced SQL TuningUniverse of Access Structures

• Indexes: B-tree indexes, B-tree cluster indexes, Hash cluster indexes, Global and local indexes, Reverse key indexes, Bitmap indexes, Function-based indexes, Domain indexes

• Materialized Views: Primary Key materialized views, Object materialized views ROWID materialized views

Complex materialized views

• Partitioned Tables: Range partitioning, Hash partitioning, List partitioning, Composite partitioning, Interval Partitioning, REF partitioning, Virtual Column Based partitioning

B-tree index

Novice+Mode

Page 27: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

27

SQL Access Advisor: Partition Advisor

Indexes

Materializedviews

Materializedviews logs

SQL Access Advisor

Representative Workload

Partitionedobjects

Automatic Tuning

Optimizer

Access Path Analysis

Novice+Mode

Page 28: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

28

SQL Access AdvisorAdvanced Options

• Workload filtering• Limited vs. advanced mode• Tablespaces for access structures• Hypothetical workload tuning• Factoring in the cost of creation• Space limitations for indexes and MVs

Expert Mode

Page 29: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

29

1. Optimizer statistics issues2. Application Issues

3. Cursor sharing issues a. Literal usageb. Bind-sensitive SQL with bind peeking

4. Resource and contention issues5. Parallelism issues

What makes SQL go bad?Root Causes of Poor SQL Performance

Page 30: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

30

What makes SQL go bad? a. Literal Usage Issue

SELECT * FROM jobs WHERE min_salary > 12000;

Library Cache

SELECT * FROM jobs WHERE min_salary > 15000;

SELECT * FROM jobs WHERE min_salary > 10000;

SELECT * FROM …

SELECT * FROM …

SELECT * FROM …

Sharing Cursors is good!

cursor_sharing ={exact, force, similar}

Expert Mode

Page 31: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

31

What makes SQL go bad? b. Bind Peeking Issue

Processed_Flag

YYYY

N

CBO10g

IRS

FTS

1

99

Full Table Scan

Index Range Scan

Two different optimal plans for different bind values

Problem: Binds will affect optimality in any subsequent uses of the stored plan

NMode

Page 32: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

32

Fixing problems with Adaptive Cursor Sharing Adaptive Cursor Sharing

SELECT * FROM emp WHERE wage := wage_value

Selectivity Ranges:

1

2

3

4

20 25

22 24

30 35

34 43

Same Plan

Different Plan

Same Plan,

Expand Interval

Expert Mode

Page 33: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

33

Agenda

• SQL Tuning Challenges

• SQL Tuning Solutions – New Feature Overview

• Problem Root Causes and their Solutions

• Preventing SQL Problems

• Q & A

Page 34: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

34

Preventing problems with SQL Plan Management

• Problem: changes in the environment cause plans to change

• Plan baseline is established

Statement log

Plan history

HJ

HJ

GB

Plan baselineGB

NL

NL

NL

NL

GB

Parse

• SQL statement is parsed again and a different plan is generated

• New plan is not executed but marked for verification

Page 35: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

35

Oracle Database 9 or 10g

Stored Outlines

OH Schema

HJ

GB

HJ

CREATE_STORED_OUTLINES=true

CREATE_STORED_OUTLINES=false

4. Upgrade to 11g

Oracle Database 11g

Oracle Database 11g

No planregressions

HJ

GB

HJ

5. Migrate Stored Outlines into SPM

PlanBaseline

Plan HistoryHJGB

HJ

1. Begin with

2. Run all SQL in the Application and auto

create a Stored Outline for each one

3. After Store Outlines are

captured

Stored Outlines

OH Schema

HJ

GB

HJ

SQL Plan ManagementMigration of Stored Outlines to Plan Baselines

Page 36: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

36

SQL Performance Analyzer (SPA) Validate statistics refresh with SPA

• Steps:1. Capture SQL workload in STS

using automatic cursor cache capture capability

2. Execute SPA pre-change trial3. Refresh statistics using

PENDING option4. Execute SPA post-change trial5. Run SPA report comparing SQL

execution statistics

• Before PUBLISHing stats:• Remediate individual few SQL

for plan regressions: SPM, STA• Revert to old statistics if too

many regressions observed

Validating upgrade with SPA

Analysis Report

Compare

SQL Performance

SQL plans + stats SQL plans + stats

Pre-change Trial Post-change Trial

SQL Workload

Page 37: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

37

Conclusion Identify, Resolve, Prevent

Identify

ADDM, Top Activity, SQL MonitoringResolve

Tuning Advisor,

Access Advisor,

Auto Stat Collection

Top Activity,

ADDM,

SQL Monitoring

PreventSPA

SPM

1. Production Performance2. Change Causing Problems3. Optimizer Statistics Management4. Bad plans – Diagnosis and Resolution

Page 38: 1. 2 SQL Tuning for Smarties, Dummies and Everyone in Between Jagan Athreya Director, Database Manageability, Oracle Arup Nanda Senior Director, Database.

38


Recommended