SlideShare ist ein Scribd-Unternehmen logo
1 von 82
Understanding	
  Op.mizer	
  
 Sta.s.cs	
  for	
  Developers
                             	
  
May	
  the	
  Odds	
  Be	
  Ever	
  in	
  Your	
  Favor	
  
©2013 Enkitec   2
www.enkitec.com	
  

                www.facebook.com/enkitec	
  


                @enkitec	
  




©2013 Enkitec                                  3
karen.morton@enkitec.com	
  

                karenmorton.blogspot.com	
  


                @karen_morton	
  




©2013 Enkitec                                  4
Statistics?	
  
    Odds?	
  
     It's just math.	
  
SQL>desc tributes
                 Name        Type
                 ----------- -------------
                 PANEM_ID    NUMBER
                 NAME        VARCHAR2(20)
                 DISTRICT    NUMBER
                 GENDER      VARCHAR2(1)
                 AGE         NUMBER
                 QUAL_SCORE NUMBER
                 WEAPON      VARCHAR2(60)
©2013 Enkitec                                6
©2013 Enkitec   7
Cardinality	
  

                 The	
  es.mated	
  number	
  of	
  rows	
  
                  a	
  query	
  is	
  expected	
  to	
  return.	
  


                number of rows in table
                           x
                 predicate selectivity
©2013 Enkitec                                                         8
select *
    from tributes
   order by district, age;




                Cardinality
                24 x 1 = 24

©2013 Enkitec                 9
©2013 Enkitec   10
select *
    from tributes
   where gender = 'F' ;




                 Cardinality
                24 x 1/2 = 12

©2013 Enkitec                   11
©2013 Enkitec   12
select          *
    from          tributes
   where          district = 12
     and          gender = 'F' ;



                    Cardinality
                24 x 1/12 x 1/2 = 1

©2013 Enkitec                         13
©2013 Enkitec   14
select *
    from tributes
   where age > 15 ;




                     Cardinality
                      (17 – 15) High	
  Value	
  -­‐	
  Predicate	
  Value	
  
                24 x
                      (17 – 12) High	
  Value	
  -­‐	
  Low	
  Value	
  
©2013 Enkitec           = 9.6                                                    15
©2013 Enkitec   16
select *
    from tributes
   where weapon = :b1 ;




                  Cardinality
                24 x 1/11 = 2.18

©2013 Enkitec                      17
:b1 = 'Bow and Arrow'




©2013 Enkitec                    18
No tricks
 	
  just	
  
              math
But…	
  
Sta.s.cs	
  that	
  don't	
  reasonably	
  
     describe	
  your	
  data	
  
…lead	
  to	
  poor	
  	
  
cardinality	
  es.mates	
  
…which	
  leads	
  to	
  poor	
  	
  
access	
  path	
  selec.on	
  
…which	
  leads	
  to	
  poor	
  	
  
join	
  method	
  selec.on	
  
…which	
  leads	
  to	
  poor	
  	
  
 join	
  order	
  selec.on	
  
…which	
  leads	
  to	
  poor	
  	
  
SQL	
  execu.on	
  .mes.	
  
Sta.s.cs	
  maTer!	
  
Goal	
  
                  	
  
 Collect	
  sta.s.cs	
  that	
  are	
  
  "good	
  enough"	
  to	
  meet	
  
most	
  needs	
  most	
  of	
  the	
  .me.	
  	
  
Say	
  you	
  were	
  standing	
  with	
  one	
  
    foot	
  in	
  the	
  oven	
  and	
  one	
  foot	
  in	
  
     an	
  ice	
  bucket.	
  According	
  to	
  the	
  
     percentage	
  people,	
  you	
  would	
  
         be	
  perfectly	
  comfortable.	
  
	
   	
   	
   	
   	
   	
  –	
  Bobby	
  Bragan	
  	
  
Data	
  Skew	
  
The	
  op.mizer	
  assumes	
  
 uniform	
  distribu.on	
  
  of	
  column	
  values.	
  
Color	
  column	
  -­‐	
  uniform	
  distribu.on	
  
Color	
  column	
  –	
  skewed	
  distribu.on	
  
Data	
  skew	
  must	
  be	
  
  iden.fied	
  with	
  
   a	
  histogram.	
  
Table:    obj_tab
                                                  100%	
  Sta.s.cs	
  
Statistic           Current value          FOR	
  ALL	
  COLUMNS	
  SIZE	
  1	
  
---------------     --------------
# rows              1601874
Blocks              22321
Avg Row Len         94
Sample Size         1601874
Monitoring          YES

Column:   object_type

OBJECT_TYPE                          PCT_TOTAL
-------------------------------      ---------
WINDOW GROUP - PROGRAM                 .00-.02
EVALUATION CONTEXT - XML SCHEMA        .03-.05
OPERATOR - PROCEDURE                   .11-.17
LIBRARY - TYPE BODY                    .30-.35
FUNCTION - INDEX PARTITION             .54-.64
JAVA RESOURCE - PACKAGE              1.54-1.69
TABLE - VIEW                         3.44-7.35
JAVA CLASS                               32.80
SYNONYM                                  40.01
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 16yy3p8sstr28, child number 0
-------------------------------------
select /*+ gather_plan_statistics */ owner, object_name, object_type, object_id,
status from obj_tab where object_type = 'PROCEDURE'

Plan hash value: 2862749165
--------------------------------------------------------------------------------
| Id | Operation                    | Name         | E-Rows | A-Rows | Buffers |
--------------------------------------------------------------------------------
|   1 | TABLE ACCESS BY INDEX ROWID| OBJ_TAB       | 44497 |    2720 |    1237 |
|* 2 |    INDEX RANGE SCAN          | OBJ_TYPE_IDX | 44497 |    2720 |     193 |
--------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("OBJECT_TYPE"='PROCEDURE')




                                                             R	
  =	
  .06	
  seconds	
  
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 9u6ppkh5mhr8v, child number 0
-------------------------------------
select /*+ gather_plan_statistics */ owner, object_name, object_type, object_id,
status from obj_tab where object_type = 'SYNONYM'

Plan hash value: 2862749165

--------------------------------------------------------------------------------
| Id | Operation                    | Name         | E-Rows | A-Rows | Buffers |
--------------------------------------------------------------------------------
|   1 | TABLE ACCESS BY INDEX ROWID| OBJ_TAB       | 44497 |     640K|     104K|
|* 2 |    INDEX RANGE SCAN          | OBJ_TYPE_IDX | 44497 |     640K|   44082 |
--------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

  2 - access("OBJECT_TYPE"='SYNONYM')




                                                          R	
  =	
  14.25	
  seconds	
  
100%	
  Sta.s.cs	
  
                                                FOR	
  ALL	
  COLUMNS	
  SIZE	
  AUTO	
  
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 16yy3p8sstr28, child number 0
-------------------------------------
select /*+ gather_plan_statistics */ owner, object_name, object_type, object_id,
status from obj_tab where object_type = 'PROCEDURE'

Plan hash value: 2862749165
--------------------------------------------------------------------------------
| Id | Operation                    | Name         | E-Rows | A-Rows | Buffers |
--------------------------------------------------------------------------------
|   1 | TABLE ACCESS BY INDEX ROWID| OBJ_TAB       |   2720 |   2720 |    1237 |
|* 2 |    INDEX RANGE SCAN          | OBJ_TYPE_IDX |   2720 |   2720 |     193 |
--------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("OBJECT_TYPE"='PROCEDURE')




                                                              R	
  =	
  .07	
  seconds	
  
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
SQL_ID 9u6ppkh5mhr8v, child number 0
-------------------------------------
select /*+ gather_plan_statistics */ owner, object_name, object_type, object_id,
status from obj_tab where object_type = 'SYNONYM'

Plan hash value: 2748991475

-----------------------------------------------------------------
| Id | Operation          | Name    | E-Rows | A-Rows | Buffers |
-----------------------------------------------------------------
|* 1 | TABLE ACCESS FULL| OBJ_TAB |      640K|    640K|   64263 |
-----------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("OBJECT_TYPE"='SYNONYM')




                                                          R	
  =	
  3.36	
  seconds	
  
Dynamic	
  
Sampling	
  
op.mizer_dynamic_sampling	
  
        parameter	
  
            	
  
    dynamic_sampling	
  
           hint	
  
SQL>create table ds_test as
  2 select mod(num, 100) c1,
  3          mod(num, 100) c2,
  4          mod(num, 75) c3,
  5          mod(num, 30) c4
  6    from (select level num from dual
  7          connect by level <= 10001);
 
Table created.

SQL>exec dbms_stats.gather_table_stats( user, 'ds_test',
estimate_percent => null, method_opt => 'for all columns size
1');
 
PL/SQL procedure successfully completed.
Statistic          Current value
---------------    --------------
# rows             10001
Blocks             28
Avg Row Len        11
Sample Size        10001
Monitoring         YES

Column    NDV   Density AvgLen Histogram LowVal   HighVal
-------   ---   ------- ------ --------- ------   -------
C1        100   .010000      3 NONE (1) 0         99
C2        100   .010000      3 NONE (1) 0         99
C3         75   .013333      3 NONE (1) 0         74
C4         30   .033333      3 NONE (1) 0         29
SQL>set autotrace traceonly explain
SQL>select count(*) from ds_test where c1 = 10;
 
Execution Plan
----------------------------------------------------------
Plan hash value: 3984367388
----------------------------------------------------------------------------------
| Id | Operation           | Name        | Rows | Bytes | Cost (%CPU)| Time      |
----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |             |     1 |     3 |     8   (0)| 00:00:01 |
|   1 | SORT AGGREGATE     |             |     1 |     3 |            |          |
|* 2 |    TABLE ACCESS FULL| DS_TEST     |   100 |   300 |     8   (0)| 00:00:01 |
----------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - filter("C1"=10)
SQL>set autotrace traceonly explain
SQL>select count(*) from ds_test where c1 = 10 and c2 = 10;
 
Execution Plan
----------------------------------------------------------
Plan hash value: 3984367388

----------------------------------------------------------------------------------
| Id | Operation           | Name        | Rows | Bytes | Cost (%CPU)| Time      |
----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |             |     1 |     6 |     8   (0)| 00:00:01 |
|   1 | SORT AGGREGATE     |             |     1 |     6 |            |          |
|* 2 |    TABLE ACCESS FULL| DS_TEST     |     1 |     6 |     8   (0)| 00:00:01 |
----------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

  2 - filter("C1"=10 AND "C2"=10)
SQL>set autotrace traceonly explain
SQL>select /*+ dynamic_sampling (4) */ count(*)
  2 from ds_test where c1 = 10 and c2 = 10;
 
Execution Plan
----------------------------------------------------------
Plan hash value: 3984367388

----------------------------------------------------------------------------------
| Id | Operation           | Name        | Rows | Bytes | Cost (%CPU)| Time      |
----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |             |     1 |     6 |     8   (0)| 00:00:01 |
|   1 | SORT AGGREGATE     |             |     1 |     6 |            |          |
|* 2 |    TABLE ACCESS FULL| DS_TEST     |   100 |   600 |     8   (0)| 00:00:01 |
----------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

  2 - filter("C1"=10 AND "C2"=10)

Note
-----
   - dynamic sampling used for this statement
Extended	
  Sta.s.cs	
  
SQL>   select dbms_stats.create_extended_stats(ownname=>user,
  2    tabname => 'DS_TEST',
  3    extension => '(c1, c2)' ) AS c1_c2_correlation
  4    from dual ;

C1_C2_CORRELATION
-------------------------------------------------------------
SYS_STUF3GLKIOP5F4B0BTTCFTMX0W

SQL> exec dbms_stats.gather_table_stats( user, 'ds_test');

PL/SQL procedure successfully completed.
SQL> set autotrace traceonly explain
SQL> select count(*) from ds_test where c1 = 10 and c2 = 10;

Execution Plan
----------------------------------------------------------
Plan hash value: 3984367388
----------------------------------------------------------------------------------
| Id | Operation           | Name        | Rows | Bytes | Cost (%CPU)| Time      |
----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |             |     1 |     6 |     9   (0)| 00:00:01 |
|   1 | SORT AGGREGATE     |             |     1 |     6 |            |          |
|* 2 |    TABLE ACCESS FULL| DS_TEST     |   100 |   600 |     9   (0)| 00:00:01 |
----------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------
   2 - filter("C1"=10 AND "C2"=10)
Understanding	
  how	
  the	
  
                op.mizer	
  uses	
  sta.s.cs	
  
                 helps	
  you	
  know	
  when	
  
                  "special"	
  sta.s.cs	
  
                          can	
  help.	
  

©2013 Enkitec                                       50
Query	
  Transforma.on	
  




©2013 Enkitec                                51
SQL	
  and	
  the	
  Op.mizer
                                                    	
  
    Parsed	
  SQL	
  
                                                                                iterative

                                              Query transformations
                                Logical	
  
                             op.miza.ons	
  
                                              Access & Join methods
                                              Join order
                               Physical	
  
                                                        stats         Dic.onary	
  
                             op.miza.ons	
  


                             Cost	
  Es.mator	
  



                              Execution Plan



©2013 Enkitec                                                                               52
Query	
  Transforma.ons
                                      	
  
•  Goal	
  is	
  to	
  enhance	
  query	
  performance	
  by	
  
   making	
  more	
  choices	
  available	
  to	
  the	
  op6mizer	
  
•  Creates	
  a	
  seman.cally	
  equivalent	
  statement	
  
   that	
  will	
  produce	
  the	
  same	
  results	
  	
  
•  Remove	
  extraneous	
  condi.ons	
  
•  Add	
  inferred	
  condi.ons	
  


                            Think	
  algebra…	
  
©2013 Enkitec                                                      53
Types	
  of	
  Transforma.ons
                                            	
  
•  Automa.c	
  
       –  Always	
  produce	
  a	
  faster	
  plan	
  
•  Heuris.c-­‐based	
  (pre-­‐10gR1)	
  
       –  Should	
  produce	
  a	
  faster	
  plan	
  most	
  of	
  the	
  .me	
  
•  Cost-­‐based	
  
       –  Does	
  not	
  always	
  produce	
  a	
  faster	
  plan	
  




©2013 Enkitec                                                                        54
So,	
  if	
  the	
  op.mizer	
  will	
  
transform	
  my	
  SQL	
  for	
  me,	
  
   why	
  should	
  I	
  write	
  it	
  
         any	
  differently?	
  
Why	
  You	
  Should	
  Refactor
                                               	
  
•    You	
  know	
  your	
  stuff	
  best	
  (or	
  you	
  should)	
  
•    Always	
  filter	
  early	
  
•    Defines	
  your	
  expecta.ons	
  
•    K.I.S.S.	
  
•    The	
  op.mizer	
  might	
  not	
  be	
  able	
  to	
  




©2013 Enkitec                                                           56
Common	
  Transforma.ons
                                       	
  
•    FPD	
  –	
  filter	
  push-­‐down	
  
•    PM	
  –	
  predicate	
  move-­‐around	
  
•    SU	
  –	
  subquery	
  unnes.ng	
  
•    CVM	
  –	
  complex	
  view	
  merging	
  
•    SPJ	
  –	
  select-­‐project-­‐join	
  (simple	
  view	
  merging)	
  
•    JF	
  –	
  join	
  factoriza.on	
  




©2013 Enkitec             Numerous additional transformations exist – see 10053 trace   57
SPJ	
  –	
  Simple	
  View	
  Merging	
  




                            is transformed into




  Merged automatically as it is deemed “always better” for the optimizer to work with direct joins.   58
Complex	
  View	
  Merging	
  




                     is transformed into




“Complex” due to GROUP BY. CVM can also be done when using DISTINCT or outer join.   59
Filter	
  Push-­‐Down	
  



             is transformed into




Purpose: To push outer query predicates into view to perform earlier filtering.   60
Predicate	
  Move-­‐Around	
  



                     is transformed into




 Purpose: To move inexpensive predicates into view query blocks to perform earlier filtering.   61
      Can generate filter predicates based on transitivity or functional dependencies.
Join	
  Factoriza.on	
  



                                  is transformed into




Combines branches of UNION / UNION ALL that join a common table in order to reduce # of accesses to that table.
                                                                                                                  62
Understanding	
  how	
  the	
  
           op.mizer	
  transforms	
  queries	
  
          helps	
  you	
  write	
  beTer	
  SQL	
  and	
  
           understand	
  execu.on	
  plans.	
  



©2013 Enkitec                                                63
Cardinality	
  Feedback	
  




©2013 Enkitec                                 64
What	
  is	
  Cardinality	
  Feedback?	
  
•  Automa.cally	
  improve	
  plans	
  for	
  repeatedly	
  
   executed	
  queries	
  where	
  the	
  op.mizer	
  does	
  
   not	
  es.mate	
  cardinali.es	
  properly	
  
•  Mises.mates	
  may	
  be	
  due	
  to	
  
       –  Missing	
  or	
  inaccurate	
  sta.s.cs	
  
       –  Complex	
  predicates	
            	
       	
     	
  …and	
  more	
  
•  Es.mates	
  and	
  actuals	
  are	
  compared	
  
•  New	
  plan	
  generated	
  using	
  adjustment	
  factors	
  
•  Es.mates	
  lost	
  if	
  plan	
  ages	
  out	
  of	
  cache	
  	
  
©2013 Enkitec                                                                       65
©2013 Enkitec   66
Original	
  plan	
  




©2013 Enkitec                          67
New	
  plan	
  




©2013 Enkitec                     68
Es.mates	
  Differences
                                             	
  
Plan	
  Opera.on	
                  Object	
                     Rows	
  es.mate	
  
Original	
  Plan	
                                               23835	
  
TABLE	
  ACCESS	
  FULL	
           VS_PRODUCT	
                 67	
  
INDEX	
  FAST	
  FULL	
  SCAN	
     IDX_VS_ORDER_027	
           514K	
  
INDEX	
  FAST	
  FULL	
  SCAN	
     IDX_VS_ORDER_TRACK_017	
     2665K	
  
INDEX	
  FAST	
  FULL	
  SCAN	
     PK_VS_ORDER	
                8673K	
  


New	
  Plan	
                                                    8371	
     ✔
INDEX	
  FULL	
  SCAN	
             IDX_VS_PRODUCT_002	
         84	
  
INDEX	
  SKIP	
  SCAN	
             IDX_VS_ORDER_027	
           941K	
  
INDEX	
  SKIP	
  SCAN	
             IDX_VS_ORDER_TRACK_017	
     2899K	
  
INDEX	
  FAST	
  FULL	
  SCAN	
     PK_VS_ORDER	
                10M	
  

©2013 Enkitec                                                                          69
Cardinality	
  feedback	
  is	
  an	
  automated	
  
     way	
  the	
  op.mizer	
  tries	
  to	
  
  "self-­‐correct"	
  and	
  produce	
  plans	
  
       that	
  perform	
  op.mally.	
  



©2013 Enkitec                                   70
What's	
  ahead  	
  
                  in	
  12c?	
  


©2013 Enkitec                           71
Dynamic	
  Sta.s.cs
                                    	
  
•  Replaces	
  dynamic	
  sampling	
  
•  Compensates	
  for	
  missing	
  or	
  incomplete	
  stats	
  
•  Time	
  to	
  do	
  collec.ons	
  is	
  limited	
  by	
  op.mizer	
  
   based	
  on	
  es.mated	
  query	
  run	
  .me	
  
•  When	
  collected,	
  stats	
  will	
  be	
  stored	
  in	
  cache	
  
   and	
  therefore	
  can	
  be	
  shared	
  among	
  queries	
  
•  Different	
  queries	
  using	
  same	
  predicates	
  can	
  
   used	
  dynamic	
  stats	
  stored	
  in	
  cache	
  
©2013 Enkitec                                                               72
Adap.ve	
  Sta.s.cs
                                   	
  
•  Was	
  cardinality	
  feedback	
  in	
  11gR2	
  
•  Es.mates	
  compared	
  to	
  actuals	
  
•  Significant	
  varia.ons	
  cause	
  new	
  plan	
  choice	
  
•  New	
  plan	
  uses	
  stats	
  from	
  previous	
  execu.ons	
  
•  Previous	
  stats	
  retained	
  for	
  later	
  use	
  as	
  SQL	
  
   Plan	
  Direc.ves	
  
•  Statements	
  can	
  be	
  re-­‐op.mized	
  over	
  and	
  over	
  
       –  New	
  column	
  V$SQL.IS_REOPTIMIZABLE
©2013 Enkitec                                                         73
Adap.ve	
  Execu.on	
  Plans
                                           	
  
•  More	
  than	
  1	
  plan	
  pre-­‐computed	
  and	
  stored	
  
•  Sta.s.cs	
  collectors	
  used	
  to	
  capture	
  execu.on	
  
   info	
  
•  If	
  NL	
  chosen	
  and	
  exceeds	
  certain	
  threshold,	
  
   will	
  switch	
  to	
  HJ	
  
•  DBMS_XPLAN.DISPLAY_CURSOR	
  has	
  new	
  
   format	
  parameter	
  to	
  show	
  ac.ons	
  	
  
                format=>'+all_dyn_plan +adaptive'


©2013 Enkitec                                                      74
Conclusion	
  




                 75
Sta.s.cs	
  must	
  reasonably	
  
represent	
  your	
  actual	
  data.	
  




                                       76
Understanding	
  basic	
  
 op.mizer	
  sta.s.cs	
  
computa.ons	
  is	
  key.	
  



                                77
Write	
  SQL	
  to	
  
u.lize	
  your	
  knowledge	
  
 of	
  how	
  sta.s.cs	
  are	
  
used	
  by	
  the	
  op.mizer.	
  

                                     78
The	
  more	
  you	
  know,	
  
    the	
  more	
  likely	
  your	
  	
  
        sta.s.cs	
  strategy	
  
 and	
  the	
  way	
  you	
  write	
  SQL	
  
   will	
  support	
  op.mally	
  
performing	
  execu.on	
  plans.	
  
                                            79
Thank	
  You!	
  
Q 	

&	

Q U E S T I O N S
 A N S W E R S

Weitere ähnliche Inhalte

Was ist angesagt?

Riyaj: why optimizer_hates_my_sql_2010
Riyaj: why optimizer_hates_my_sql_2010Riyaj: why optimizer_hates_my_sql_2010
Riyaj: why optimizer_hates_my_sql_2010Riyaj Shamsudeen
 
SQL Plan Directives explained
SQL Plan Directives explainedSQL Plan Directives explained
SQL Plan Directives explainedMauro Pagano
 
Adapting to Adaptive Plans on 12c
Adapting to Adaptive Plans on 12cAdapting to Adaptive Plans on 12c
Adapting to Adaptive Plans on 12cMauro Pagano
 
Sangam 19 - PLSQL still the coolest
Sangam 19 - PLSQL still the coolestSangam 19 - PLSQL still the coolest
Sangam 19 - PLSQL still the coolestConnor McDonald
 
Mysqlconf2013 mariadb-cassandra-interoperability
Mysqlconf2013 mariadb-cassandra-interoperabilityMysqlconf2013 mariadb-cassandra-interoperability
Mysqlconf2013 mariadb-cassandra-interoperabilitySergey Petrunya
 
Connor McDonald 11g for developers
Connor McDonald 11g for developersConnor McDonald 11g for developers
Connor McDonald 11g for developersInSync Conference
 
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should KnowOTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should KnowAlex Zaballa
 
More Than po: Debugging in LLDB @ CocoaConf SJ 2015
More Than po: Debugging in LLDB @ CocoaConf SJ 2015More Than po: Debugging in LLDB @ CocoaConf SJ 2015
More Than po: Debugging in LLDB @ CocoaConf SJ 2015Michele Titolo
 
UKOUG - 25 years of hints and tips
UKOUG - 25 years of hints and tipsUKOUG - 25 years of hints and tips
UKOUG - 25 years of hints and tipsConnor McDonald
 
Demystifying cost based optimization
Demystifying cost based optimizationDemystifying cost based optimization
Demystifying cost based optimizationRiyaj Shamsudeen
 
Robert Bernier - Recovering From A Damaged PostgreSQL Cluster @ Postgres Open
Robert Bernier - Recovering From A Damaged PostgreSQL Cluster @ Postgres OpenRobert Bernier - Recovering From A Damaged PostgreSQL Cluster @ Postgres Open
Robert Bernier - Recovering From A Damaged PostgreSQL Cluster @ Postgres OpenPostgresOpen
 
Optimizer in oracle 11g by wwf from ebay COC
Optimizer in oracle 11g by wwf from ebay COCOptimizer in oracle 11g by wwf from ebay COC
Optimizer in oracle 11g by wwf from ebay COCLouis liu
 
Relational DB Course
Relational DB  Course Relational DB  Course
Relational DB Course Sunny U Okoro
 
12c Mini Lesson - Inline PLSQL from SQL
12c Mini Lesson - Inline PLSQL from SQL12c Mini Lesson - Inline PLSQL from SQL
12c Mini Lesson - Inline PLSQL from SQLConnor McDonald
 
MySQL Idiosyncrasies That Bite
MySQL Idiosyncrasies That BiteMySQL Idiosyncrasies That Bite
MySQL Idiosyncrasies That BiteRonald Bradford
 
Dbms plan - A swiss army knife for performance engineers
Dbms plan - A swiss army knife for performance engineersDbms plan - A swiss army knife for performance engineers
Dbms plan - A swiss army knife for performance engineersRiyaj Shamsudeen
 

Was ist angesagt? (20)

Riyaj: why optimizer_hates_my_sql_2010
Riyaj: why optimizer_hates_my_sql_2010Riyaj: why optimizer_hates_my_sql_2010
Riyaj: why optimizer_hates_my_sql_2010
 
Sql queries
Sql queriesSql queries
Sql queries
 
Do You Know The 11g Plan?
Do You Know The 11g Plan?Do You Know The 11g Plan?
Do You Know The 11g Plan?
 
SQL Plan Directives explained
SQL Plan Directives explainedSQL Plan Directives explained
SQL Plan Directives explained
 
Adapting to Adaptive Plans on 12c
Adapting to Adaptive Plans on 12cAdapting to Adaptive Plans on 12c
Adapting to Adaptive Plans on 12c
 
Sangam 19 - PLSQL still the coolest
Sangam 19 - PLSQL still the coolestSangam 19 - PLSQL still the coolest
Sangam 19 - PLSQL still the coolest
 
Mysqlconf2013 mariadb-cassandra-interoperability
Mysqlconf2013 mariadb-cassandra-interoperabilityMysqlconf2013 mariadb-cassandra-interoperability
Mysqlconf2013 mariadb-cassandra-interoperability
 
Connor McDonald 11g for developers
Connor McDonald 11g for developersConnor McDonald 11g for developers
Connor McDonald 11g for developers
 
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should KnowOTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
OTN TOUR 2016 - DBA Commands and Concepts That Every Developer Should Know
 
More Than po: Debugging in LLDB @ CocoaConf SJ 2015
More Than po: Debugging in LLDB @ CocoaConf SJ 2015More Than po: Debugging in LLDB @ CocoaConf SJ 2015
More Than po: Debugging in LLDB @ CocoaConf SJ 2015
 
UKOUG - 25 years of hints and tips
UKOUG - 25 years of hints and tipsUKOUG - 25 years of hints and tips
UKOUG - 25 years of hints and tips
 
Demystifying cost based optimization
Demystifying cost based optimizationDemystifying cost based optimization
Demystifying cost based optimization
 
Robert Bernier - Recovering From A Damaged PostgreSQL Cluster @ Postgres Open
Robert Bernier - Recovering From A Damaged PostgreSQL Cluster @ Postgres OpenRobert Bernier - Recovering From A Damaged PostgreSQL Cluster @ Postgres Open
Robert Bernier - Recovering From A Damaged PostgreSQL Cluster @ Postgres Open
 
Optimizer in oracle 11g by wwf from ebay COC
Optimizer in oracle 11g by wwf from ebay COCOptimizer in oracle 11g by wwf from ebay COC
Optimizer in oracle 11g by wwf from ebay COC
 
Relational DB Course
Relational DB  Course Relational DB  Course
Relational DB Course
 
Optimizer Statistics
Optimizer StatisticsOptimizer Statistics
Optimizer Statistics
 
12c Mini Lesson - Inline PLSQL from SQL
12c Mini Lesson - Inline PLSQL from SQL12c Mini Lesson - Inline PLSQL from SQL
12c Mini Lesson - Inline PLSQL from SQL
 
MySQL Idiosyncrasies That Bite
MySQL Idiosyncrasies That BiteMySQL Idiosyncrasies That Bite
MySQL Idiosyncrasies That Bite
 
Analytic SQL Sep 2013
Analytic SQL Sep 2013Analytic SQL Sep 2013
Analytic SQL Sep 2013
 
Dbms plan - A swiss army knife for performance engineers
Dbms plan - A swiss army knife for performance engineersDbms plan - A swiss army knife for performance engineers
Dbms plan - A swiss army knife for performance engineers
 

Andere mochten auch

In Memory Database In Action by Tanel Poder and Kerry Osborne
In Memory Database In Action by Tanel Poder and Kerry OsborneIn Memory Database In Action by Tanel Poder and Kerry Osborne
In Memory Database In Action by Tanel Poder and Kerry OsborneEnkitec
 
Think Exa!
Think Exa!Think Exa!
Think Exa!Enkitec
 
Mini Session - Using GDB for Profiling
Mini Session - Using GDB for ProfilingMini Session - Using GDB for Profiling
Mini Session - Using GDB for ProfilingEnkitec
 
In Search of Plan Stability - Part 1
In Search of Plan Stability - Part 1In Search of Plan Stability - Part 1
In Search of Plan Stability - Part 1Enkitec
 
Controlling execution plans 2014
Controlling execution plans   2014Controlling execution plans   2014
Controlling execution plans 2014Enkitec
 
Using Angular JS in APEX
Using Angular JS in APEXUsing Angular JS in APEX
Using Angular JS in APEXEnkitec
 
Enterprise class apex
Enterprise class apexEnterprise class apex
Enterprise class apexEnkitec
 
Engineered Systems: Environment-as-a-Service Demonstration
Engineered Systems: Environment-as-a-Service DemonstrationEngineered Systems: Environment-as-a-Service Demonstration
Engineered Systems: Environment-as-a-Service DemonstrationEnkitec
 

Andere mochten auch (8)

In Memory Database In Action by Tanel Poder and Kerry Osborne
In Memory Database In Action by Tanel Poder and Kerry OsborneIn Memory Database In Action by Tanel Poder and Kerry Osborne
In Memory Database In Action by Tanel Poder and Kerry Osborne
 
Think Exa!
Think Exa!Think Exa!
Think Exa!
 
Mini Session - Using GDB for Profiling
Mini Session - Using GDB for ProfilingMini Session - Using GDB for Profiling
Mini Session - Using GDB for Profiling
 
In Search of Plan Stability - Part 1
In Search of Plan Stability - Part 1In Search of Plan Stability - Part 1
In Search of Plan Stability - Part 1
 
Controlling execution plans 2014
Controlling execution plans   2014Controlling execution plans   2014
Controlling execution plans 2014
 
Using Angular JS in APEX
Using Angular JS in APEXUsing Angular JS in APEX
Using Angular JS in APEX
 
Enterprise class apex
Enterprise class apexEnterprise class apex
Enterprise class apex
 
Engineered Systems: Environment-as-a-Service Demonstration
Engineered Systems: Environment-as-a-Service DemonstrationEngineered Systems: Environment-as-a-Service Demonstration
Engineered Systems: Environment-as-a-Service Demonstration
 

Ähnlich wie Understanding Optimizer-Statistics-for-Developers

New Tuning Features in Oracle 11g - How to make your database as boring as po...
New Tuning Features in Oracle 11g - How to make your database as boring as po...New Tuning Features in Oracle 11g - How to make your database as boring as po...
New Tuning Features in Oracle 11g - How to make your database as boring as po...Sage Computing Services
 
12c SQL Plan Directives
12c SQL Plan Directives12c SQL Plan Directives
12c SQL Plan DirectivesFranck Pachot
 
A few things about the Oracle optimizer - 2013
A few things about the Oracle optimizer - 2013A few things about the Oracle optimizer - 2013
A few things about the Oracle optimizer - 2013Connor McDonald
 
SQL Tuning 101 - Sep 2013
SQL Tuning 101 - Sep 2013SQL Tuning 101 - Sep 2013
SQL Tuning 101 - Sep 2013Connor McDonald
 
SQLチューニング総合診療Oracle CloudWorld出張所
SQLチューニング総合診療Oracle CloudWorld出張所SQLチューニング総合診療Oracle CloudWorld出張所
SQLチューニング総合診療Oracle CloudWorld出張所Hiroshi Sekiguchi
 
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ OracleUnderstanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ OracleGuatemala User Group
 
Oracle Database 12c Application Development
Oracle Database 12c Application DevelopmentOracle Database 12c Application Development
Oracle Database 12c Application DevelopmentSaurabh K. Gupta
 
CBO Basics: Cardinality
CBO Basics: CardinalityCBO Basics: Cardinality
CBO Basics: CardinalitySidney Chen
 
Advanced tips of dbms statas
Advanced tips of dbms statasAdvanced tips of dbms statas
Advanced tips of dbms statasLouis liu
 
Informix Warehouse Accelerator (IWA) features in version 12.1
Informix Warehouse Accelerator (IWA) features in version 12.1Informix Warehouse Accelerator (IWA) features in version 12.1
Informix Warehouse Accelerator (IWA) features in version 12.1Keshav Murthy
 
EvolveExecutionPlans.pdf
EvolveExecutionPlans.pdfEvolveExecutionPlans.pdf
EvolveExecutionPlans.pdfPraveenPolu1
 
Writing efficient sql
Writing efficient sqlWriting efficient sql
Writing efficient sqlj9soto
 
All on Adaptive and Extended Cursor Sharing
All on Adaptive and Extended Cursor SharingAll on Adaptive and Extended Cursor Sharing
All on Adaptive and Extended Cursor SharingMohamed Houri
 
Adaptive Query Optimization
Adaptive Query OptimizationAdaptive Query Optimization
Adaptive Query OptimizationAnju Garg
 
SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?Andrej Pashchenko
 
zen and the art of SQL optimization
zen and the art of SQL optimizationzen and the art of SQL optimization
zen and the art of SQL optimizationKaren Morton
 

Ähnlich wie Understanding Optimizer-Statistics-for-Developers (20)

New Tuning Features in Oracle 11g - How to make your database as boring as po...
New Tuning Features in Oracle 11g - How to make your database as boring as po...New Tuning Features in Oracle 11g - How to make your database as boring as po...
New Tuning Features in Oracle 11g - How to make your database as boring as po...
 
12c SQL Plan Directives
12c SQL Plan Directives12c SQL Plan Directives
12c SQL Plan Directives
 
A few things about the Oracle optimizer - 2013
A few things about the Oracle optimizer - 2013A few things about the Oracle optimizer - 2013
A few things about the Oracle optimizer - 2013
 
Sql2
Sql2Sql2
Sql2
 
SQL Tuning 101 - Sep 2013
SQL Tuning 101 - Sep 2013SQL Tuning 101 - Sep 2013
SQL Tuning 101 - Sep 2013
 
SQLチューニング総合診療Oracle CloudWorld出張所
SQLチューニング総合診療Oracle CloudWorld出張所SQLチューニング総合診療Oracle CloudWorld出張所
SQLチューニング総合診療Oracle CloudWorld出張所
 
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ OracleUnderstanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
Understanding Query Optimization with ‘regular’ and ‘Exadata’ Oracle
 
Oracle Database 12c Application Development
Oracle Database 12c Application DevelopmentOracle Database 12c Application Development
Oracle Database 12c Application Development
 
CBO Basics: Cardinality
CBO Basics: CardinalityCBO Basics: Cardinality
CBO Basics: Cardinality
 
Advanced tips of dbms statas
Advanced tips of dbms statasAdvanced tips of dbms statas
Advanced tips of dbms statas
 
The Cost Based Optimiser in 11gR2
The Cost Based Optimiser in 11gR2The Cost Based Optimiser in 11gR2
The Cost Based Optimiser in 11gR2
 
Informix Warehouse Accelerator (IWA) features in version 12.1
Informix Warehouse Accelerator (IWA) features in version 12.1Informix Warehouse Accelerator (IWA) features in version 12.1
Informix Warehouse Accelerator (IWA) features in version 12.1
 
EvolveExecutionPlans.pdf
EvolveExecutionPlans.pdfEvolveExecutionPlans.pdf
EvolveExecutionPlans.pdf
 
5 Cool Things About SQL
5 Cool Things About SQL5 Cool Things About SQL
5 Cool Things About SQL
 
Writing efficient sql
Writing efficient sqlWriting efficient sql
Writing efficient sql
 
All on Adaptive and Extended Cursor Sharing
All on Adaptive and Extended Cursor SharingAll on Adaptive and Extended Cursor Sharing
All on Adaptive and Extended Cursor Sharing
 
Adaptive Query Optimization
Adaptive Query OptimizationAdaptive Query Optimization
Adaptive Query Optimization
 
SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?
 
Experiment 16 x2 parallel lcd
Experiment   16 x2 parallel lcdExperiment   16 x2 parallel lcd
Experiment 16 x2 parallel lcd
 
zen and the art of SQL optimization
zen and the art of SQL optimizationzen and the art of SQL optimization
zen and the art of SQL optimization
 

Mehr von Enkitec

Profiling Oracle with GDB
Profiling Oracle with GDBProfiling Oracle with GDB
Profiling Oracle with GDBEnkitec
 
Oracle Performance Tools of the Trade
Oracle Performance Tools of the TradeOracle Performance Tools of the Trade
Oracle Performance Tools of the TradeEnkitec
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsOracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsEnkitec
 
SQL Tuning Tools of the Trade
SQL Tuning Tools of the TradeSQL Tuning Tools of the Trade
SQL Tuning Tools of the TradeEnkitec
 
Using SQL Plan Management (SPM) to Balance Plan Flexibility and Plan Stability
Using SQL Plan Management (SPM) to Balance Plan Flexibility and Plan StabilityUsing SQL Plan Management (SPM) to Balance Plan Flexibility and Plan Stability
Using SQL Plan Management (SPM) to Balance Plan Flexibility and Plan StabilityEnkitec
 
Oracle GoldenGate Architecture Performance
Oracle GoldenGate Architecture PerformanceOracle GoldenGate Architecture Performance
Oracle GoldenGate Architecture PerformanceEnkitec
 
OGG Architecture Performance
OGG Architecture PerformanceOGG Architecture Performance
OGG Architecture PerformanceEnkitec
 
APEX Security Primer
APEX Security PrimerAPEX Security Primer
APEX Security PrimerEnkitec
 
How Many Ways Can I Manage Oracle GoldenGate?
How Many Ways Can I Manage Oracle GoldenGate?How Many Ways Can I Manage Oracle GoldenGate?
How Many Ways Can I Manage Oracle GoldenGate?Enkitec
 
Understanding how is that adaptive cursor sharing (acs) produces multiple opt...
Understanding how is that adaptive cursor sharing (acs) produces multiple opt...Understanding how is that adaptive cursor sharing (acs) produces multiple opt...
Understanding how is that adaptive cursor sharing (acs) produces multiple opt...Enkitec
 
Sql tuning made easier with sqltxplain (sqlt)
Sql tuning made easier with sqltxplain (sqlt)Sql tuning made easier with sqltxplain (sqlt)
Sql tuning made easier with sqltxplain (sqlt)Enkitec
 
Profiling the logwriter and database writer
Profiling the logwriter and database writerProfiling the logwriter and database writer
Profiling the logwriter and database writerEnkitec
 
Fatkulin hotsos 2014
Fatkulin hotsos 2014Fatkulin hotsos 2014
Fatkulin hotsos 2014Enkitec
 
Combining ACS Flexibility with SPM Stability
Combining ACS Flexibility with SPM StabilityCombining ACS Flexibility with SPM Stability
Combining ACS Flexibility with SPM StabilityEnkitec
 
Why You May Not Need Offloading
Why You May Not Need OffloadingWhy You May Not Need Offloading
Why You May Not Need OffloadingEnkitec
 
LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX
LOBS, BLOBS, CLOBS: Dealing with Attachments in APEXLOBS, BLOBS, CLOBS: Dealing with Attachments in APEX
LOBS, BLOBS, CLOBS: Dealing with Attachments in APEXEnkitec
 
Creating a Business Oriented UI in APEX
Creating a Business Oriented UI in APEXCreating a Business Oriented UI in APEX
Creating a Business Oriented UI in APEXEnkitec
 
Colvin RMAN New Features
Colvin RMAN New FeaturesColvin RMAN New Features
Colvin RMAN New FeaturesEnkitec
 
Enkitec Exadata Human Factor
Enkitec Exadata Human FactorEnkitec Exadata Human Factor
Enkitec Exadata Human FactorEnkitec
 
About Multiblock Reads v4
About Multiblock Reads v4About Multiblock Reads v4
About Multiblock Reads v4Enkitec
 

Mehr von Enkitec (20)

Profiling Oracle with GDB
Profiling Oracle with GDBProfiling Oracle with GDB
Profiling Oracle with GDB
 
Oracle Performance Tools of the Trade
Oracle Performance Tools of the TradeOracle Performance Tools of the Trade
Oracle Performance Tools of the Trade
 
Oracle Performance Tuning Fundamentals
Oracle Performance Tuning FundamentalsOracle Performance Tuning Fundamentals
Oracle Performance Tuning Fundamentals
 
SQL Tuning Tools of the Trade
SQL Tuning Tools of the TradeSQL Tuning Tools of the Trade
SQL Tuning Tools of the Trade
 
Using SQL Plan Management (SPM) to Balance Plan Flexibility and Plan Stability
Using SQL Plan Management (SPM) to Balance Plan Flexibility and Plan StabilityUsing SQL Plan Management (SPM) to Balance Plan Flexibility and Plan Stability
Using SQL Plan Management (SPM) to Balance Plan Flexibility and Plan Stability
 
Oracle GoldenGate Architecture Performance
Oracle GoldenGate Architecture PerformanceOracle GoldenGate Architecture Performance
Oracle GoldenGate Architecture Performance
 
OGG Architecture Performance
OGG Architecture PerformanceOGG Architecture Performance
OGG Architecture Performance
 
APEX Security Primer
APEX Security PrimerAPEX Security Primer
APEX Security Primer
 
How Many Ways Can I Manage Oracle GoldenGate?
How Many Ways Can I Manage Oracle GoldenGate?How Many Ways Can I Manage Oracle GoldenGate?
How Many Ways Can I Manage Oracle GoldenGate?
 
Understanding how is that adaptive cursor sharing (acs) produces multiple opt...
Understanding how is that adaptive cursor sharing (acs) produces multiple opt...Understanding how is that adaptive cursor sharing (acs) produces multiple opt...
Understanding how is that adaptive cursor sharing (acs) produces multiple opt...
 
Sql tuning made easier with sqltxplain (sqlt)
Sql tuning made easier with sqltxplain (sqlt)Sql tuning made easier with sqltxplain (sqlt)
Sql tuning made easier with sqltxplain (sqlt)
 
Profiling the logwriter and database writer
Profiling the logwriter and database writerProfiling the logwriter and database writer
Profiling the logwriter and database writer
 
Fatkulin hotsos 2014
Fatkulin hotsos 2014Fatkulin hotsos 2014
Fatkulin hotsos 2014
 
Combining ACS Flexibility with SPM Stability
Combining ACS Flexibility with SPM StabilityCombining ACS Flexibility with SPM Stability
Combining ACS Flexibility with SPM Stability
 
Why You May Not Need Offloading
Why You May Not Need OffloadingWhy You May Not Need Offloading
Why You May Not Need Offloading
 
LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX
LOBS, BLOBS, CLOBS: Dealing with Attachments in APEXLOBS, BLOBS, CLOBS: Dealing with Attachments in APEX
LOBS, BLOBS, CLOBS: Dealing with Attachments in APEX
 
Creating a Business Oriented UI in APEX
Creating a Business Oriented UI in APEXCreating a Business Oriented UI in APEX
Creating a Business Oriented UI in APEX
 
Colvin RMAN New Features
Colvin RMAN New FeaturesColvin RMAN New Features
Colvin RMAN New Features
 
Enkitec Exadata Human Factor
Enkitec Exadata Human FactorEnkitec Exadata Human Factor
Enkitec Exadata Human Factor
 
About Multiblock Reads v4
About Multiblock Reads v4About Multiblock Reads v4
About Multiblock Reads v4
 

Understanding Optimizer-Statistics-for-Developers

  • 1. Understanding  Op.mizer   Sta.s.cs  for  Developers   May  the  Odds  Be  Ever  in  Your  Favor  
  • 3. www.enkitec.com   www.facebook.com/enkitec   @enkitec   ©2013 Enkitec 3
  • 4. karen.morton@enkitec.com   karenmorton.blogspot.com   @karen_morton   ©2013 Enkitec 4
  • 5. Statistics?   Odds?   It's just math.  
  • 6. SQL>desc tributes Name Type ----------- ------------- PANEM_ID NUMBER NAME VARCHAR2(20) DISTRICT NUMBER GENDER VARCHAR2(1) AGE NUMBER QUAL_SCORE NUMBER WEAPON VARCHAR2(60) ©2013 Enkitec 6
  • 8. Cardinality   The  es.mated  number  of  rows   a  query  is  expected  to  return.   number of rows in table x predicate selectivity ©2013 Enkitec 8
  • 9. select * from tributes order by district, age; Cardinality 24 x 1 = 24 ©2013 Enkitec 9
  • 11. select * from tributes where gender = 'F' ; Cardinality 24 x 1/2 = 12 ©2013 Enkitec 11
  • 13. select * from tributes where district = 12 and gender = 'F' ; Cardinality 24 x 1/12 x 1/2 = 1 ©2013 Enkitec 13
  • 15. select * from tributes where age > 15 ; Cardinality (17 – 15) High  Value  -­‐  Predicate  Value   24 x (17 – 12) High  Value  -­‐  Low  Value   ©2013 Enkitec = 9.6 15
  • 17. select * from tributes where weapon = :b1 ; Cardinality 24 x 1/11 = 2.18 ©2013 Enkitec 17
  • 18. :b1 = 'Bow and Arrow' ©2013 Enkitec 18
  • 19. No tricks  just   math
  • 21. Sta.s.cs  that  don't  reasonably   describe  your  data  
  • 22. …lead  to  poor     cardinality  es.mates  
  • 23. …which  leads  to  poor     access  path  selec.on  
  • 24. …which  leads  to  poor     join  method  selec.on  
  • 25. …which  leads  to  poor     join  order  selec.on  
  • 26. …which  leads  to  poor     SQL  execu.on  .mes.  
  • 28. Goal     Collect  sta.s.cs  that  are   "good  enough"  to  meet   most  needs  most  of  the  .me.    
  • 29. Say  you  were  standing  with  one   foot  in  the  oven  and  one  foot  in   an  ice  bucket.  According  to  the   percentage  people,  you  would   be  perfectly  comfortable.              –  Bobby  Bragan    
  • 31. The  op.mizer  assumes   uniform  distribu.on   of  column  values.  
  • 32. Color  column  -­‐  uniform  distribu.on  
  • 33. Color  column  –  skewed  distribu.on  
  • 34. Data  skew  must  be   iden.fied  with   a  histogram.  
  • 35. Table: obj_tab 100%  Sta.s.cs   Statistic Current value FOR  ALL  COLUMNS  SIZE  1   --------------- -------------- # rows 1601874 Blocks 22321 Avg Row Len 94 Sample Size 1601874 Monitoring YES Column: object_type OBJECT_TYPE PCT_TOTAL ------------------------------- --------- WINDOW GROUP - PROGRAM .00-.02 EVALUATION CONTEXT - XML SCHEMA .03-.05 OPERATOR - PROCEDURE .11-.17 LIBRARY - TYPE BODY .30-.35 FUNCTION - INDEX PARTITION .54-.64 JAVA RESOURCE - PACKAGE 1.54-1.69 TABLE - VIEW 3.44-7.35 JAVA CLASS 32.80 SYNONYM 40.01
  • 36. PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------- SQL_ID 16yy3p8sstr28, child number 0 ------------------------------------- select /*+ gather_plan_statistics */ owner, object_name, object_type, object_id, status from obj_tab where object_type = 'PROCEDURE' Plan hash value: 2862749165 -------------------------------------------------------------------------------- | Id | Operation | Name | E-Rows | A-Rows | Buffers | -------------------------------------------------------------------------------- | 1 | TABLE ACCESS BY INDEX ROWID| OBJ_TAB | 44497 | 2720 | 1237 | |* 2 | INDEX RANGE SCAN | OBJ_TYPE_IDX | 44497 | 2720 | 193 | -------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("OBJECT_TYPE"='PROCEDURE') R  =  .06  seconds  
  • 37. PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------- SQL_ID 9u6ppkh5mhr8v, child number 0 ------------------------------------- select /*+ gather_plan_statistics */ owner, object_name, object_type, object_id, status from obj_tab where object_type = 'SYNONYM' Plan hash value: 2862749165 -------------------------------------------------------------------------------- | Id | Operation | Name | E-Rows | A-Rows | Buffers | -------------------------------------------------------------------------------- | 1 | TABLE ACCESS BY INDEX ROWID| OBJ_TAB | 44497 | 640K| 104K| |* 2 | INDEX RANGE SCAN | OBJ_TYPE_IDX | 44497 | 640K| 44082 | -------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("OBJECT_TYPE"='SYNONYM') R  =  14.25  seconds  
  • 38. 100%  Sta.s.cs   FOR  ALL  COLUMNS  SIZE  AUTO   PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------- SQL_ID 16yy3p8sstr28, child number 0 ------------------------------------- select /*+ gather_plan_statistics */ owner, object_name, object_type, object_id, status from obj_tab where object_type = 'PROCEDURE' Plan hash value: 2862749165 -------------------------------------------------------------------------------- | Id | Operation | Name | E-Rows | A-Rows | Buffers | -------------------------------------------------------------------------------- | 1 | TABLE ACCESS BY INDEX ROWID| OBJ_TAB | 2720 | 2720 | 1237 | |* 2 | INDEX RANGE SCAN | OBJ_TYPE_IDX | 2720 | 2720 | 193 | -------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("OBJECT_TYPE"='PROCEDURE') R  =  .07  seconds  
  • 39. PLAN_TABLE_OUTPUT -------------------------------------------------------------------------------- SQL_ID 9u6ppkh5mhr8v, child number 0 ------------------------------------- select /*+ gather_plan_statistics */ owner, object_name, object_type, object_id, status from obj_tab where object_type = 'SYNONYM' Plan hash value: 2748991475 ----------------------------------------------------------------- | Id | Operation | Name | E-Rows | A-Rows | Buffers | ----------------------------------------------------------------- |* 1 | TABLE ACCESS FULL| OBJ_TAB | 640K| 640K| 64263 | ----------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("OBJECT_TYPE"='SYNONYM') R  =  3.36  seconds  
  • 41. op.mizer_dynamic_sampling   parameter     dynamic_sampling   hint  
  • 42. SQL>create table ds_test as 2 select mod(num, 100) c1, 3 mod(num, 100) c2, 4 mod(num, 75) c3, 5 mod(num, 30) c4 6 from (select level num from dual 7 connect by level <= 10001);   Table created. SQL>exec dbms_stats.gather_table_stats( user, 'ds_test', estimate_percent => null, method_opt => 'for all columns size 1');   PL/SQL procedure successfully completed.
  • 43. Statistic Current value --------------- -------------- # rows 10001 Blocks 28 Avg Row Len 11 Sample Size 10001 Monitoring YES Column NDV Density AvgLen Histogram LowVal HighVal ------- --- ------- ------ --------- ------ ------- C1 100 .010000 3 NONE (1) 0 99 C2 100 .010000 3 NONE (1) 0 99 C3 75 .013333 3 NONE (1) 0 74 C4 30 .033333 3 NONE (1) 0 29
  • 44. SQL>set autotrace traceonly explain SQL>select count(*) from ds_test where c1 = 10;   Execution Plan ---------------------------------------------------------- Plan hash value: 3984367388 ---------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ---------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 3 | 8 (0)| 00:00:01 | | 1 | SORT AGGREGATE | | 1 | 3 | | | |* 2 | TABLE ACCESS FULL| DS_TEST | 100 | 300 | 8 (0)| 00:00:01 | ----------------------------------------------------------------------------------   Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter("C1"=10)
  • 45. SQL>set autotrace traceonly explain SQL>select count(*) from ds_test where c1 = 10 and c2 = 10;   Execution Plan ---------------------------------------------------------- Plan hash value: 3984367388 ---------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ---------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 6 | 8 (0)| 00:00:01 | | 1 | SORT AGGREGATE | | 1 | 6 | | | |* 2 | TABLE ACCESS FULL| DS_TEST | 1 | 6 | 8 (0)| 00:00:01 | ---------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter("C1"=10 AND "C2"=10)
  • 46. SQL>set autotrace traceonly explain SQL>select /*+ dynamic_sampling (4) */ count(*) 2 from ds_test where c1 = 10 and c2 = 10;   Execution Plan ---------------------------------------------------------- Plan hash value: 3984367388 ---------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ---------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 6 | 8 (0)| 00:00:01 | | 1 | SORT AGGREGATE | | 1 | 6 | | | |* 2 | TABLE ACCESS FULL| DS_TEST | 100 | 600 | 8 (0)| 00:00:01 | ---------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter("C1"=10 AND "C2"=10) Note ----- - dynamic sampling used for this statement
  • 48. SQL> select dbms_stats.create_extended_stats(ownname=>user, 2 tabname => 'DS_TEST', 3 extension => '(c1, c2)' ) AS c1_c2_correlation 4 from dual ; C1_C2_CORRELATION ------------------------------------------------------------- SYS_STUF3GLKIOP5F4B0BTTCFTMX0W SQL> exec dbms_stats.gather_table_stats( user, 'ds_test'); PL/SQL procedure successfully completed.
  • 49. SQL> set autotrace traceonly explain SQL> select count(*) from ds_test where c1 = 10 and c2 = 10; Execution Plan ---------------------------------------------------------- Plan hash value: 3984367388 ---------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ---------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 6 | 9 (0)| 00:00:01 | | 1 | SORT AGGREGATE | | 1 | 6 | | | |* 2 | TABLE ACCESS FULL| DS_TEST | 100 | 600 | 9 (0)| 00:00:01 | ---------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter("C1"=10 AND "C2"=10)
  • 50. Understanding  how  the   op.mizer  uses  sta.s.cs   helps  you  know  when   "special"  sta.s.cs   can  help.   ©2013 Enkitec 50
  • 52. SQL  and  the  Op.mizer   Parsed  SQL   iterative Query transformations Logical   op.miza.ons   Access & Join methods Join order Physical   stats Dic.onary   op.miza.ons   Cost  Es.mator   Execution Plan ©2013 Enkitec 52
  • 53. Query  Transforma.ons   •  Goal  is  to  enhance  query  performance  by   making  more  choices  available  to  the  op6mizer   •  Creates  a  seman.cally  equivalent  statement   that  will  produce  the  same  results     •  Remove  extraneous  condi.ons   •  Add  inferred  condi.ons   Think  algebra…   ©2013 Enkitec 53
  • 54. Types  of  Transforma.ons   •  Automa.c   –  Always  produce  a  faster  plan   •  Heuris.c-­‐based  (pre-­‐10gR1)   –  Should  produce  a  faster  plan  most  of  the  .me   •  Cost-­‐based   –  Does  not  always  produce  a  faster  plan   ©2013 Enkitec 54
  • 55. So,  if  the  op.mizer  will   transform  my  SQL  for  me,   why  should  I  write  it   any  differently?  
  • 56. Why  You  Should  Refactor   •  You  know  your  stuff  best  (or  you  should)   •  Always  filter  early   •  Defines  your  expecta.ons   •  K.I.S.S.   •  The  op.mizer  might  not  be  able  to   ©2013 Enkitec 56
  • 57. Common  Transforma.ons   •  FPD  –  filter  push-­‐down   •  PM  –  predicate  move-­‐around   •  SU  –  subquery  unnes.ng   •  CVM  –  complex  view  merging   •  SPJ  –  select-­‐project-­‐join  (simple  view  merging)   •  JF  –  join  factoriza.on   ©2013 Enkitec Numerous additional transformations exist – see 10053 trace 57
  • 58. SPJ  –  Simple  View  Merging   is transformed into Merged automatically as it is deemed “always better” for the optimizer to work with direct joins. 58
  • 59. Complex  View  Merging   is transformed into “Complex” due to GROUP BY. CVM can also be done when using DISTINCT or outer join. 59
  • 60. Filter  Push-­‐Down   is transformed into Purpose: To push outer query predicates into view to perform earlier filtering. 60
  • 61. Predicate  Move-­‐Around   is transformed into Purpose: To move inexpensive predicates into view query blocks to perform earlier filtering. 61 Can generate filter predicates based on transitivity or functional dependencies.
  • 62. Join  Factoriza.on   is transformed into Combines branches of UNION / UNION ALL that join a common table in order to reduce # of accesses to that table. 62
  • 63. Understanding  how  the   op.mizer  transforms  queries   helps  you  write  beTer  SQL  and   understand  execu.on  plans.   ©2013 Enkitec 63
  • 65. What  is  Cardinality  Feedback?   •  Automa.cally  improve  plans  for  repeatedly   executed  queries  where  the  op.mizer  does   not  es.mate  cardinali.es  properly   •  Mises.mates  may  be  due  to   –  Missing  or  inaccurate  sta.s.cs   –  Complex  predicates        …and  more   •  Es.mates  and  actuals  are  compared   •  New  plan  generated  using  adjustment  factors   •  Es.mates  lost  if  plan  ages  out  of  cache     ©2013 Enkitec 65
  • 68. New  plan   ©2013 Enkitec 68
  • 69. Es.mates  Differences   Plan  Opera.on   Object   Rows  es.mate   Original  Plan   23835   TABLE  ACCESS  FULL   VS_PRODUCT   67   INDEX  FAST  FULL  SCAN   IDX_VS_ORDER_027   514K   INDEX  FAST  FULL  SCAN   IDX_VS_ORDER_TRACK_017   2665K   INDEX  FAST  FULL  SCAN   PK_VS_ORDER   8673K   New  Plan   8371   ✔ INDEX  FULL  SCAN   IDX_VS_PRODUCT_002   84   INDEX  SKIP  SCAN   IDX_VS_ORDER_027   941K   INDEX  SKIP  SCAN   IDX_VS_ORDER_TRACK_017   2899K   INDEX  FAST  FULL  SCAN   PK_VS_ORDER   10M   ©2013 Enkitec 69
  • 70. Cardinality  feedback  is  an  automated   way  the  op.mizer  tries  to   "self-­‐correct"  and  produce  plans   that  perform  op.mally.   ©2013 Enkitec 70
  • 71. What's  ahead   in  12c?   ©2013 Enkitec 71
  • 72. Dynamic  Sta.s.cs   •  Replaces  dynamic  sampling   •  Compensates  for  missing  or  incomplete  stats   •  Time  to  do  collec.ons  is  limited  by  op.mizer   based  on  es.mated  query  run  .me   •  When  collected,  stats  will  be  stored  in  cache   and  therefore  can  be  shared  among  queries   •  Different  queries  using  same  predicates  can   used  dynamic  stats  stored  in  cache   ©2013 Enkitec 72
  • 73. Adap.ve  Sta.s.cs   •  Was  cardinality  feedback  in  11gR2   •  Es.mates  compared  to  actuals   •  Significant  varia.ons  cause  new  plan  choice   •  New  plan  uses  stats  from  previous  execu.ons   •  Previous  stats  retained  for  later  use  as  SQL   Plan  Direc.ves   •  Statements  can  be  re-­‐op.mized  over  and  over   –  New  column  V$SQL.IS_REOPTIMIZABLE ©2013 Enkitec 73
  • 74. Adap.ve  Execu.on  Plans   •  More  than  1  plan  pre-­‐computed  and  stored   •  Sta.s.cs  collectors  used  to  capture  execu.on   info   •  If  NL  chosen  and  exceeds  certain  threshold,   will  switch  to  HJ   •  DBMS_XPLAN.DISPLAY_CURSOR  has  new   format  parameter  to  show  ac.ons     format=>'+all_dyn_plan +adaptive' ©2013 Enkitec 74
  • 76. Sta.s.cs  must  reasonably   represent  your  actual  data.   76
  • 77. Understanding  basic   op.mizer  sta.s.cs   computa.ons  is  key.   77
  • 78. Write  SQL  to   u.lize  your  knowledge   of  how  sta.s.cs  are   used  by  the  op.mizer.   78
  • 79. The  more  you  know,   the  more  likely  your     sta.s.cs  strategy   and  the  way  you  write  SQL   will  support  op.mally   performing  execu.on  plans.   79
  • 80.
  • 82. Q & Q U E S T I O N S A N S W E R S