{"id":195,"date":"2015-10-25T20:40:03","date_gmt":"2015-10-25T20:40:03","guid":{"rendered":"http:\/\/nenadnoveljic.com\/blog\/?p=195"},"modified":"2020-12-04T17:54:16","modified_gmt":"2020-12-04T17:54:16","slug":"stale-index-statistics","status":"publish","type":"post","link":"https:\/\/nenadnoveljic.com\/blog\/stale-index-statistics\/","title":{"rendered":"Stale index statistics"},"content":{"rendered":"<p>Gather empty schema statistics might cause index statistics to be marked as stale.<\/p>\n<p>Firstly, we will create a table with an index:<\/p>\n<pre><code>drop user u cascade ;\ncreate user u identified by Temp_1234 ;\ncreate table u.test ( a varchar2(100) ) ;\nCREATE INDEX u.ind_test_a ON U.TEST (a);<\/code><\/pre>\n<p>As expected, there are no table statistics after table creation:<\/p>\n<pre><code>select stale_stats,last_analyzed \n  from dba_tab_statistics \n  where owner='U' and table_name='TEST' \n;\nSTAL LAST_ANALY\n---- ----------\nNULL NULL<\/code><\/pre>\n<p>Although the statistics for the index were gathered during the index creation, the stale_stats is NULL:<\/p>\n<pre><code>select <span style=\"color: #ff0000;\">stale_stats<\/span>,last_analyzed \n  from <span style=\"color: #ff0000;\">dba_ind_statistics<\/span> \n  where table_owner='U' and table_name='TEST' \n;\nSTAL LAST_ANALYZED\n---- -------------------\n<span style=\"color: #ff0000;\">NULL<\/span> <span style=\"color: #ff0000;\">25.10.2015 21:09:30<\/span><\/code><\/pre>\n<p>Let&#8217;s gather empty schema statistics:<\/p>\n<pre><code>begin\n  SYS.DBMS_STATS.GATHER_schema_STATS (\n    OwnName =&gt; 'U' ,\n    Options =&gt; 'GATHER EMPTY' ,\n    Cascade =&gt; TRUE\n  );\nend ;\n\/<\/code><\/pre>\n<p>The table statistics are updated:<\/p>\n<pre><code>select stale_stats,last_analyzed \n  from dba_tab_statistics \n  where owner='U' and table_name='TEST' \n;\nSTAL LAST_ANALYZED\n---- -------------------\nNO   25.10.2015 21:09:31<\/code><\/pre>\n<p>However, according to LAST_ANALYZED the index statistics were not updated, but are marked as stale now:<\/p>\n<pre><code>select <span style=\"color: #ff0000;\">stale_stats<\/span>,last_analyzed \n  from dba_ind_statistics \n  where table_owner='U' and table_name='TEST' \n;\nSTAL LAST_ANALYZED\n---- -------------------\n<span style=\"color: #ff0000;\">YES  25.10.2015 21:09:30<\/span><\/code><\/pre>\n<h1>Partitioned index<\/h1>\n<p>I discovered another variant of this problem when a local index is involved.<\/p>\n<p>Creating the model:<\/p>\n<pre><code>drop table t ;\n\ncreate table t ( n1 integer, n2 integer ) partition by range (n1)\n(\n  partition t_1 values less than (1) \n);\n\ninsert into t values (0,0) ; \n\ncommit ;\n\ncreate index ix_t_1 on t (n2) local ;<\/code><\/pre>\n<p>Adjusting column formatting:<\/p>\n<pre><code>column partition_name format a4\ncolumn stale_stats format a4\ncolumn index_name format a6<\/code><\/pre>\n<p>Gathering statistics:<\/p>\n<pre><code>exec dbms_stats.gather_table_stats( null, 'T' ) ;\nselect index_name, partition_name, stale_stats, last_analyzed \n  from user_ind_statistics where table_name = 'T' ;\n\nINDEX_ PART STAL LAST_ANALYZED\n------ ---- ---- -------------------\nIX_T_1      NO   2020-12-04 18:23:05\nIX_T_1 T_1  NO   2020-12-04 18:23:05<\/code><\/pre>\n<p>Creating a new partition:<\/p>\n<pre><code>alter table t add partition t_2 values less than (2) ;\n\nINDEX_ PART STAL LAST_ANALYZED\n------ ---- ---- -------------------\nIX_T_1      NO   2020-12-04 18:23:05\nIX_T_1 T_1  NO   2020-12-04 18:23:05\n<span style=\"color: red;\">IX_T_1 T_2<\/span><\/code><\/pre>\n<p>Index partition rebuild gathers statistics, but doesn&#8217;t populate the STALE_STATS column.<\/p>\n<pre><code>alter index ix_t_1 rebuild partition t_2 online ;\n\nINDEX_ PART STAL LAST_ANALYZED\n------ ---- ---- -------------------\nIX_T_1      NO   2020-12-04 18:23:05\nIX_T_1 T_1  NO   2020-12-04 18:23:05\nIX_T_1 T_2       <span style=\"color: red;\">2020-12-04 18:24:17<\/span><\/code><\/pre>\n<p>Gather empty schema statistics gathers the partition statistics, including the index statistics, but wrongly sets the STALE_STATS column to &#8216;YES&#8217;.<\/p>\n<pre><code>begin\n    SYS.DBMS_STATS.GATHER_schema_STATS (\n    null,\n    Options =&gt; 'GATHER EMPTY' ,\n    Cascade =&gt; TRUE\n  );\nend ;\n\/\n\nINDEX_ PART STAL LAST_ANALYZED\n------ ---- ---- -------------------\nIX_T_1      NO   2020-12-04 18:23:05\nIX_T_1 T_1  NO   2020-12-04 18:23:05\nIX_T_1 T_2  <span style=\"color: red;\">YES  2020-12-04 18:24:17<\/span><\/code><\/pre>\n<h1>Conclusion<\/h1>\n<p>In conclusion, implicit index statistics gathering through, for example index creation or rebuild, doesn&#8217;t set STALE_STATS. A subsequent gather empty schema statistics then wrongly sets the index STALE_STATS to &#8216;YES&#8217;.<\/p>\n<h1>Updates<\/h1>\n<ul>\n<li>January 14, 2016 &#8211; Oracle filed the Bug 22544615 : GATHER EMPTY OPTION OF GATHERING STATS MARKS THE INDEX STALE<\/li>\n<li>December 4, 2020 &#8211; the local index case and conclusion added<\/li>\n<\/ul>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The gathering of empty schema statistics might cause index statistics to be marked as stale. <a href=\"https:\/\/nenadnoveljic.com\/blog\/stale-index-statistics\/\" class=\"more-link\">Continue Reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[5,18],"tags":[],"class_list":["post-195","post","type-post","status-publish","format-standard","hentry","category-oracle","category-statistics"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Stale index statistics<\/title>\n<meta name=\"description\" content=\"The gathering of empty schema statistics might cause stale index statistics.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/nenadnoveljic.com\/blog\/stale-index-statistics\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Stale index statistics\" \/>\n<meta property=\"og:description\" content=\"The gathering of empty schema statistics might cause stale index statistics.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/nenadnoveljic.com\/blog\/stale-index-statistics\/\" \/>\n<meta property=\"og:site_name\" content=\"All-round Database Topics\" \/>\n<meta property=\"article:published_time\" content=\"2015-10-25T20:40:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-04T17:54:16+00:00\" \/>\n<meta name=\"author\" content=\"Nenad Noveljic\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@NenadNoveljic\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nenad Noveljic\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/stale-index-statistics\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/stale-index-statistics\\\/\"},\"author\":{\"name\":\"Nenad Noveljic\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"headline\":\"Stale index statistics\",\"datePublished\":\"2015-10-25T20:40:03+00:00\",\"dateModified\":\"2020-12-04T17:54:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/stale-index-statistics\\\/\"},\"wordCount\":201,\"commentCount\":0,\"articleSection\":[\"Oracle\",\"Statistics\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/stale-index-statistics\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/stale-index-statistics\\\/\",\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/stale-index-statistics\\\/\",\"name\":\"Stale index statistics\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#website\"},\"datePublished\":\"2015-10-25T20:40:03+00:00\",\"dateModified\":\"2020-12-04T17:54:16+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"description\":\"The gathering of empty schema statistics might cause stale index statistics.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/stale-index-statistics\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/stale-index-statistics\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/stale-index-statistics\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Stale index statistics\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/\",\"name\":\"All-round Database Topics\",\"description\":\"Nenad Noveljic\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\",\"name\":\"Nenad Noveljic\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"caption\":\"Nenad Noveljic\"},\"sameAs\":[\"nenad-noveljic-9b746a6\",\"https:\\\/\\\/x.com\\\/NenadNoveljic\"],\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/author\\\/nenad\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Stale index statistics","description":"The gathering of empty schema statistics might cause stale index statistics.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/nenadnoveljic.com\/blog\/stale-index-statistics\/","og_locale":"en_US","og_type":"article","og_title":"Stale index statistics","og_description":"The gathering of empty schema statistics might cause stale index statistics.","og_url":"https:\/\/nenadnoveljic.com\/blog\/stale-index-statistics\/","og_site_name":"All-round Database Topics","article_published_time":"2015-10-25T20:40:03+00:00","article_modified_time":"2020-12-04T17:54:16+00:00","author":"Nenad Noveljic","twitter_card":"summary_large_image","twitter_creator":"@NenadNoveljic","twitter_misc":{"Written by":"Nenad Noveljic","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/nenadnoveljic.com\/blog\/stale-index-statistics\/#article","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/stale-index-statistics\/"},"author":{"name":"Nenad Noveljic","@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"headline":"Stale index statistics","datePublished":"2015-10-25T20:40:03+00:00","dateModified":"2020-12-04T17:54:16+00:00","mainEntityOfPage":{"@id":"https:\/\/nenadnoveljic.com\/blog\/stale-index-statistics\/"},"wordCount":201,"commentCount":0,"articleSection":["Oracle","Statistics"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/nenadnoveljic.com\/blog\/stale-index-statistics\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/nenadnoveljic.com\/blog\/stale-index-statistics\/","url":"https:\/\/nenadnoveljic.com\/blog\/stale-index-statistics\/","name":"Stale index statistics","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#website"},"datePublished":"2015-10-25T20:40:03+00:00","dateModified":"2020-12-04T17:54:16+00:00","author":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"description":"The gathering of empty schema statistics might cause stale index statistics.","breadcrumb":{"@id":"https:\/\/nenadnoveljic.com\/blog\/stale-index-statistics\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/nenadnoveljic.com\/blog\/stale-index-statistics\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/nenadnoveljic.com\/blog\/stale-index-statistics\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/nenadnoveljic.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Stale index statistics"}]},{"@type":"WebSite","@id":"https:\/\/nenadnoveljic.com\/blog\/#website","url":"https:\/\/nenadnoveljic.com\/blog\/","name":"All-round Database Topics","description":"Nenad Noveljic","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/nenadnoveljic.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa","name":"Nenad Noveljic","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","caption":"Nenad Noveljic"},"sameAs":["nenad-noveljic-9b746a6","https:\/\/x.com\/NenadNoveljic"],"url":"https:\/\/nenadnoveljic.com\/blog\/author\/nenad\/"}]}},"_links":{"self":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/comments?post=195"}],"version-history":[{"count":1,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/195\/revisions"}],"predecessor-version":[{"id":3699,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/195\/revisions\/3699"}],"wp:attachment":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/media?parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/categories?post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/tags?post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}