Understanding How is that Adaptive Cursor Sharing (ACS) produces multiple Optimal Plans
-
Upload
carlos-sierra -
Category
Software
-
view
5.194 -
download
0
description
Transcript of Understanding How is that Adaptive Cursor Sharing (ACS) produces multiple Optimal Plans
Understanding how is that Adaptive Cursor Sharing (ACS)
produces multiple Optimal Plans
Carlos Sierra
Enkitec (c) 2014 2
Carlos Sierra• SQLTXPLAIN + SQL Health-Check SQLHC +• Consultant/Developer/DBA/Design/+• Oracle Performance + SQL Tuning• Oracle Database Health-Check• Tools + Scripts• Speaker
QuestionEverything
Enkitec (c) 2014 3
Topics• Motivation• Adaptive Cursor Sharing (ACS)– Mechanics– Demos
Enkitec (c) 2014 4
Plan Flexibility Allies• CBO Parameters• CBO Statistics• Dynamic Sampling• Cardinality Feedback• Cursor Sharing• Adaptive Cursor Sharing (ACS)
Enkitec (c) 2014 5
Plan Stability Tools• CBO Hints• Stored Outlines• SQL Profiles• SQL Plan Management (SPM)
Enkitec (c) 2014 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 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 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 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 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 11
Cursors State• Bind Sensitive– A subset of Cursors are Bind Sensitive
• Bind Aware– A subset of Bind Sensitive Cursors become Bind Aware
All Statements
Bind Sensitive
Bind Aware
ACS
Enkitec (c) 2014 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 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 14
ACS Dynamic Views• V$SQL (Cursor State)– is_shareable (Y/N)– is_bind_sensitive (Y/N)– is_bind_aware (Y/N)
• V$SQL_CS_STATISTICS (Rows Processed)• V$SQL_CS_HISTOGRAM (3 Buckets S/M/L)• V$SQL_CS_SELECTIVITY (Selectivity Profile)
Enkitec (c) 2014 15
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– 0: Small – 1: Medium – 2: Large
Enkitec (c) 2014 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 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 18
Rows Processed and ACS BucketsIF 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 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 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 21
WHY becoming BA is important?• Multiple Optimal Plans are created after Cursor
becomes Bind Aware
Enkitec (c) 2014 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 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 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 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 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 27
Demo 2 ResultsQuer
yRows Processed ACS Bucket Plan Hash Value
1 1,483,124 2 20485510272 1,280,074 2 36006186563 1,017,774 2 1893728154 2,770 1 8475747635 3,132 1 847574763
Enkitec (c) 2014 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 29
Demo 3 ResultsQuer
yRows Processed ACS Bucket Optimal Plan ACS Aware Executed
1 1,483,124 2 2048551027 N 20485510272 1,280,074 (~1.2M) 2 3600618656 N 20485510273 1,017,774 (~1.2M) 2 189372815 N 20485510274 2,770 (~526K ) 1 847574763 N 20485510275 3,132 (~50) 1 (0) 847574763 N 2048551027
Enkitec (c) 2014 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 31
Demo 4 ResultsQuer
yRows Processed ACS Bucket Optimal Plan Bind Aware Executed
5 3,132 1 847574763 N 8475747634 2,770 1 847574763 N 8475747633 1,017,774 (~130K) 2 (1) 189372815 N 8475747632 1,280,074 (~241K) 2 (1) 3600618656 N 8475747631 1,483,124 (~1.7M) 2 2048551027 N 847574763
Enkitec (c) 2014 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 33
Demo 5 ResultsQuer
yRows Processed ACS Bucket Optimal Plan Bind Aware Executed
5 3,132 1 847574763 N 8475747631 1,483,124 (1.7M) 2 2048551027 N 8475747632 1,280,074 2 3600618656 Y 36006186563 1,017,774 2 189372815 Y 1893728154 2,770 1 847574763 Y 847574763
Enkitec (c) 2014 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 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 36
Controlling ACS with SQL Patch• SYS.DBMS_SQLDIAG_INTERNAL.I_CREATE_PATCH– sql_text– hint_text => BIND_AWARE
• Script sqlpch.sql connected as SYS– SQL_ID • 8u0n7w1jug5dg
• Test Demo 5…
Enkitec (c) 2014 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 38
Selectivity Profile (1)• v$sql_cs_selectivity– predicate– range_id• low and high (selectivities)
– child_number
Enkitec (c) 2014 39
Selectivity Profile (2) CHILD PREDICATE RANGE_ID LOW HIGH----------- ---------- ----------- ---------- ---------- 1 <=B4 0 0.860941 1.052262 1 =B1 0 0.602369 0.736229 1 =B2 0 0.455337 0.556523 1 >=B3 0 0.182445 0.222988 1 B5 0 0.306250 0.374306 2 <=B4 0 0.892666 1.091036 2 =B1 0 0.297574 0.363702 2 =B2 0 0.455337 0.556523 2 >=B3 0 0.077947 0.095268 2 B5 0 0.306250 0.374306 3 <=B4 0 0.836835 1.022798 3 =B1 0 0.297574 0.363702 3 =B2 0 0.002085 0.002548 3 >=B3 0 0.221447 0.270657 3 B5 0 0.306250 0.374306
Enkitec (c) 2014 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 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 42
References (1)• Using SQL Patch to add hints to a packaged
application– https://blogs.oracle.com/optimizer/entry/
how_can_i_hint_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 43
References (2)• Oracle® Database PL/SQL Packages and Types
Reference– 11g Release 2 (11.2)– Part Number E25788-04
Enkitec (c) 2014 44
Contact Information• [email protected]• carlos-sierra.net• @csierra_usa