Friday, October 3, 2008

Sparse Columns in Sql Server 2008


One of the major enhancements in database engine of SQL Server 2008 is Sparse column. It improves data retrieval and reduces the storage cost. It also can be used with Filtered Index to improve the performance.

Sparse columns are ordinary columns that have an optimized storage for null values. Sparse columns reduce the space requirements for null values at the cost of more overhead to retrieve non-null values. Consider using sparse columns when the space saved is at least 20 percent to 40 percent. Sparse columns and column sets are defined by using the CREATE TABLE or ALTER TABLE statements.


Say you have 70,00 records in a particular Table, out which say 50,000 can remain NULL as you know.
So now it will take the space for whole 70000 record. What if the table saves this space?.this is where Sparse Coluns comes in the role.



How to create sparse column? 
Simple , just mention sparse keyword in table creation or alter statement.

CREATE TABLE TestSparseColumn
(Comments varchar(max) SPARSE null)


Summary

Using sparse columns is a good way to reduce physical storage. This feature will be useful in following scenarios

  • Storage space is a critical criteria (with sparse columns the disk space can be saved from 20 to 40 %)
  • If there are a lot of columns in a table which are likely to have NULL values (more than 50%)


Thanks,
Nitin Sharma