<p>In your experience, how often should Oracle database statistics be run? Our team of developers recently discovered that statistics hadn't been run our production box in over 2 1/2 months. That sounds like a long time to me, but I'm not a DBA.</p>

[{'answer_id': 79425, 'author': 'Jonathan Rupp', 'author_id': 12502, 'author_profile': 'https://Stackoverflow.com/users/12502', 'pm_score': 4, 'selected': False, 'text': '<p>Whenever the data changes "significantly".</p>\n\n<p>If a table goes from 1 row to 200 rows, that\'s a significant change.  When a table goes from 100,000 rows to 150,000 rows, that\'s not a terribly significant change.  When a table goes from 1000 rows all with identical values in commonly-queried column X to 1000 rows with nearly unique values in column X, that\'s a significant change.</p>\n\n<p>Statistics store information about item counts and relative frequencies -- things that will let it "guess" at how many rows will match a given criteria.  When it guesses wrong, the optimizer can pick a <strong>very</strong> suboptimal query plan.</p>\n'}, {'answer_id': 79539, 'author': 'David Medinets', 'author_id': 219658, 'author_profile': 'https://Stackoverflow.com/users/219658', 'pm_score': 3, 'selected': False, 'text': '<p>What Oracle version are you using? Check this page which refers to Oracle 10:</p>\n\n<p><a href="http://www.acs.ilstu.edu/docs/Oracle/server.101/b10752/stats.htm" rel="noreferrer">http://www.acs.ilstu.edu/docs/Oracle/server.101/b10752/stats.htm</a></p>\n\n<p>It says:</p>\n\n<blockquote>\n  <p>The recommended approach to gathering statistics is to allow Oracle to automatically gather the statistics. Oracle gathers statistics on all database objects automatically and maintains those statistics in a regularly-scheduled maintenance job.</p>\n</blockquote>\n'}, {'answer_id': 79560, 'author': 'Joe Skora', 'author_id': 14057, 'author_profile': 'https://Stackoverflow.com/users/14057', 'pm_score': 2, 'selected': False, 'text': '<p>When I was managing a large multi-user planning system backed by Oracle, our DBA had a weekly job that gathered statistics.  Also, when we rolled out a significant change that could affect or be affected by statistics, we would force the job to run out of cycle to get things caught up.</p>\n'}, {'answer_id': 87175, 'author': 'Justin Cave', 'author_id': 10397, 'author_profile': 'https://Stackoverflow.com/users/10397', 'pm_score': 1, 'selected': False, 'text': '<p>Make sure to balance the risk that fresh statistics cause undesirable changes to query plans against the risk that stale statistics can themselves cause query plans to change.  </p>\n\n<p>Imagine you have a bug database with a table ISSUE and a column CREATE_DATE where the values in the column increase more or less monotonically.  Now, assume that there is a histogram on this column that tells Oracle that the values for this column are uniformly distributed between January 1, 2008 and September 17, 2008.  This makes it possible for the optimizer to reasonably estimate the number of rows that would be returned if you were looking for all issues created last week (i.e. September 7 - 13).  If the application continues to be used and the statistics are never updated, though, this histogram will be less and less accurate.  So the optimizer will expect queries for "issues created last week" to be less and less accurate over time and may eventually cause Oracle to change the query plan negatively.  </p>\n'}, {'answer_id': 90349, 'author': 'user11318', 'author_id': 11318, 'author_profile': 'https://Stackoverflow.com/users/11318', 'pm_score': 5, 'selected': True, 'text': "<p>At my last job we ran statistics once a week.  If I remember correctly, we scheduled them on a Thursday night, and on Friday the DBAs were very careful to monitor the longest running queries for anything unexpected.  (Friday was picked because it was often just after a code release, and tended to be a fairly low traffic day.)  When they saw a bad query they would find a better query plan and save that one so it wouldn't change again unexpectedly.  (Oracle has tools to do this for you automatically, you tell it the query to optimize and it does.)</p>\n\n<p>Many organizations avoid running statistics out of fear of bad query plans popping up unexpectedly.  But this usually means that their query plans get worse and worse over time.  And when they do run statistics then they encounter a number of problems.  The resulting scramble to fix those issues confirms their fears about the dangers of running statistics.  But if they ran statistics regularly, used the monitoring tools as they are supposed to, and fixed issues as they came up then they would have fewer headaches, and they wouldn't encounter them all at once.</p>\n"}, {'answer_id': 623588, 'author': 'David Aldridge', 'author_id': 6742, 'author_profile': 'https://Stackoverflow.com/users/6742', 'pm_score': 0, 'selected': False, 'text': '<p>In the case of a data warehouse-type system you can consider collecting no statistics at all, and relying on dynamic sampling (setting optimizer_dynamic_sampling to level 2 or above).</p>\n'}, {'answer_id': 630725, 'author': 'MichaelN', 'author_id': 75756, 'author_profile': 'https://Stackoverflow.com/users/75756', 'pm_score': 2, 'selected': False, 'text': '<p>With 10g and higher version of oracle, up to date statistics on tables and indexes are needed by the optimizer to make "good" execution plan decision.  How often you collect statistics is a tricky call.  It depends on your application, schema, data rate and business practice.  Some third party apps which are written to be backward compatible with older version of oracle do not perform well with the new optimizer.  Those application require that tables have no stats so that the db resorts back to rule base execution plan.  But on the average oracle recommends that stats be collected on tables with stale statistics.  You can set tables to be monitor and check their state and have them analyze if/when stale.  Often that is enough, sometime it is not.  It really depend on your database.  For my database we have a set of OLTP tables that need nightly stats collection to maintain performance.  Other tables are analyze once a week.  On our large dw database, we analyze as needed as the tables are too large for regular analysis without affecting overall db load and performance.  So the correct answer is, it depends on the application, data change and business needs.</p>\n'}, {'answer_id': 16741489, 'author': 'grokster', 'author_id': 502441, 'author_profile': 'https://Stackoverflow.com/users/502441', 'pm_score': 4, 'selected': False, 'text': "<p>Since Oracle 11g statistics are gathered automatically by default.</p>\n\n<p>Two Scheduler windows are predefined upon installation of Oracle Database:</p>\n\n<ul>\n<li>WEEKNIGHT_WINDOW starts at 10 p.m. and ends at 6 a.m. every Monday\nthrough Friday. </li>\n<li>WEEKEND_WINDOW covers whole days Saturday and Sunday.</li>\n</ul>\n\n<p>When statistics were last gathered?</p>\n\n<pre><code>SELECT owner, table_name, last_analyzed FROM all_tables ORDER BY last_analyzed DESC NULLS LAST; --Tables.\nSELECT owner, index_name, last_analyzed FROM all_indexes ORDER BY last_analyzed DESC NULLS LAST; -- Indexes.\n</code></pre>\n\n<p>Status of automated statistics gathering?</p>\n\n<pre><code>SELECT * FROM dba_autotask_client WHERE client_name = 'auto optimizer stats collection';\n</code></pre>\n\n<p>Windows Groups?</p>\n\n<pre><code>SELECT window_group_name, window_name FROM dba_scheduler_wingroup_members;\n</code></pre>\n\n<p>Window Schedules?</p>\n\n<pre><code>SELECT window_name, start_time, duration FROM dba_autotask_schedule;\n</code></pre>\n\n<p>Manually gather Database Statistics in this Schema: </p>\n\n<pre><code>EXEC dbms_stats.gather_schema_stats(ownname=&gt;NULL, cascade=&gt;TRUE); -- cascade=&gt;TRUE means include Table Indexes too.\n</code></pre>\n\n<p>Manually gather Database Statistics in all Schemas!</p>\n\n<pre><code>-- Probably need to CONNECT / AS SYSDBA\nEXEC dbms_stats.gather_database_stats;\n</code></pre>\n"}, {'answer_id': 25973507, 'author': 'Chion', 'author_id': 4065146, 'author_profile': 'https://Stackoverflow.com/users/4065146', 'pm_score': 0, 'selected': False, 'text': '<p>Generally it\'s not recommended to gather statistics so frequent on the whole database unless you have a strong justification for that, such as a bulk insert or big data change happen frequently on the database.\ngathering statistics on the database in this frequency MAY change the queries execution plan to a new poor execution plans, the thing may cost you much time trying to tune every query affected by the new poor plans, this is why you should test the impact of gathering new statistics on a test database, or in case you don\'t have the time or the man power for that, at least you should keep a fallback plan by backing up the original statics before you gather new ones, so in case you gather a new statistics and then the queries didn\'t perform as expected, you can easily restore back the original statistics.</p>\n\n<p>There is a very useful script can help you backup original statistics and gather new ones and provide you with SQL command you can use to restore back the original statics in case the thing didn\'t go as expected after gathering new statistics. You can find the script in this link:\n<a href="http://dba-tips.blogspot.com/2014/09/script-to-ease-gathering-statistics-on.html" rel="nofollow">http://dba-tips.blogspot.com/2014/09/script-to-ease-gathering-statistics-on.html</a></p>\n'}]
['https://Stackoverflow.com/questions/79229', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']