72
Combining Adap-ve Cursor Sharing (ACS) Flexibility with SQL Plan Management (SPM) Stability Carlos Sierra

Combining ACS Flexibility with SPM Stability

  • Upload
    enkitec

  • View
    249

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Combining ACS Flexibility with SPM Stability

Combining  Adap-ve  Cursor  Sharing  (ACS)  Flexibility  

with    SQL  Plan  Management  (SPM)  Stability  

Carlos  Sierra  

Page 2: Combining ACS Flexibility with SPM Stability

Carlos  Sierra  •  Consultant/Developer/DBA  •  Oracle  Performance  •  SQLTXPLAIN  (SQLT)  •  SQL  Tuning  •  Tools  

Enkitec  (c)  2014   2  

Page 3: Combining ACS Flexibility with SPM Stability

Topics  •  Plan  Flexibility  and  Plan  Stability  •  Adap-ve  Cursor  Sharing  (ACS)  •  SQL  Plan  Management  (SPM)  •  Combining  ACS  and  SPM  

Enkitec  (c)  2014   3  

Page 4: Combining ACS Flexibility with SPM Stability

Plan  Flexibility  •  Cost-­‐based  Op-mizer  (CBO)  –  Introduced  in  Oracle  7  – Computes  an  Op-mal  Plan  based  on    •  Heuris-cs  •  Query  Predicates  •  Schema  Object  Sta-s-cs  •  Several  Others  

Enkitec  (c)  2014   4  

Page 5: Combining ACS Flexibility with SPM Stability

Flexibility:  Good  or  Evil?  •  CBO  Mission  – Compute  an  Op-mal  Plan  for  a  given  SQL  and  its  Data  

•  Plans  have  many  and  complex  Dependencies  •  CBO  Performance  – Successful  most  of  the  -mes  •  Produces  a  sub-­‐op-mal  Plan  some&mes  

Enkitec  (c)  2014   5  

Page 6: Combining ACS Flexibility with SPM Stability

DBA  Recurrent  Bad  Dream  •  A  business  cri-cal  process  experiences  an  occasional  slow  down  – Same  SQL  usually  runs  very  quickly  – Developers  claim  they  haven’t  changed  anything  – Users  claim  they  do  the  same  every  -me  – Business  is  sick  and  -red  of  repor-ng  this  to…  YOU!  

Enkitec  (c)  2014   6  

Page 7: Combining ACS Flexibility with SPM Stability

Plan  Performs  Inconsistently?  (1)  •  Concurrency  with  some  other  Process  •  State  of  Buffer  Cache  •  RAC  Node  Affinity  •  CPU  Starva-on  •  Sudden  Changes  on  Data  Volume  •  Inconsistent  Performance  through  Database  Link  

Enkitec  (c)  2014   7  

Page 8: Combining ACS Flexibility with SPM Stability

Plan  Performs  Inconsistently?  (2)  •  Downgraded  Parallel  Execu-on  •  Crossing  Small-­‐Table  Threshold  •  Resource  Manager  Direc-ve  •  Plan  Flips  •  Many  Others…  

Enkitec  (c)  2014   8  

Page 9: Combining ACS Flexibility with SPM Stability

WHY  a  Plan  Flips?  (1)  •  Bind  Variable  Peeking  with  Histograms  •  Outdated  Schema  Object  (SO)  Sta-s-cs  •  Missing  SO  Sta-s-cs  •  Fresh  SO  Sta-s-cs  •  New  Empty  Par--ons  

Enkitec  (c)  2014   9  

Page 10: Combining ACS Flexibility with SPM Stability

WHY  a  Plan  Flips?  (2)  •  Incomplete  set  of  CBO  Hints  – Either  on  SQL  Text  or  SQL  Profile  

•  Dropped,  Invalid,  Invisible  or  new  Indexes  •  Index  or  Table  rebuild  •  Two  or  more  Plans  with  similar  Cost  

Enkitec  (c)  2014   10  

Page 11: Combining ACS Flexibility with SPM Stability

WHY  a  Plan  Flips?  (3)  •  Database  Parameters  Changes  •  CBO  System  Sta-s-cs  Changes  •  Asynchronous  SO  Sta-s-cs  Gathering  •  Many  Others…  

Enkitec  (c)  2014   11  

Page 12: Combining ACS Flexibility with SPM Stability

How  to  Mi-gate  Plan  Flipping  •  Sound  CBO  Sta-s-cs  •  Default  CBO  Parameters  •  Regulate  Global  Changes  

Enkitec  (c)  2014   12  

Page 13: Combining ACS Flexibility with SPM Stability

Plan  Flexibility  Allies  •  CBO  Parameters  •  CBO  Sta-s-cs  •  Dynamic  Sampling  •  Cardinality  Feedback  •  Cursor  Sharing  •  Adap,ve  Cursor  Sharing  (ACS)  

Enkitec  (c)  2014   13  

Page 14: Combining ACS Flexibility with SPM Stability

Plan  Stability  Tools  •  CBO  Hints  •  Stored  Outlines  •  SQL  Profiles  •  SQL  Plan  Management  (SPM)  

Enkitec  (c)  2014   14  

Page 15: Combining ACS Flexibility with SPM Stability

Cursor  Sharing  •  Use  of  Bind  Variables  instead  of  Literals  – AND  p.prod_category  LIKE  'Sooware%’  – AND  p.prod_category  LIKE  :b5  

•  Goal:  Reduce  Hard  Parsing  –  Improve  Scalability  •  Reduce  CPU  u-liza-on  •  Reduce  Shared  Memory  footprint  

Enkitec  (c)  2014   15  

Page 16: Combining ACS Flexibility with SPM Stability

Cursor  Sharing  Shortcomings  •  Flipping  Plans  – Exacerbated  by  Histograms  on  Skewed  Data    – AND  c.cust_marital_status  =  :b2  •  :b2  :=  ‘married’  •  :b2  :=  ‘widow’  

– Plan  is  computed  at  hard  parse  •  Plan  becomes  a  long  term  moving  target  

Enkitec  (c)  2014   16  

Page 17: Combining ACS Flexibility with SPM Stability

Adap-ve  Cursor  Sharing  (ACS)  •  11g+  •  Mul-ple  Op-mal  Plans  per  SQL  – As  per  “Selec-vity”  of  Predicates  •  :b2  :=  ‘married’  (close  to  60%  maybe)  •  :b2  :=  ‘widow’  (this  one  is  more  selec-ve)  

Enkitec  (c)  2014   17  

Page 18: Combining ACS Flexibility with SPM Stability

ACS  Challenges  •  Minimize  Resources  Impact  – Monitor  only  a  subset  of  SQL  Statements  – Ac-vate  ACS  only  for  a  subset  of  the  monitored  SQL  – Share  Execu-ons  Plans  through  a  “Selec-vity  Profile”  

Enkitec  (c)  2014   18  

All  Statements  

Bind  Sensi-ve  

Bind  Aware  

ACS  

Page 19: Combining ACS Flexibility with SPM Stability

Becoming  Bind  Sensi-ve  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.-me_id,  'YYYY')  =  :b6  

Enkitec  (c)  2014   19  

Page 20: Combining ACS Flexibility with SPM Stability

Becoming  Bind  Aware  1.  Rows  Processed  change  substan-ally  between  

Execu-ons  – Between  a  few  rows  to  millions  

2.  Rows  Processed  oscillate  significantly  between  Execu-ons  – Between  a  few  rows  and  a  few  thousand  – Between  a  few  thousand  and  millions  

Enkitec  (c)  2014   20  

Page 21: Combining ACS Flexibility with SPM Stability

ACS  Monitoring  •  V$SQL  (State)  –  is_shareable  –  is_bind_sensi-ve  –  is_bind_aware  

•  V$SQL_CS_STATISTICS  (Rows  Processed)  •  V$SQL_CS_HISTOGRAM  (3  Buckets  S/M/L)  •  V$SQL_CS_SELECTIVITY  (Selec-vity  Profile)  

Enkitec  (c)  2014   21  

Page 22: Combining ACS Flexibility with SPM Stability

Rows  Processed  •  v$sql_cs_sta-s-cs.rows_processed  •  Updated  only  at  hard  parse  •  A  measure  of  amount  of  work  on  Execu-on  Plan  •  Three  sizes:  S/M/L  – 0:  Small    – 1:  Medium    – 2:  Large  

Enkitec  (c)  2014   22  

Page 23: Combining ACS Flexibility with SPM Stability

Rows  Processed  •  v$sql_cs_sta-s-cs.rows_processed  •  Updated  only  at  hard  parse  •  A  measure  of  amount  of  work  on  Execu-on  Plan  •  Three  sizes:  S/M/L  – 0:  Small  (less  than  1K  rows)  – 1:  Medium  (between  1k  and  1m  rows)  – 2:  Large  (more  than  1m  rows)  

Enkitec  (c)  2014   23  

Page 24: Combining ACS Flexibility with SPM Stability

ACS  Buckets  •  v$sql_cs_histogram.bucket_id  – 0:  Small  – 1:  Medium  – 2:  Large  

•  v$sql_cs_histogram.count  –  Incremented  with  each  Execu-on  as  per  •  v$sql_cs_sta-s-cs.rows_processed  

Enkitec  (c)  2014   24  

Page 25: Combining ACS Flexibility with SPM Stability

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   25  

Page 26: Combining ACS Flexibility with SPM Stability

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   26  

Page 27: Combining ACS Flexibility with SPM Stability

Rows  Processed  per  Execu-on  •  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  •  rows_processed(bucket_id)…  Bind  Aware(BA)  

Enkitec  (c)  2014   27  

Page 28: Combining ACS Flexibility with SPM Stability

WHY  becoming  BA  is  important?  •  Mul-ple  Op-mal  Plans  are  created  aoer  Cursor  becomes  Bind  Aware  

Enkitec  (c)  2014   28  

Page 29: Combining ACS Flexibility with SPM Stability

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   29  

Page 30: Combining ACS Flexibility with SPM Stability

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  Execu-on  Plans  

Enkitec  (c)  2014   30  

Page 31: Combining ACS Flexibility with SPM Stability

Demo  1  •  5  Execu-ons  of  Sample  Query  using  Literals  – Different  values  for  each  Execu-on  •  Sequence  1,  2,  3,  4  and  5  

– Each  Execu-on  performs  a  Hard  Parse  – Each  Execu-on  computes  a  “new”  Plan  – Each  seems  to  be  an  “Op-mal”  Plan  

Enkitec  (c)  2014   31  

Page 32: Combining ACS Flexibility with SPM Stability

Demo  2  •  5  Execu-ons  of  Sample  Query  using  Binds  – Different  values  for  each  Execu-on  •  Sequence  1,  2,  3,  4  and  5  

– Each  Execu-on  performs  a  Hard  Parse  •  Forced  with  a  Cursor  Flush  before  the  Execu-on  

– Each  computes  a  “new”  Op-mal  Plan  •  Almost  same  as  “with  Literals”  

Enkitec  (c)  2014   32  

Page 33: Combining ACS Flexibility with SPM Stability

Demo  2  Results  Query   Rows  Processed   ACS  Bucket   Plan  Hash  Value  

1   1,483,124   2   2048551027  2   1,272,154   2   3022804314  3   1,014,876   2   2326939410  4   716,168   1   2163719564  5   530   0   2163719564  

Enkitec  (c)  2014   33  

Page 34: Combining ACS Flexibility with SPM Stability

Demo  3  •  5  Execu-ons  of  Sample  Query  using  Binds  – Different  values  for  each  Execu-on  •  Sequence  1,  2,  3,  4  and  5  

– No  Cursor  Flush  between  Execu-ons  – First  Execu-on  computes  a  “new”  Op-mal  Plan  – All  Execu-ons  use  same  Plan…  

Enkitec  (c)  2014   34  

Page 35: Combining ACS Flexibility with SPM Stability

Demo  3  Results  Query   Rows  Processed   ACS  Bucket   Op,mal  Plan   ACS  Aware   Executed  

1   1,483,124   2   2048551027   N   2048551027  2   1,272,154   2   3022804314   N   2048551027  3   1,014,876   2   2326939410   N   2048551027  4   716,168   1   2163719564   N   2048551027  5   530   0   2163719564   N   2048551027  

Enkitec  (c)  2014   35  

Page 36: Combining ACS Flexibility with SPM Stability

Demo  4  •  5  Execu-ons  of  Sample  Query  using  Binds  – Different  values  for  each  Execu-on  •  Sequence  5,  4,  3,  2  and  1  

– No  Cursor  Flush  between  Execu-ons  – Cursor  becomes  Bind  Aware  aoer  2nd  Execu-on  – All  Execu-ons  used  an  Op-mal  Plan  

Enkitec  (c)  2014   36  

Page 37: Combining ACS Flexibility with SPM Stability

Demo  4  Results  Query   Rows  Processed   ACS  Bucket   Op,mal  Plan   Bind  Aware   Executed  

5   530   0   2163719564   N   2163719564  4   716,168   1   2163719564   N   2163719564  3   1,014,876   2   2326939410   Y   2326939410  2   1,272,154   2   3022804314   Y   3022804314  1   1,483,124   2   2048551027   Y   2048551027  

Enkitec  (c)  2014   37  

Page 38: Combining ACS Flexibility with SPM Stability

Demo  5  •  5  Execu-ons  of  Sample  Query  using  Binds  – Different  values  for  each  Execu-on  •  Sequence  5,  1,  2,  3  and  4  

– No  Cursor  Flush  between  Execu-ons  – Cursor  becomes  Bind  Aware  aoer  2nd  Execu-on  – All  but  one  Execu-ons  used  an  Op-mal  Plan  

Enkitec  (c)  2014   38  

Page 39: Combining ACS Flexibility with SPM Stability

Demo  5  Results  Query   Rows  Processed   ACS  Bucket   Op,mal  Plan   Bind  Aware   Executed  

5   530   0   2163719564   N   2163719564  1   1,483,124   2   2048551027   N   2163719564  2   1,272,154   2   3022804314   Y   3022804314  3   1,014,876   2   2326939410   Y   2326939410  4   716,168   1   2163719564   Y   2163719564  

Enkitec  (c)  2014   39  

Page 40: Combining ACS Flexibility with SPM Stability

Controlling  ACS  with  CBO  Hint  •  /*+  BIND_AWARE  */  – Bypasses  the  monitoring  phase  of  a  Bind  Sensi-ve  SQL  

•  /*+  NO_BIND_AWARE  */  – Turns  off  ACS  for  given  SQL  

Enkitec  (c)  2014   40  

Page 41: Combining ACS Flexibility with SPM Stability

Controlling  ACS  with  SQL  Patch  •  SYS.DBMS_SQLDIAG_INTERNAL.I_CREATE_PATCH  – sql_text  – hint_text  =>  BIND_AWARE  

•  Script  sqlpch.sql  

Enkitec  (c)  2014   41  

Page 42: Combining ACS Flexibility with SPM Stability

ACS  Plan  Selec-on  •  On  every  Execu-on  of  Bind  Aware  Cursor  – Compute  Selec-vity  of  each  qualifying  Predicate  – Search  Selec-vity  within  Range  of  values  on  ACS  Selec-vity  Profile  

–  If  within  Range,  lookup  Child  Number  and  use  its  Plan  – Else,  Hard  Parse  and  Execute  newly  computed  Plan  •  If  same  as  exis-ng  Plan,  then  update  Selec-vity  Profile  •  Else,  create  Selec-vity  Profile  for  new  Child  Number  

Enkitec  (c)  2014   42  

Page 43: Combining ACS Flexibility with SPM Stability

Selec-vity  Profile  (1)  •  v$sql_cs_selec-vity  – predicate  –  range_id  •  low  and  high  (selec-vi-es)  

– child_number  

Enkitec  (c)  2014   43  

Page 44: Combining ACS Flexibility with SPM Stability

Selec-vity  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   44  

Page 45: Combining ACS Flexibility with SPM Stability

ACS  Summary  •  ACS  is  capable  of  producing  mul-ple  Op-mal  Execu-on  Plans  per  SQL  

•  During  ramp-­‐up  sub  Op-mal  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   45  

Page 46: Combining ACS Flexibility with SPM Stability

SQL  Plan  Management  •  11g+  •  Mul-ple  Persistent  Op-mal  Execu-on  Plans  per  SQL  

•  Only  “accepted”  and  “enabled”  Plans  are  Executed  •  New  Plans  are  acknowledged  but  not  Executed  •  Goal:  Plan  Stability  with  controlled  Flexibility    

Enkitec  (c)  2014   46  

Page 47: Combining ACS Flexibility with SPM Stability

SQL  Plan  Baseline  •  A  set  of  Plans  available  to  the  CBO  for  a  given  SQL  –  Iden-fied  by  SQL  Handle  and  Signature    •  Hash  func-on  on  SQL  Text  •  dbms_sqltune.sqltext_to_signature  

– View  dba_sql_plan_baselines  •  enabled  =  YES  •  accepted  =  YES  •  reproduced  =  YES  

Enkitec  (c)  2014   47  

Page 48: Combining ACS Flexibility with SPM Stability

Plan  History  •  Content  of  dba_sql_plan_baselines  •  Includes  SQL  Plan  Baseline    •  Includes  Pending  Plans  – accepted  =  NO  and  last_verified  is  NULL  

•  Includes  Rejected  Plans  – accepted  =  NO  and  last_verified  is  not  NULL  

•  Includes  Disabled  Plans  Enkitec  (c)  2014   48  

Page 49: Combining ACS Flexibility with SPM Stability

Methods  to  Create  a  Plan  Baseline  1.  Capture  2.  Load  

a.  Cursor  Cache  (CUR)  b.  SQL  Tuning  Set  (STS)  c.  Stored  Outline  

3.  Migra-on  

Enkitec  (c)  2014   49  

1.  Capture  2.  Load  

a.  CUR  b.  STS  c.  AWR  d.  SPA  e.  TRC  

3.  Migra-on  

Page 50: Combining ACS Flexibility with SPM Stability

Capturing  a  SQL  Plan  Baseline  (1)  •  Set  op-mizer_use_sql_plan_baselines  to  TRUE  (default)  

•  Set  op-mizer_capture_sql_plan_baselines  to  TRUE  (default  is  FALSE)  – Set  this  parameter  at  SESSION  level  

•  Execute  SQL  2  -mes  •  Set  op-mizer_capture_sql_plan_baselines  to  FALSE  

Enkitec  (c)  2014   50  

Page 51: Combining ACS Flexibility with SPM Stability

Capturing  a  SQL  Plan  Baseline  (2)  

Enkitec  (c)  2014   51  

Page 52: Combining ACS Flexibility with SPM Stability

Loading  SQL  Plan  Baseline  from  Cache  •  dbms_spm.load_plans_from_cursor_cache  –  Inputs  •  sql_id  •  plan_hash_value  (opt)  

– Outputs  •  Number  of  Plans  loaded  

Enkitec  (c)  2014   52  

Page 53: Combining ACS Flexibility with SPM Stability

Demo  6  •  Load  Plans  for  Query  out  of  Demo  5  – demo5.sql  –  load_plans.sql  

•  Execute  Demo  5  again  and  review  Plan  History  – demo5.sql  –  list_plans.sql  

•  Execute  demo5.sql  twice  and  verify  results  

Enkitec  (c)  2014   53  

Page 54: Combining ACS Flexibility with SPM Stability

Demo  6  Results  (aoer  1st  demo5.sql)              CHILD    EXECUTIONS  BUFFER_GETS  PLAN_HASH_VALUE  SHAR  SENS  AWRE  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐                      0                      3                9090            3022804314  Y        Y        N                CHILD      BUCKET_ID              COUNT  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐                      0                      0                      0                      0                      1                      1                      0                      2                      2  Note  -­‐-­‐-­‐-­‐-­‐        -­‐  SQL  plan  baseline  SQL_PLAN_652hmt7yxthdwd624c0cd  used  for  this  statement    CREATED                                                PLAN_NAME                                            ENA  ACC  REP  FIX  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐  -­‐-­‐-­‐  -­‐-­‐-­‐  -­‐-­‐-­‐  28-­‐DEC-­‐13  02.03.43.000000  PM      SQL_PLAN_652hmt7yxthdw501f1424  YES  YES  YES  NO  28-­‐DEC-­‐13  02.03.43.000000  PM      SQL_PLAN_652hmt7yxthdwb6c39290  YES  YES  YES  NO  28-­‐DEC-­‐13  02.03.43.000000  PM      SQL_PLAN_652hmt7yxthdwd624c0cd  YES  YES  YES  NO  28-­‐DEC-­‐13  02.03.50.000000  PM      SQL_PLAN_652hmt7yxthdwc82a6b0c  YES  NO    YES  NO  

Enkitec  (c)  2014   54  

Page 55: Combining ACS Flexibility with SPM Stability

Demo  6  Results  (aoer  4rd  demo5.sql)              CHILD    EXECUTIONS  BUFFER_GETS  PLAN_HASH_VALUE  SHAR  SENS  AWRE  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐                      0                      2                4274            2163719564  N        Y        N                      1                      1                3030            3022804314  Y        Y        Y                      2                      1                3030            2326939410  Y        Y        Y                      3                      1                1587            2163719564  Y        Y        Y  Note  -­‐-­‐-­‐-­‐-­‐        -­‐  SQL  plan  baseline  SQL_PLAN_652hmt7yxthdwb6c39290  used  for  this  statement    CREATED                                                PLAN_NAME                                            ENA  ACC  REP  FIX  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐  -­‐-­‐-­‐  -­‐-­‐-­‐  -­‐-­‐-­‐  28-­‐DEC-­‐13  02.03.43.000000  PM      SQL_PLAN_652hmt7yxthdw501f1424  YES  YES  YES  NO  28-­‐DEC-­‐13  02.03.43.000000  PM      SQL_PLAN_652hmt7yxthdwb6c39290  YES  YES  YES  NO  28-­‐DEC-­‐13  02.03.43.000000  PM      SQL_PLAN_652hmt7yxthdwd624c0cd  YES  YES  YES  NO  28-­‐DEC-­‐13  02.03.50.000000  PM      SQL_PLAN_652hmt7yxthdwc82a6b0c  YES  NO    YES  NO  

Enkitec  (c)  2014   55  

Page 56: Combining ACS Flexibility with SPM Stability

Demo  7  •  Drop  SQL  Plan  Baseline  – drop_plans.sql  

•  Create  SQL  Patch  with  /*+  BIND_AWARE  */  Hint  – sqlpch.sql  connected  as  SYS  

•  Load  Plans  for  Query  out  of  Demo  5  – demo5.sql  –  load_plans.sql  

Enkitec  (c)  2014   56  

Page 57: Combining ACS Flexibility with SPM Stability

Demo  7  Results              CHILD    EXECUTIONS  BUFFER_GETS  PLAN_HASH_VALUE  SHAR  SENS  AWRE  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐                      0                      1                3052            2163719564  N        Y        Y                      1                      1                3030            2048551027  Y        Y        Y                      2                      1                3030            3022804314  Y        Y        Y                      3                      1                3030            2326939410  Y        Y        Y                      4                      1                1587            2163719564  Y        Y        Y    CREATED                                                PLAN_NAME                                            ENA  ACC  REP  FIX  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐  -­‐-­‐-­‐  -­‐-­‐-­‐  -­‐-­‐-­‐  28-­‐DEC-­‐13  02.31.11.000000  PM      SQL_PLAN_652hmt7yxthdw501f1424  YES  YES  YES  NO  28-­‐DEC-­‐13  02.31.11.000000  PM      SQL_PLAN_652hmt7yxthdwb6c39290  YES  YES  YES  NO  28-­‐DEC-­‐13  02.31.11.000000  PM      SQL_PLAN_652hmt7yxthdwc82a6b0c  YES  YES  YES  NO  28-­‐DEC-­‐13  02.31.11.000000  PM      SQL_PLAN_652hmt7yxthdwd624c0cd  YES  YES  YES  NO      

Enkitec  (c)  2014   57  

Page 58: Combining ACS Flexibility with SPM Stability

Demo  8  •  Execute  queries  1-­‐5  and  observe  how  number  of  Execu-ons  increases  per  Child  Cursor  – flush.sql  – demo8.sql  – demo8.sql  – demo8.sql  

Enkitec  (c)  2014   58  

Page 59: Combining ACS Flexibility with SPM Stability

Demo  8  Results    CHILD    EXECUTIONS  BUFFER_GETS  PLAN_HASH_VALUE  SHAR  SENS  AWRE                                                                                                                                                                                                                                                                              -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐                                                                                                                                                                                                                                                                                                  0                      1                1585            2163719564  N        Y        Y                                                                                                                                                                                                                                                                                                        1                      1                3030            2048551027  Y        Y        Y                                                                                                                                                                                                                                                                                                        2                      1                3030            3022804314  Y        Y        Y                                                                                                                                                                                                                                                                                                        3                      1                3030            2326939410  Y        Y        Y                                                                                                                                                                                                                                                                                                        4                      2                3172            2163719564  Y        Y        Y  Note  -­‐-­‐-­‐-­‐-­‐        -­‐  SQL  patch  "sqlpch_8u0n7w1jug5dg"  used  for  this  statement        -­‐  SQL  plan  baseline  SQL_PLAN_652hmt7yxthdwb6c39290  used  for  this  statement                CHILD    EXECUTIONS  BUFFER_GETS  PLAN_HASH_VALUE  SHAR  SENS  AWRE                                                                                                                                                                                                                                                                              -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐  -­‐-­‐-­‐-­‐                                                                                                                                                                                                                                                                                                  0                      1                1585            2163719564  N        Y        Y                                                                                                                                                                                                                                                                                                        1                      2                6060            2048551027  Y        Y        Y                                                                                                                                                                                                                                                                                                        2                      2                6060            3022804314  Y        Y        Y                                                                                                                                                                                                                                                                                                        3                      2                6060            2326939410  Y        Y        Y                                                                                                                                                                                                                                                                                                        4                      4                6344            2163719564  Y        Y        Y    

Enkitec  (c)  2014   59  

Page 60: Combining ACS Flexibility with SPM Stability

Evolving  a  Plan  •  Evolving  a  Plan  means  “accep-ng”  it  – Promo-ng  it  from  Plan  History  into  SQL  Plan  Baseline  

•  dbms_spm.evolve_sql_plan_baseline  – sql_handle  (opt)  – plan_name  (opt)  – verify  (default  YES)  – commit  (default  YES)  

Enkitec  (c)  2014   60  

Page 61: Combining ACS Flexibility with SPM Stability

Demo  9  •  Drop  SQL  Plan  Baseline  – drop_plans.sql  

•  Capture  SQL  Plan  Baseline  with  1  accepted  Plan  – demo9.sql  –  list_plans.sql  

•  Evolve  all  Plans  on  Plan  History  – evolve.sql  

Enkitec  (c)  2014   61  

Page 62: Combining ACS Flexibility with SPM Stability

FIXED  Flag  •  When  set  to  YES  – Only  FIXED  Plans  are  considered  for  Plan  Selec-on  – No  more  Plans  are  Captured  into  Plan  History  

Enkitec  (c)  2014   62  

Page 63: Combining ACS Flexibility with SPM Stability

Plan  Selec-on  (1)  •  At  hard  parse  when  SQL  Plan  Baseline  (SPB)  exists  

1.  CBO  computes  an  “Op-mal”  Plan  (OP)  •  Adap-ve  Cursor  Sharing  is  used  if  applicable  

2.  If  OP  exists  in  SPB  then  execute  this  OP  3.  If  OP  does  not  exist  in  SPB  then  store  it  in  Plan  History  

a.  If  there  are  FIXED  Plans  in  SPB  i.  Re-­‐cost  all  FIXED  Plans  using  Binds  and  execute  cheapest  

b.  If  there  are  no  FIXED  Plans  in  SPM  i.  Re-­‐cost  all  Plans  using  Binds  and  execute  cheapest  

Enkitec  (c)  2014   63  

Page 64: Combining ACS Flexibility with SPM Stability

Plan  Selec-on  (2)  

Enkitec  (c)  2014   64  

Page 65: Combining ACS Flexibility with SPM Stability

Demo  10  •  FIX  one  Plan  from  SQL  Plan  Baseline  – alter_plans.sql  

•  Verify  only  one  Plan  was  Executed  – flush.sql  – demo8.sql  – demo8.sql  

Enkitec  (c)  2014   65  

Page 66: Combining ACS Flexibility with SPM Stability

SPM  Summary  

Enkitec  (c)  2014   66  

•  SPM  provides  much  desired  Plan  Stability  – Only  “approved”  Plans  are  allowed  to  Execute  

•  ACS  can  generate  a  healthy  stock  of  Op-mal  Plans  – You  can  restrict  them  to  work  under  SPM  

•  Combining  ACS  and  SPM  you  can  obtain  – Plan  Flexibility  and  Plan  Stability  

Page 67: Combining ACS Flexibility with SPM Stability

SPM/ACS  Suggested  Strategy  •  Use  dbms_applica-on_info.set_module  in  your  code  to  set  MODULE  and  ACTION  

•  Use  dbms_spm.load_plans_from_cursor_cache  filtering  with  MODULE  or  ACTION  

•  Use  sys.dbms_sqldiag_internal.i_create_patch  to  SQL  Patch  with  BIND_AWARE  the  SQL  on  Baselines    

•  Use  dbms_spm.evolve_sql_plan_baseline  to  verify  and  evolve  periodically  good  performing  Plans  

Enkitec  (c)  2014   67  

Page 68: Combining ACS Flexibility with SPM Stability

Oracle  Pack  Licenses  •  DBMS_SPM  – No  Oracle  Pack  License  

•  DBMS_SQLTUNE  – Oracle  Tuning  Pack  

•  AWR  – Oracle  Diagnos-cs  Pack  

Enkitec  (c)  2014   68  

Page 69: Combining ACS Flexibility with SPM Stability

References  (1)  •  Random  Slowdown  by  Jonathan  Lewis  – h{p://jonathanlewis.wordpress.com/2013/12/23/plan-­‐changes/  

•  Using  SQL  Patch  to  add  hints  to  a  packaged  applica-on  – h{ps://blogs.oracle.com/op-mizer/entry/how_can_i_hint_a  

Enkitec  (c)  2014   69  

Page 70: Combining ACS Flexibility with SPM Stability

References  (2)  •  SQL  Plan  Management  SPM  Aware  Op-mizer  – h{ps://blogs.oracle.com/op-mizer/entry/sql_plan_management_part_2_of_4_spm_aware_op-mizer  

•  SQLTXPLAIN  MOS  215187.1  – sqlt/utl/spm  

Enkitec  (c)  2014   70  

Page 71: Combining ACS Flexibility with SPM Stability

References  (3)  •  Oracle®  Database  PL/SQL  Packages  and  Types  Reference  – 11g  Release  2  (11.2)  – Part  Number  E25788-­‐04  

Enkitec  (c)  2014   71  

Page 72: Combining ACS Flexibility with SPM Stability

Contact  Informa-on  •  [email protected]  •  carlos-­‐sierra.net  •  @csierra_usa  

Enkitec  (c)  2014   72