{
  "date": "2016-12-07T10:48:55",
  "slug": "oracle-12cr2-statistics-advisor",
  "link": "https://www.dbi-services.com/blog/oracle-12cr2-statistics-advisor/",
  "title": {
    "rendered": "Oracle 12cR2: Statistics Advisor"
  },
  "content": {
    "rendered": "<h2>By Franck Pachot</h2>\n<p>.<br />\nToday at #ukoug_tech16 12:30 in hall 11A I&#8217;ll talk about <a href=\"http://www.tech16.ukoug.org/default.asp?p=14778&amp;dlgact=shwprs&amp;prs_prsid=11948&amp;day_dayid=102\" target=\"_blank\" rel=\"noopener noreferrer\">Statistics Gathering Best Practice &amp; 12cR2 Statistics Advisor</a><br />\nRather than taking the Optimizer Statistics Advisor rules one by one I&#8217;ll show the things to take care (some people may call it best practices) when  gathering statistics and they mention the Statistics Advisor Rule.<br />\nIf you need a reference about all rules, you can get it from V$STATS_ADVISOR_RULES<br />\n<!--more--></p>\n<pre><code>09:41:19 SQL&gt; select to_char(rule_id,99)||' '||description||' ('||name||')' from V$STATS_ADVISOR_RULES where rule_id&gt;0 order by rule_id;\n&amp;nbsp\nTO_CHAR(RULE_ID,99)||''||DESCRIPTION||'('||NAME||')'\n----------------------------------------------------------------------------------------------------</code></pre>\n<p>I&#8217;ll explain them briefly here.</p>\n<p>You should always enable to automatic statistic gathering job. You may manage special cases manually, but do not disable it.</p>\n<pre><code>  1 Use Auto Job for Statistics Collection (UseAutoJob)</code></pre>\n<p>For sure if the job does not complete successfully, the advisor detects the probable cause.</p>\n<pre><code>  2 Auto Statistics Gather Job should complete successfully (CompleteAutoJob)</code></pre>\n<p>Statistics history may save your life in case of a regression. But be sure the retention is not too large and purge occurs or SYSAUX will grow</p>\n<pre><code>  3 Maintain Statistics History (MaintainStatsHistory)</code></pre>\n<p>The faster the statistics gathering run, the more statistics you can gather. Use all your server resources for it.</p>\n<pre><code>  4 Use Concurrent preference for Statistics Collection (UseConcurrent)</code></pre>\n<p>Default options for global preferences are what the optimizer developers think are the best for most cases.</p>\n<pre><code>  5 Use Default Preference for Stats Collection (UseDefaultPreference)</code></pre>\n<p>Humm.. this one was introduced before the decision not to activate SPD by default<br />\n(see <a href=\"http://dbi-services.com/blog/oracle-12cr2-optimizer-adaptive-statistics/\" target=\"_blank\" rel=\"noopener noreferrer\">http://dbi-services.com/blog/oracle-12cr2-optimizer-adaptive-statistics/</a>)</p>\n<pre><code>  6 SQL Plan Directives should not be disabled (TurnOnSQLPlanDirective)</code></pre>\n<p>Setting statistics manually may be used as a workaround but not the general case</p>\n<pre><code>  7 Avoid Set Statistics Procedures (AvoidSetProcedures)</code></pre>\n<p>When you run dbms_stats.gather_&#8230;_stats manually, default options are what the optimizer developers think are the best for most cases.</p>\n<pre><code>  8 Use Default Parameters in Statistics Collection Procedures (UseDefaultParams)</code></pre>\n<p>And in those cases, better to run it for a schema so that you are sure to include newly created tables</p>\n<pre><code>  9 Use gather_schema_stats procedure (UseGatherSchemaStats)</code></pre>\n<p>You waste time and ressources if you gather statistics in a addition to what is done with online statistics gathering</p>\n<pre><code> 10 Avoid inefficient statistics operation sequences (AvoidInefficientStatsOprSeq)</code></pre>\n<p>You waste time and ressources if you gather statistics when nothing has changed</p>\n<pre><code> 11 Avoid unnecessary statistics collection (AvoidUnnecessaryStatsCollection)</code></pre>\n<p>You need statistics for all tables</p>\n<pre><code> 12 Avoid objects with stale or no statistics (AvoidStaleStats)</code></pre>\n<p>Statistics gathered before bulk inserts will be immediately stale</p>\n<pre><code> 13 Do not gather statistics right before bulk DML (GatherStatsAfterBulkDML)</code></pre>\n<p>You don&#8217;t want the automatic statistics gathering run on a table between a truncate and an insert</p>\n<pre><code> 14 Statistics for objects with volatile data should be locked (LockVolatileTable)</code></pre>\n<p>But let it run for tables with no massive change</p>\n<pre><code> 15 Statistics for objects with non-volatile should not be locked (UnlockNonVolatileTable)\n 16 Statistics of dependent objects should be consistent (MaintainStatsConsistency)</code></pre>\n<p>Better truncate, make indexes unusable, and insert /*+ append */ than drop and recreate the table (which removes statistics).</p>\n<pre><code> 17 Avoid drop and recreate object seqauences (AvoidDropRecreate)</code></pre>\n<p>Statistics advisor may detect when incremental statistics gathering is me efficient for partitioned tables</p>\n<pre><code> 18 Statistics should be maintained incrementally when it is beneficial (UseIncremental)\n 19 Statistics should not be maintained incrementally when it is not beneficial (NotUseIncremental)</code></pre>\n<p>Stale statistics may lead to under-estimation because of linear decay</p>\n<pre><code> 20 Avoid Out of Range Histogram endpoints (AvoidOutOfRange)</code></pre>\n<p>Large tables can be scanned in parallel, recommendation is default degree</p>\n<pre><code> 21 Use Auto Degree for statistics collection (UseAutoDegree)</code></pre>\n<p>As we have seen about global preferences, table preference should be default for most cases (rolling invalidation, auto sample size, auto histogram size)</p>\n<pre><code> 22 Use Default Object Preference for statistics collection (UseDefaultObjectPreference)</code></pre>\n<p>And for sure dbms_stats is the way to gather statistics for the optimizer. ANALYZE is deprecated for that since 91</p>\n<pre><code> 23 Avoid using analyze table commands for statistics collection (AvoidAnalyzeTable)</code></pre>\n<p>Those are only my interpretation. 12.2 is new (and cloud first) and I&#8217;ve not observed all those recommandations yet. But there are properly described by the advisor.<br />\nThis is the kind of output we can get:</p>\n<pre><code>\n----------------------------------------------------------------------------------------------------\nGENERAL INFORMATION\n-------------------------------------------------------------------------------\n&nbsp;\n Task Name       : MY_TASK\n Execution Name  : EXEC_52\n Created         : 12-07-16 11:31:40\n Last Modified   : 12-07-16 11:32:37\n-------------------------------------------------------------------------------\nSUMMARY\n-------------------------------------------------------------------------------\n For execution EXEC_52 of task MY_TASK, the Statistics Advisor has 6\n finding(s). The findings are related to the following rules: USECONCURRENT,\n AVOIDSETPROCEDURES, USEDEFAULTPARAMS, USEGATHERSCHEMASTATS, AVOIDSTALESTATS,\n UNLOCKNONVOLATILETABLE. Please refer to the finding section for detailed\n information.\n-------------------------------------------------------------------------------\nFINDINGS\n-------------------------------------------------------------------------------\n Rule Name:         UseConcurrent\n Rule Description:  Use Concurrent preference for Statistics Collection\n Finding:  The CONCURRENT preference is not used.\n&nbsp;\n Recommendation:  Set the CONCURRENT preference.\n Example:\n dbms_stats.set_global_prefs('CONCURRENT', 'ALL');\n Rationale:  The system's condition satisfies the use of concurrent statistics\n             gathering. Using CONCURRENT increases the efficiency of statistics\n             gathering.\n----------------------------------------------------\n...\n</code></pre>\n",
    "protected": false
  }
}
