Adaptive Cursor Sharing (ACS) is a feature available since 11g. It is enabled by default. ACS can help to generate multiple non-persistent Optimal Execution Plans for a given SQL. But it requires a sequence of events for it to get truly activated. This presentation describes what is ACS, when it is used and when it is not. Then it demonstrates ACS capabilities and limitations with a live demo.
This session is about: How Adaptive Cursor Sharing (ACS) actually works. How a bind sensitive cursor becomes bind aware. What are those "ACS buckets". How the "Selectivity Profile" works. Why sometimes your SQL becomes bind aware and why sometimes it does not. How is that ACS interacts with SQL Plan Management (SPM). These and other questions about ACS are answered in detail.
Some live demonstrations are used to illustrate the ramp-up process on ACS and how some child cursors are created then flagged as non-shareable. You will also "see" how the ACS Selectivity Profile is adapted as new executions make use of predicates with new Selectivities. ACS promotes Plan Flexibility while SPM promotes Plan Stability. Understanding how these duo interacts becomes of great value when some gentle intervention is needed to restore this delicate balance.
This session is for those Developers and DBAs that "need" to understand how things work. ACS can be seen as a back-box; or you can "look" inside and understand how it actually works. If you are curious about the ACS functionality, then this Session brings some light. Consider this session only if you are pretty familiar with Cursor Sharing, Binds, Plan Stability and Plan Flexibility.
6. Flexibility versus Stability
• Flexible Plans adapt as Data evolves over Time
• Performance of Stable Plans is more Predictable
• But, you can't eat your cake and have it (too)…
• Can we balance Flexibility and Stability???
Enkitec (c) 2014 6
7. Cursor Sharing Review
• Use of Bind Variables instead of Literals
– AND c.car_maker = ‘Ford’
– AND c.car_maker = :b1
• Goal: Reduce Hard Parsing
– Improve Scalability
• Reduce CPU utilization
• Reduce Shared Memory footprint
Enkitec (c) 2014 7
8. Cursor Sharing Shortcomings
• Flipping Plans
– Exacerbated by Histograms on Skewed Data
– AND c.car_maker = :b2
• :b2 := ‘Ford’
• :b2 := ‘Lotus’
– Plan is computed at hard parse
• Plan performance becomes a moving target
Enkitec (c) 2014 8
9. Adaptive Cursor Sharing (ACS)
• Available on 11g+
• Offers Multiple Optimal Plans per SQL
– As per “Selectivity” of Predicates
• :b2 := ‘Ford’ (many rows)
• :b2 := ‘Lotus’ (fewer rows)
• Works well with SQL Plan Management
Enkitec (c) 2014 9
10. ACS Challenges
• Strategy
– Minimize Resources Impact
• Implementation
– Monitor only a subset of SQL Statements
– Activate ACS only for a subset of the monitored SQL
– Share Executions Plans through a “Selectivity Profile”
Enkitec (c) 2014 10
11. Cursors State
• Bind Sensitive
– A subset of Cursors are Bind Sensitive
• Bind Aware
– A subset of Bind Sensitive Cursors become Bind Aware
Enkitec (c) 2014 11
All Statements
Bind Sensitive
Bind Aware
ACS
12. Becoming Bind Sensitive
1. SQL has Range Predicates on Bind Variables
– AND c.cust_year_of_birth BETWEEN :b3 AND :b4
– AND p.prod_category LIKE :b5
2. SQL has Equality Predicates on Bind Variables and
Column has a Histogram
– AND c.cust_marital_status = :b2
– AND TO_CHAR(s.time_id, 'YYYY') = :b6
Enkitec (c) 2014 12
13. Becoming Bind Aware
1. Rows Processed change substantially between
Executions
– Between a few rows to millions
2. Rows Processed oscillate significantly between
Executions
– Between a few rows and a few thousand
– Between a few thousand and millions
Enkitec (c) 2014 13
16. Rows Processed
• v$sql_cs_statistics.rows_processed
• Updated only at hard parse
• A measure of amount of work on Execution Plan
• Three sizes: S/M/L (undocumented boundaries)
– 0: Small (less than 1K rows)
– 1: Medium (between 1k and 1m rows)
– 2: Large (more than 1m rows)
Enkitec (c) 2014 16
17. ACS Buckets
• v$sql_cs_histogram.bucket_id
– 0: Small
– 1: Medium
– 2: Large
• v$sql_cs_histogram.count
– Incremented with each Execution as per
• v$sql_cs_statistics.rows_processed
Enkitec (c) 2014 17
18. Rows Processed and ACS Buckets
IF v$sql_cs_statistics.rows_processed < 1K THEN
v$sql_cs_histogram.count(0)++
ELSIF v$sql_cs_statistics.rows_processed < 1M THEN
v$sql_cs_histogram.count(1)++
ELSE
v$sql_cs_histogram.count(2)++
END IF
Enkitec (c) 2014 18
19. Becoming Bind Aware
1. Small and Large buckets have a value
– bucket_id.count(0) > 0 AND bucket_id.count(2) > 0
2. Two adjacent buckets have same non-zero value
– bucket_id.count(0) = bucket_id.count(1) > 0
– bucket_id.count(1) = bucket_id.count(2) > 0
Enkitec (c) 2014 19
20. Rows Processed per Execution
• rows_processed(bucket_id)… Bind Aware(BA)
• 10(0)… 50(0)… 3,000,000(2)… BA
• 30(0)… 3,000(1)… BA
• 2,000,000(2)… 1(0)… BA
• 0(0)… 10,000(1)… BA
• 3,000(1)… 2,000(1)… 200(0)… 300(0)… BA
• 10… 100… 500… 2,000… 3,000… 5,000… BA
Enkitec (c) 2014 20
21. WHY becoming BA is important?
• Multiple Optimal Plans are created after Cursor
becomes Bind Aware
Enkitec (c) 2014 21
22. Recap
• V$SQL_CS_STATISTICS (Rows Processed)
– Bind Sensitive Cursors determine which bucket should be
incremented according to actual rows processed
• V$SQL_CS_HISTOGRAM (3 Buckets S/M/L)
– Bind Sensitive Cursors increase count (+1) of respective
bucket at the end of each execution
• V$SQL_CS_SELECTIVITY (Selectivity Profile)
– Bind Aware Cursors maintain this “Selectivity” Data Structure
– To determine which Cursor (Plan) to use on each execution
Enkitec (c) 2014 22
23. Sample Query (1)
SELECT p.prod_subcategory_desc subcatagory,
SUM(amount_sold) amount_sold
FROM sh.customers c,
sh.products p,
sh.sales s
WHERE c.cust_gender = 'M'
AND c.cust_marital_status = 'single'
AND c.cust_year_of_birth BETWEEN 1913 AND 1990
AND p.prod_category LIKE 'Software%'
AND TO_CHAR(s.time_id, 'YYYY') = '2001'
AND s.cust_id = c.cust_id
AND s.prod_id = p.prod_id
GROUP BY
p.prod_subcategory_desc
ORDER BY
p.prod_subcategory_desc;
Enkitec (c) 2014 23
24. Sample Query (2)
• Based on Sample Schema SH
– With CBO Histograms in all Columns
• Not a requirement for this ACS test
• 3 Tables with Filter Predicates
• 2 Joins
• 6 possible Join Orders
• Several possible Execution Plans
Enkitec (c) 2014 24
25. Demo 1
• 5 Executions of Sample Query using Literals
– Different values for each Execution
• Sequence 1, 2, 3, 4 and 5
– Each Execution performs a Hard Parse
– Each Execution computes a “new” Plan
– Each seems to be an “Optimal” Plan
Enkitec (c) 2014 25
26. Demo 2
• 5 Executions of Sample Query using Binds
– Different values for each Execution
• Sequence 1, 2, 3, 4 and 5
– Each Execution performs a Hard Parse
• Forced with a Cursor Flush before the Execution
– Each computes a “new” Optimal Plan
• Almost same as “with Literals”
Enkitec (c) 2014 26
28. Demo 3
• 5 Executions of Sample Query using Binds
– Different values for each Execution
• Sequence 1, 2, 3, 4 and 5
– No Cursor Flush between Executions
– First Execution computes a “new” Optimal Plan
– All Executions use same Plan…
Enkitec (c) 2014 28
29. Demo 3 Results
Query Rows Processed ACS Bucket Optimal
Plan
ACS Aware Executed
1 1,483,124 2 2048551027 N 2048551027
2 1,280,074 (~1.2M) 2 3600618656 N 2048551027
3 1,017,774 (~1.2M) 2 189372815 N 2048551027
4 2,770 (~526K ) 1 847574763 N 2048551027
5 3,132 (~50) 1 (0) 847574763 N 2048551027
Enkitec (c) 2014 29
30. Demo 4
• 5 Executions of Sample Query using Binds
– Different values for each Execution
• Sequence 5, 4, 3, 2 and 1
– No Cursor Flush between Executions
– Cursor becomes Bind Aware after 2nd Execution
– All Executions used an Optimal Plan
Enkitec (c) 2014 30
31. Demo 4 Results
Query Rows Processed ACS Bucket Optimal
Plan
Bind Aware Executed
5 3,132 1 847574763 N 847574763
4 2,770 1 847574763 N 847574763
3 1,017,774 (~130K) 2 (1) 189372815 N 847574763
2 1,280,074 (~241K) 2 (1) 3600618656 N 847574763
1 1,483,124 (~1.7M) 2 2048551027 N 847574763
Enkitec (c) 2014 31
32. Demo 5
• 5 Executions of Sample Query using Binds
– Different values for each Execution
• Sequence 5, 1, 2, 3 and 4
– No Cursor Flush between Executions
– Cursor becomes Bind Aware after 2nd Execution
– All but one Executions used an Optimal Plan
Enkitec (c) 2014 32
33. Demo 5 Results
Query Rows Processed ACS Bucket Optimal
Plan
Bind Aware Executed
5 3,132 1 847574763 N 847574763
1 1,483,124 (1.7M) 2 2048551027 N 847574763
2 1,280,074 2 3600618656 Y 3600618656
3 1,017,774 2 189372815 Y 189372815
4 2,770 1 847574763 Y 847574763
Enkitec (c) 2014 33
34. What is the Problem?
• Ramp-up Process may lead to some suboptimal
Executions
– Sensitive to sequence of values passed…
• Kind of a Learning Curve…
– Can we override it?
Enkitec (c) 2014 34
35. Controlling ACS with CBO Hint
• /*+ BIND_AWARE */
– Bypasses the monitoring phase of a Bind Sensitive SQL
• /*+ NO_BIND_AWARE */
– Turns off ACS for given SQL
Enkitec (c) 2014 35
37. ACS Plan Selection
• On every Execution of Bind Aware Cursor
– Compute Selectivity of each qualifying Predicate
– Search Selectivity within Range of values on ACS
Selectivity Profile
– If within Range, lookup Child Number and use its Plan
– Else, Hard Parse and Execute newly computed Plan
• If same as existing Plan, then update Selectivity Profile
• Else, create Selectivity Profile for new Child Number
Enkitec (c) 2014 37
40. ACS Summary
• ACS is capable of producing multiple Optimal
Execution Plans per SQL
• During ramp-up sub Optimal Plans may happen
• ACS Metadata resides in Memory (not Persistent)
• ACS provides desirable Plan Flexibility
• ACS does not address the Plan Stability concern
Enkitec (c) 2014 40
41. ACS Suggested Strategy
• Use sys.dbms_sqldiag_internal.i_create_patch to
SQL Patch with BIND_AWARE the SQL on Baselines
• Use free script sqlpch.sql
Enkitec (c) 2014 41
42. References (1)
• Using SQL Patch to add hints to a packaged
application
– https://blogs.oracle.com/optimizer/entry/how_can_i_h
int_a
• Skipping ACS ramp-up using a SQL Patch
– http://carlos-sierra.net/2014/06/19/skipping-acs-ramp-
up-using-a-sql-patch/
Enkitec (c) 2014 42
43. References (2)
• Oracle® Database PL/SQL Packages and Types
Reference
– 11g Release 2 (11.2)
– Part Number E25788-04
Enkitec (c) 2014 43