Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor...

23
Adaptive Cursor Sharing: An Introduction Copyright © 2010, Oracle and/or its affiliates. All rights reserved. 1-1 Harald van Breederode Oracle University 17-NOV-2010

Transcript of Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor...

Page 1: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

Adaptive Cursor Sharing:

An Introduction

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 1

Harald van Breederode

Oracle University

17-NOV-2010

Page 2: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

About Me

• Senior Principal DBA Trainer – Oracle University

• 25 years Unix Experience

• 12 years Oracle DBA Experience

• Oracle8i, 9i, 10g and 11g OCP

• Oracle10g and Oracle11g OCM

• DBA Certification Exam Team Reviewer

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 2

• DBA Certification Exam Team Reviewer

• DBA Curriculum Development Reviewer

• Blog: prutser.wordpress.com

• Visually Impaired (Legally Blind)

Page 3: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

Agenda

• Overview

• SQL statement processing

• Data Skew

• Using Bind variables

• Optimizer Bind Variable Peeking

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 3

• Optimizer Bind Variable Peeking

• Adaptive Cursor Sharing

• Demos

• Questions & Answers

Page 4: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

Overview

The goal of this presentation is to answer the following questions:

• What is data skew?

• What are Histograms?

• What is Optimizer Bind Variable Peeking?

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 4

• What is Adaptive Cursor Sharing?

Page 5: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

Basics of SQL statement processing

• Executing a SQL statement requires a cursor

• Lifetime of a cursor:

– Open

– Parse

– Execute

– Fetch

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 5

– Fetch

– Close

• The Optimizer generates and selects a execution plan

• Cursors in the library cache can be shared

– Good for library cache efficiency

– In general good for performance

• Implemented as parent and child cursors

• v$sqlarea and v$sql

Page 6: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

What is Selectivity?

• Used to calculate the cardinality of a result set

– Cardinality is the number of rows that needs processing

• Number between 0 and 1

• Basic algorithm: #Rows / #DistinctValues

• Visible in optimizer trace file (event 10053)

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 6

• Cardinality greatly influences execution plan selection

• Good selectivity means fewer rows => Index access path

• Bad selectivity means more rows => Full table scan

– Poorly estimated selectivity may result into sub-optimal

execution plans

Page 7: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

What is Data Skew?

• Non-uniform distribution of data

• Basically two flavors:

– Popular and non-popular non-unique values

— 10,10,10,10,20,10,10,20,10,10,30

– Non-consecutive unique values

1,2,3,4,5,1001,1002,1003,1004,1005

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 7

— 1,2,3,4,5,1001,1002,1003,1004,1005

• Skewed data may cause optimizer selectivity challenges

Page 8: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

Question

How can we help the optimizer to

better deal with skewed data?

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 8

Page 9: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

Answer

Create histograms on columns

containing skewed data

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 9

Page 10: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

What is a Histogram?

• Data dictionary objects to store skew metadata

• Introduced in Oracle8i

• Store metadata in 1..254 buckets

• Two different types:

– Width-balanced (a.k.a. frequency histogram)

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 10

– Height-balanced

• Type depends on number of buckets and number of DV

– #DV <= #Buckets => Width-balanced

– #DV > #Buckets => Height-balanced

• Enables the optimizer to better calculate selectivity

• Created by using METHOD_OPT clause of DBMS_STATS

• Viewed using DBA_TAB_HISTOGRAMS

Page 11: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

Using Bind Variables

• Goal is to improve sharing of cursors

• Reduce SQL memory usage

• Cure against SQL injection

• Cursor lifetime becomes:

– OPEN, PARSE, BIND, EXECUTE, FETCH, CLOSE

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 11

• May cause optimizer selectivity problems with skewed data

• Should be carefully implemented by SQL developers

• Can be enforced by the CURSOR_SHARING parameter

– EXACT – No literal replacement

– FORCE – (8i) Replace all literals by bind variables

– SIMILAR – (9i) Only replace literals when safe to do so

Page 12: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

Question

Is there a solution for the bind

variable optimizer selectivity

problem concerning skewed data?

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 12

Page 13: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

Answer

Use Optimizer Bind Variable Peeking

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 13

Page 14: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

Optimizer Bind Variable Peeking

• Goal is to avoid optimizer selectivity problems with Bind

variables and skewed data

• Optimizer peeks at value to be bound at hard parse time

• Introduced in Oracle9i

• Peeked value is visible in execution plan using

PEEKED_BINDS in format clause of DBMS_XPLAN

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 14

PEEKED_BINDS in format clause of DBMS_XPLAN

Page 15: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

Question

What happens if the peeked value

is not representative for future

executions?

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 15

Page 16: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

Answer

Again, we end up with optimizer

selectivity problems

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 16

Page 17: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

Adaptive Cursor Sharing

Goal is to strike a balance between never sharing cursors and

always sharing cursors.

• Introduced in Oracle11g

• Enabled by default

• Only applicable for SQL statements with bind variables

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 17

• If a Bind variable influences execution plan selection:

– Cursor is marked IS_BIND_SENSITIVE

– Optimizer monitors cursor executions

– Cardinality feedback is generated

• Optimizer learns from wrongly calculated selectivity

• A new child cursor is created if a better plan is possible

• The new cursor is marked IS_BIND_AWARE

Page 18: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

Adaptive Cursor Sharing process flow

• Application executes SQL statement with bind variables

• Optimizer performs the following action at hard parse time

– Peek at bind variables

– Generate and select optimal execution plan

– Cursor is marked IS_BIND_SENSITIVE (we assume!)

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 18

• During execution, cursor is monitored

• Cardinality feedback is generated upon completion

• At next execution cardinality feedback is used possibly

resulting in a new child and if so, then cursor is marked

IS_BIND_AWARE

• If a previous cursor exists for the same selectivity it is

marked as not sharable which leads to cursor flush

Page 19: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

Adaptive Cursor Sharing Views

Views containing Adaptive Cursor Sharing information:

• V$SQL

– Columns: IS_BIND_SENSITIVE, IS_BIND_AWARE

• V$SQL_CS_STATISTICS

– Adaptive Cursor Sharing execution statistics

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 19

– Adaptive Cursor Sharing execution statistics

• V$SQL_CS_SELECTIVITY

– Selectivity ranges for Adaptive Cursors

• V$SQL_CS_HISTOGRAM

– Adaptive Cursor Sharing monitoring information

Page 20: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

Interaction between Adaptive Cursor Sharing and

the CURSOR_SHARING parameter

• Well-written applications should use:

– CURSOR_SHARING=EXACT

• Misbehaving Oracle8i applications should use:

– CURSOR_SHARING=FORCE (or better, upgrade to 11g!)

• Misbehaving Oracle9i/10g applications should use:

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 20

• Misbehaving Oracle9i/10g applications should use:

– CURSOR_SHARING=SIMILAR

• Misbehaving Oracle11g applications should use:

– CURSOR_SHARING=FORCE

– Adaptive Cursor Sharing will compensate over-sharing of

cursors caused by CURSOR_SHARING=FORCE

Page 21: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

Live Demo!

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 21

Page 22: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

Q U E S T I O N S&

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 22

A N S W E R S&

Page 23: Adaptive Cursor Sharing: An Introduction€¢ Cure against SQL injection ... – Adaptive Cursor Sharing monitoring information. ... AdaptiveCursorSharing.ppt [Compatibility Mode]

And Finally

Thank you for your kind attention!

For a copy of my demonstration scripts email me at:

Copyright © 2010, Oracle and/or its affiliates. All rights reserved.1 - 23

For a copy of my demonstration scripts email me at:

[email protected]

Remember: prutser.wordpress.com