51
Using Adaptive Cursor Sharing (ACS) to produce multiple Optimal Plans Carlos Sierra

Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

  • Upload
    enkitec

  • View
    194

  • Download
    1

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Using Adaptive Cursor Sharing (ACS) to produce multiple Optimal Plans

Carlos Sierra

Page 2: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

§ SQL Tuner handyman: developer, advisor, trainer, support §  IT: Enkitec (<0.2), Oracle(17), UNISYS(12), Ford(3), others(3) § Florida(17), Venezuela(3), Puerto Rico(6), Michigan(1), Mexico(x) § Tools: SQLTXPLAIN(SQLT), SQLHC, TRCANLZR(TRCA), others § Motto: Life’s Good!

Carlos Sierra Senior Technical Consultant at Enkitec

2

Page 3: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Adaptive Cursor Sharing (ACS)

§  Motivation

§  Mechanics

§  Test Case

§  Demo

§  Remarks

3

Page 4: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

ACS Motivation

Page 5: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

SQL Processing

§ Hard parse side effects –  CPU consumption –  Latch contention

§ Excessive hard parsing –  Affects concurrency –  Restricts scalability

§ Mitigating hard parsing –  Cursor sharing

Hard parsing is expensive!

5

Page 6: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Implementing Cursor Sharing Replacing literals with bind variables

6

Page 7: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Before Bind Peeking

§ Predicate –  WHERE channel_id = :b1

§ Unknowns –  Is :b1 between low and high values of channel_id? –  Is :b1 a popular value of channel_id? –  Are there any rows with value :b1 for channel_id?

§ Penalty –  Possible suboptimal plans

Before 9i CBO was blind to values passed

7

Page 8: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

With Bind Peeking

§ Predicate –  WHERE channel_id = :b1

§ Plan is determined by peeked values –  EXEC :b1 := 9;

§ Optimal plan for 1st execution –  CBO can use low/high and histograms on channel_id

§ Penalty –  Possible suboptimal plans for subsequent executions on skewed data

9i offers a partial solution

8

Page 9: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Real-life Problem with Bind Peeking People Soft Payroll Application

9

§ WHERE employee BETWEEN :b1 AND :b2 § Payroll for one employee

–  :b1 = 123456 –  :b2 = 123456

§ Payroll for one company –  :b1 = 000001 –  :b2 = 999999

§ Doing payroll for an employee first then entire company

Page 10: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

With Adaptive Cursor Sharing

§ Some queries are ACS candidates § Sophisticated non-persistent mechanism § Selectivity of predicates determine plan § Multiple optimal plans for a query!

–  If ACS is successfully applied § Penalty

–  Marginal increase in CPU and memory overhead

11g improves cursor sharing

10

Page 11: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

ACS Mechanics

Page 12: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

ACS high-level Overview High level overview

§  If SQL with binds meets some requirements –  Flag cursor as bind sensitive –  Start monitoring data volume manipulated by cursor

§  If bind sensitive and data volume manipulated by cursor varies significantly

–  Flag cursor as bind aware –  Start generating multiple optimal plans for this query on next hard parse

§  If bind aware then use selectivity of predicates to decide on plan

12

Page 13: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Bind Sensitive

§ SQL has explicit binds –  Or literals and cursor_sharing is “force”

§ Predicate: column + operand + bind_variable –  Equality operand “=“ and histogram on column

§  Ex: channel_id = :b1

–  Non-equality operand (range) regardless of histogram on column §  “>”, “>=“, “<“, ‘<=“, BETWEEN, LIKE

Minimum requirements

13

Page 14: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Bind Aware

§ Significant changes in data volume manipulated by cursor –  A few rows versus a few thousands of rows –  A few thousands of rows versus a few millions of rows

§ Specifying /*+ BIND_AWARE */ CBO Hint –  Bypasses the monitoring phase on data volume

How to become bind aware?

14

Page 15: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Plan Selection

§ Evaluate selectivity of predicates at soft parse § Compare to a non-persistent selectivity profile §  If within ranges of a known profile then select associated plan § Else hard parse

–  Compute and execute newly generated plan –  Create selectivity profile for new plan or update profile of existing plan

§  If ranges on selectivity profiles overlap then merge profiles

Based on selectivity profile of predicates

15

Page 16: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

V$ dynamic views for ACS

§ V$SQL –  Shareable, bind sensitive and bind aware flags

§ V$SQL_CS_STATISTICS –  Data volume manipulated (rows processed)

§ V$SQL_CS_HISTOGRAM –  Record keeping of data volume per execution (small, medium, large)

§ V$SQL_CS_SELECTIVITY –  Predicates selectivity profiles

ACS non-persistent performance views

16

Page 17: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

ACS Test Case

Page 18: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Our Query with Literals Guesstimate execution plan then verify it with demo 0

18

Page 19: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Possible Access Paths?

19

Page 20: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Optimal Execution Plan

20

Page 21: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Our Query with Bind Variables How many optimal execution plans can you foresee?

21

Page 22: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Multiple Optimal Plans for one Query Guesstimate optimal plan (access paths) for each query

Query :b1 :b2 AP1 AP2 q1 9 33 N1 N2 q2 5 32 q3 2 999 q4 9 999 q5 2 33

22

Page 23: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Multiple Optimal Plans for one Query Guesstimate optimal plan (access paths) for each query

Query :b1 :b2 AP1 AP2 q1 9 33 N1 N2 q2 5 32 N1 N2 q3 2 999 FTS FTS q4 9 999 N1 FTS q5 2 33 FTS N2

23

Page 24: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Multiple Optimal Plans for one Query Execute demos 1-5 and verify access paths

Query :b1 :b2 AP1 AP2 q1 9 33 N1 N2 q2 5 32 N1 N2 q3 2 999 FTS FTS q4 9 999 N1 FTS q5 2 33 FTS N2

24

Page 25: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Bind Sensitive: Rows Processed

§ Data volume manipulated –  Fuzzy representation –  S: few rows –  M: thousands or rows –  L: millions of rows

§  v$sql_cs_histogram –  Bucket(0): S –  Bucket(1): M –  Bucket(2): L

Monitor v$sql_cs_statistics.rows_processed

Query :b1 :b2 Optimal Rows

Processed q1 9 33 N1/N2 37,382 q2 5 32 N1/N2 2 q3 2 999 FTS/FTS 8,021,324 q4 9 999 N1/FTS 6,233,815 q5 2 33 FTS/N2 1,825,131

25

Page 26: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

ACS Demo

Page 27: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Demo 6: When Cursor becomes Bind Aware?

Query :b1 :b2 Optimal Rows

Processed Bucket Aware Child Actual q1 9 33 N1/N2 37,382 q2 5 32 N1/N2 2 q3 2 999 FTS/FTS 8,021,324 q4 9 999 N1/FTS 6,233,815 q5 2 33 FTS/N2 1,825,131

Obtain rows processed from demo 1-5 then guesstimate aware flag

27

Page 28: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Demo 6: When Cursor becomes Bind Aware?

Query :b1 :b2 Optimal Rows

Processed Bucket Aware Child Actual q1 9 33 N1/N2 37,382 1 q2 5 32 N1/N2 2 0 q3 2 999 FTS/FTS 8,021,324 2 q4 9 999 N1/FTS 6,233,815 2 q5 2 33 FTS/N2 1,825,131 2

Obtain rows processed from demo 1-5 then guesstimate aware flag

28

Page 29: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Demo 6: When Cursor becomes Bind Aware?

Query :b1 :b2 Optimal Rows

Processed Bucket Aware Child Actual q1 9 33 N1/N2 37,382 1 N q2 5 32 N1/N2 2 0 N q3 2 999 FTS/FTS 8,021,324 2 Y q4 9 999 N1/FTS 6,233,815 2 Y q5 2 33 FTS/N2 1,825,131 2 Y

Obtain rows processed from demo 1-5 then guesstimate aware flag

29

Page 30: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Demo 6: When Cursor becomes Bind Aware?

Query :b1 :b2 Optimal Rows

Processed Bucket Aware Child Actual q1 9 33 N1/N2 37,382 1 N 0 N1/N2 q2 5 32 N1/N2 2 0 N 0 N1/N2 q3 2 999 FTS/FTS 8,021,324 2 Y 1 FTS/FTS q4 9 999 N1/FTS 6,233,815 2 Y 2 N1/FTS q5 2 33 FTS/N2 1,825,131 2 Y 3 FTS/N2

Obtain rows processed from demo 1-5 then guesstimate aware flag

30

Page 31: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Demo 6: When Cursor becomes Bind Aware?

Query :b1 :b2 Optimal Rows

Processed Bucket Aware Child Actual q1 9 33 N1/N2 37,382 1 N 0 N1/N2 q2 5 32 N1/N2 2 0 N 0 N1/N2 q3 2 999 FTS/FTS 8,021,324 2 Y 1 FTS/FTS q4 9 999 N1/FTS 6,233,815 2 Y 2 N1/FTS q5 2 33 FTS/N2 1,825,131 2 Y 3 FTS/N2

Obtain rows processed from demo 1-5 then guesstimate aware flag

31

Page 32: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Demo 7: When Cursor becomes Bind Aware?

Query :b1 :b2 Optimal Rows

Processed Bucket Aware Child Actual q5 2 33 FTS/N2 1,825,131 q4 9 999 N1/FTS 6,233,815 q3 2 999 FTS/FTS 8,021,324 q2 5 32 N1/N2 2 q1 9 33 N1/N2 37,382

Compute bucket and guesstimate aware flag and actual plan

32

Page 33: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Demo 7: When Cursor becomes Bind Aware?

Query :b1 :b2 Optimal Rows

Processed Bucket Aware Child Actual q5 2 33 FTS/N2 1,825,131 2 q4 9 999 N1/FTS 6,233,815 2 q3 2 999 FTS/FTS 8,021,324 2 q2 5 32 N1/N2 2 0 q1 9 33 N1/N2 37,382 1

Compute bucket and guesstimate aware flag and actual plan

33

Page 34: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Demo 7: When Cursor becomes Bind Aware?

Query :b1 :b2 Optimal Rows

Processed Bucket Aware Child Actual q5 2 33 FTS/N2 1,825,131 2 N q4 9 999 N1/FTS 6,233,815 2 N q3 2 999 FTS/FTS 8,021,324 2 N q2 5 32 N1/N2 2 0 N q1 9 33 N1/N2 37,382 1 Y

Compute bucket and guesstimate aware flag and actual plan

34

Page 35: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Demo 7: When Cursor becomes Bind Aware?

Query :b1 :b2 Optimal Rows

Processed Bucket Aware Child Actual q5 2 33 FTS/N2 1,825,131 2 N 0 FTS/N2 q4 9 999 N1/FTS 6,233,815 2 N 0 FTS/N2 q3 2 999 FTS/FTS 8,021,324 2 N 0 FTS/N2 q2 5 32 N1/N2 2 0 N 0 FTS/N2 q1 9 33 N1/N2 37,382 1 Y 1 N1/N2

Compute bucket and guesstimate aware flag and actual plan

35

Page 36: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Demo 7: When Cursor becomes Bind Aware?

Query :b1 :b2 Optimal Rows

Processed Bucket Aware Child Actual q5 2 33 FTS/N2 1,825,131 2 N 0 FTS/N2 q4 9 999 N1/FTS 6,233,815 2 N 0 FTS/N2 q3 2 999 FTS/FTS 8,021,324 2 N 0 FTS/N2 q2 5 32 N1/N2 2 0 N 0 FTS/N2 q1 9 33 N1/N2 37,382 1 Y 1 N1/N2

Compute bucket and guesstimate aware flag and actual plan

36

Page 37: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Demo 8: When Cursor becomes Bind Aware?

Query :b1 :b2 Optimal Rows

Processed Bucket Aware Child Actual q5 2 33 FTS/N2 1,825,131 2 q4 9 999 N1/FTS 6,233,815 2 q3 2 999 FTS/FTS 8,021,324 2 q1 9 33 N1/N2 37,382 1 q2 5 32 N1/N2 2 0

Guesstimate aware flag and actual plan

37

Page 38: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Demo 8: When Cursor becomes Bind Aware?

Query :b1 :b2 Optimal Rows

Processed Bucket Aware Child Actual q5 2 33 FTS/N2 1,825,131 2 N q4 9 999 N1/FTS 6,233,815 2 N q3 2 999 FTS/FTS 8,021,324 2 N q1 9 33 N1/N2 37,382 1 N q2 5 32 N1/N2 2 0 N

Guesstimate aware flag and actual plan

38

Page 39: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Demo 8: When Cursor becomes Bind Aware?

Query :b1 :b2 Optimal Rows

Processed Bucket Aware Child Actual q5 2 33 FTS/N2 1,825,131 2 N 0 FTS/N2 q4 9 999 N1/FTS 6,233,815 2 N 0 FTS/N2 q3 2 999 FTS/FTS 8,021,324 2 N 0 FTS/N2 q1 9 33 N1/N2 37,382 1 N 0 FTS/N2 q2 5 32 N1/N2 2 0 N 0 FTS/N2

Guesstimate aware flag and actual plan

39

Page 40: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Demo 8: When Cursor becomes Bind Aware?

Query :b1 :b2 Optimal Rows

Processed Bucket Aware Child Actual q5 2 33 FTS/N2 1,825,131 2 N 0 FTS/N2 q4 9 999 N1/FTS 6,233,815 2 N 0 FTS/N2 q3 2 999 FTS/FTS 8,021,324 2 N 0 FTS/N2 q1 9 33 N1/N2 37,382 1 N 0 FTS/N2 q2 5 32 N1/N2 2 0 N 0 FTS/N2

Guesstimate aware flag and actual plan

40

Page 41: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Real-life Problem with ACS People Soft Payroll Application

41

§ WHERE employee BETWEEN :b1 AND :b2 § Payroll for one employee

–  :b1 = 123456 –  :b2 = 123456

§ Payroll for one company –  :b1 = 000001 –  :b2 = 999999

§ Doing payroll for a few employees first then entire company

Page 42: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Closing Remarks

Page 43: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Understanding Selectivity Profile From demo 6

Query :b1 :b2 Child q3 2 999 1 q4 9 999 2 q5 2 33 3

43

Page 44: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Understanding Selectivity Profile From demo 6

Query :b1 :b2 Child q3 2 999 1 q4 9 999 2 q5 2 33 3

44

Page 45: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Understanding Selectivity Profile From demo 6

Query :b1 :b2 Child q3 2 999 1 q4 9 999 2 q5 2 33 3

45

Page 46: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Remarks on Bind Sensitivity

§ Monitor V$SQL_CS_STATISTICS.rows_processed –  If small number of rows then

§  V$SQL_CS_HISTOGRAM.bucket_id(0)++

–  If medium number of rows then §  V$SQL_CS_HISTOGRAM.bucket_id(1)++

–  If large number of rows then §  V$SQL_CS_HISTOGRAM.bucket_id(2)++

Based on experimental observation

46

Page 47: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Remarks on Bind Aware

§ Some cases where cursor may become bind aware –  bucket_id(0) = bucket_id(1) > 0 –  bucket_id(1) = bucket_id(2) > 0 –  bucket_id(0) > 0 and bucket_id(2) > 0

§ Or use /*+ BIND_AWARE */ CBO Hint –  What if we cannot modify code?

Based on experimental observation

47

Page 48: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Conclusions

§ ACS only applies to a subset of queries with binds § ACS requires a ramp-up process (few executions) §  In some cases cursor may fail to become bind aware § To force a cursor become bind aware use CBO Hint § ACS is not persistent § ACS works well with SQL Plan Management

ACS can produce multiple optimal plans for one query

48

Page 49: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

Give Away

§ Creates a SQL Patch for one SQL_ID § Turns “on” EVENT 10053 for SQL_ID § Hints on SQL Patch

–  GATHER_PLAN_STATISTICS –  MONITOR –  BIND_AWARE

§ Consider using and customizing this free script

Script sqlt/utl/coe_gen_sql_patch.sql (MOS 215187.1)

49

Page 50: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2

References and Contact Info

§ https://blogs.oracle.com/optimizer/ –  Insight into the workings of the Optimizer

Oracle Optimizer Blog

§  [email protected] § http://carlos-sierra.net § @csierra_usa

50

Page 51: Using adaptive cursor sharing (acs) to produce multiple optimal plans v2