{"id":142,"date":"2008-11-04T23:33:32","date_gmt":"2008-11-04T21:33:32","guid":{"rendered":"https:\/\/www.sqlinthewild.co.za\/?p=142"},"modified":"2008-11-04T23:33:32","modified_gmt":"2008-11-04T21:33:32","slug":"what-are-statistics-and-why-do-we-need-them","status":"publish","type":"post","link":"https:\/\/www.sqlinthewild.co.za\/index.php\/2008\/11\/04\/what-are-statistics-and-why-do-we-need-them\/","title":{"rendered":"What are statistics and why do we need them?"},"content":{"rendered":"<p>Statistics are another area of SQL that seem to be fairly poorly understood, on what they are, how they are used and how they are maintained..<\/p>\n<p>Statistics store information about the distribution of data in the columns. The information includes the density of the key columns which gives a measure of the uniqueness of the index, and a histogram that stores information on how the distribution of values within the column.<\/p>\n<h3><strong>Creating statistics<\/strong><\/h3>\n<p>When an index is created, statistics will automatically be created on the index columns. The stats on an index cannot be dropped while the index exists. If the index is a multi-column one, the density is stored for all columns, but the histogram is only stored for the leading column.<\/p>\n<p>It&#8217;s also possible to have statistics that aren&#8217;t associated with indexes. These are created automatically by the query optimiser (providing auto_create_statistics is enabled) or they can be created manually with the CREATE STATISTICS statement.<\/p>\n<p>It&#8217;s not usually necessary to create statistics manually as the optimiser usually knows best what stats are required.<\/p>\n<h3><strong>Updating Statistics<\/strong><\/h3>\n<p>SQL keeps a count of the number of changes to a column and will update the statistics when it deems sufficient changes to have occurred (providing auto_update_stats is enabled).The stats are not updates as part of a data modification. Rather the next select that requires those statistics will trigger the update. In most cases, the auto-update doesn&#8217;t cause problems. It runs in the read-uncommitted isolation level and hence won&#8217;t cause blocking and SQL will, by default, only sample a portion of the table to calculate the stats.<\/p>\n<p><!--more-->For larger tables, that threshold is 20% of the rows in the table. Sometimes, especially on ever-increasing columns (identity, date inserted), on larger tables that threshold is too high and the statistics can get very inaccurate before the auto update kicks in. In  cases like that it&#8217;s sometimes necessary to manually update the statistics on a regular basis using the UPDATE STATISTICS statement.<\/p>\n<p>The other problem with the auto update is that sometimes it&#8217;s not accurate enough. In cases where large tables have an uneven distribution of values in the column, the sampled update may miss values or may miscalculate the distribution. In cases like this, it may be necessary to manually update the statistics with a full scan, forcing SQL to evaluate the entire table. That option should be used with care and only where necessary as it may have a negative impact on the performance of the entire system<\/p>\n<p>Finally, when indexes are rebuilt (not reorganised) the statistics on those indexes are updated with a full scan.<\/p>\n<h3><strong>How are stats used<\/strong><\/h3>\n<p>Statistics are used by the query optimiser to estimate how many rows each operator in the query plan will operate on. The row count affects the cost of the various operators, some work well on small rowsets, others work well on large row sets. With a good estimate of the row count, the optimiser can get a fairly accurate calculation of the cost of each operator and hence can work out what the cheapest plan is for the particular query.<\/p>\n<p>Without accurate statistics, the optimiser is likely to miscalculate the cost of various query operators and hence miscalculate the cost of the entire plan. This can lead to very poorly performing queries<\/p>\n<p>In a future post I&#8217;ll discuss how to identify out of date or inaccurate statistics.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Statistics are another area of SQL that seem to be fairly poorly understood, on what they are, how they are used and how they are maintained.. Statistics store information about the distribution of data in the columns. The information includes&#8230; <a class=\"read-more-button\" href=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2008\/11\/04\/what-are-statistics-and-why-do-we-need-them\/\">(Read more)<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[25,15],"tags":[],"class_list":["post-142","post","type-post","status-publish","format-standard","hentry","category-performance","category-sql-server"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7h6n-2i","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/comments?post=142"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/142\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/media?parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/categories?post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/tags?post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}