{"id":366,"date":"2011-11-12T12:12:12","date_gmt":"2011-11-12T06:42:12","guid":{"rendered":"http:\/\/beginsql.wordpress.com\/?p=366"},"modified":"2011-11-12T12:12:12","modified_gmt":"2011-11-12T06:42:12","slug":"sparse-columns-in-sql-server-2008","status":"publish","type":"post","link":"https:\/\/sqlhints.com\/2011\/11\/12\/sparse-columns-in-sql-server-2008\/","title":{"rendered":"Sparse Columns in SQL Server 2008"},"content":{"rendered":"<p style=\"text-align:justify;\">Sparse Column is one more new feature introduced in SQL SERVER 2008. Storing a null value in a sparse column doesn&#8217;t take any space, but storing a non-null value in sparse column takes 4 bytes extra space than the non-sparse columns of the same data type.<\/p>\n<p style=\"text-align:justify;\">For example: As we know that storing a null\/non-null value in a DATETIME column takes 8 bytes. On the other hand Sparse DATETIME column takes no space for storing null value\u00a0but storing a non-null value will take 12 bytes i.e. 4 bytes extra then normal DATETIME column.<\/p>\n<p style=\"text-align:justify;\">At this moment the obvious question which arises in our mind is: Why 0 bytes for null value and 4 bytes extra for storing non-null value in a sparse column? Reason for this is, sparse column&#8217;s value is not stored together with normal columns in a row, instead they are stored at the end of each row as special structure named Sparse Vector. Sparse vector structure contains:<\/p>\n<p style=\"text-align:justify;\">\u00a0[List of non-null Sparse Column Id&#8217;s \u2013 It takes 2 Bytes for each non-null Sparse column] + [List of Column Offsets &#8212; It takes 2 bytes for each non-null Sparse column].<\/p>\n<p style=\"text-align:justify;\">So, defining columns with high density of null value as Sparse will result in huge space saving. As explained previously non-null value in the sparse column is stored in a complex structure, so reading non-null sparse column value will have slight performance overhead.<\/p>\n<p style=\"text-align:justify;\">Let us understand the Sparse Column concept with below example.<\/p>\n<p style=\"text-align:justify;\"><span style=\"color:#0000ff;\">Example:\u00a0 <\/span>In this example we will create two identical tables. Only difference between them is, in one table two columns are marked as Sparse. In both of these tables insert some 25k records and check the space utilization by these tables.<\/p>\n<pre>CREATE DATABASE SPARSEDEMO\nGO\nUSE SPARSEDEMO\nGO\nCREATE TABLE SPARSECOLUMNTABLE\n(\n\u00a0col1 int identity(1,1),\n\u00a0col2 datetime <span style=\"color:#0000ff;\">sparse<\/span>,\n\u00a0col3 int <span style=\"color:#0000ff;\">sparse<\/span>\n)\nCREATE TABLE NONSPARSECOLUMNTABLE\n(\n\u00a0col1 int identity(1,1),\n\u00a0col2 datetime,\n\u00a0col3 int\n)\nGO\nINSERT INTO SPARSECOLUMNTABLE VALUES(NULL,NULL)\nINSERT INTO NONSPARSECOLUMNTABLE VALUES(NULL,NULL)\nGO 25000<\/pre>\n<p style=\"text-align:justify;\">\u00a0Now check the space used by these tables by executing the below statements:<\/p>\n<pre><span style=\"font-size:small;\">EXEC SP_Spaceused SPARSECOLUMNTABLE <\/span>\n<span style=\"font-size:small;\">EXEC SP_Spaceused NONSPARSECOLUMNTABLE<\/span><\/pre>\n<p>Result:<\/p>\n<pre>name\u00a0             rows\u00a0       reserved\u00a0data\u00a0  index_size\u00a0unused\nSPARSECOLUMNTABLE\u00a025000\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0<span style=\"color:#008000;\">392 KB\u00a0 344 KB<\/span>\u00a08 KB\u00a0      40 KB\n\nname\u00a0                rows\u00a0       reserved\u00a0data\u00a0  index_size\u00a0unused\nNONSPARSECOLUMNTABLE\u00a025000\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0<span style=\"color:#ff0000;\">712 KB\u00a0 656 KB\u00a0<\/span>8 KB\u00a0      48 KB<\/pre>\n<p>So, with above example it is clear that defining a column with high density of null values result&#8217;s in huge space saving.<\/p>\n<p>Please correct me, if\u00a0my understanding\u00a0is wrong. Comments are always welcome.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sparse Column is one more new feature introduced in SQL SERVER 2008. Storing a null value in a sparse column doesn&#8217;t take any space, but storing a non-null value in sparse column takes 4 bytes extra space than the non-sparse columns of the same data type. For example: As we know that storing a null\/non-null &hellip; <a href=\"https:\/\/sqlhints.com\/2011\/11\/12\/sparse-columns-in-sql-server-2008\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Sparse Columns in SQL Server 2008<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"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":"Sparse Columns in SQL Server 2008: http:\/\/wp.me\/p14R6Y-5U","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":[3,4],"tags":[50,53],"class_list":["post-366","post","type-post","status-publish","format-standard","hentry","category-sql-server","category-sql-server-2008-whats-new","tag-sparse","tag-sql-server-2008"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3xNAz-5U","_links":{"self":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/366","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/comments?post=366"}],"version-history":[{"count":0,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/posts\/366\/revisions"}],"wp:attachment":[{"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/media?parent=366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/categories?post=366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlhints.com\/wp-json\/wp\/v2\/tags?post=366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}