Leveraging Storage Spaces Direct for SQL Server High Availability
Thursday, July 19, 2018 - click here to learn more
SQL Server 2008 has introduced a new way to store data for columns that contain excessive NULL values called Sparse Columns. What this means is that when you declare a column as Sparse and any time a NULL value is entered in the column it will not use any space. Is there a way to identify what columns would make a good candidate for this without having to analyze each column individually?
With the introduction of the new Sparse Column feature in SQL Server 2008, it is now possible to declare a column as Sparse and any time a NULL value is entered in the column it will not use any space. One of the tricks here is to figure out when to determine when a column should be defined as Sparse or not.
In the following example Address Lines 1 through 3 are required, Address Lines 4 and 5 are not required but often used, and Address Lines 6 through 8 are rarely used at all. When creating the table for Address Lines 6 through 8 we are using the SPARSE option, since we know this data will be rarely stored.
So why not just declare all columns as Sparse Columns?
Sparse Columns require four extra bytes of storage for each non NULL fixed-length data type value in the table and zero bytes to store a NULL value; therefore it is very important to have the correct threshold per data type or you will end up using more space instead of gaining it. The fewer bytes a data type uses, the higher the percentage of NULL values are required to save space.
There is a table on MSDN that gives recommended percentages for using Sparse Columns. Take a look at this to help identify when you will get the benefits of Sparse Columns.
Using this table as a guideline, the following script will identify any columns that may qualify for the new Sparse Columns feature. By searching for columns in the database that have NULL values over a certain threshold, you can easily analyze the results and determine if the new feature may be applicable. The thresholds for the fixed-length columns are stored in a temp table and the data types that are dependant on precision and length will default to sixty percent.
Here is a sample of the output when run against the AdventureWorks database. The NullPercent column should be compared to the ValuePercent column to determine if there is an advantage of using Sparse Columns. As you can see in row one all rows for Sales.SalesOrderHeader for column Comment are NULL therefore the NullPercent is 100% and the ValuePercent is 60%, so this is a great candidate for using Sparse Columns. Note: the query above limits the output to only show which columns would gain the benefit of using Sparse Columns.
In the code above I commented out two lines, shown below, that can be used to limit the search to one table at a time. Just uncomment these and change the Schema and Table values for the table you want to analyze.
|--AND INFORMATION_SCHEMA.COLUMNS.TABLE_SCHEMA = 'Person'|
--AND INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = 'Contact'
If you try to run the script on SQL 2000 you will have to change Varchar(MAX) to Varchar(8000) and will most likely have to search on a table by table basis.
- Review the article on Using Sparse Columns in Books Online. http://msdn.microsoft.com/en-us/library/cc280604.aspx
- Make note of the restrictions that are placed on Sparse Columns.
- Check out some of the other programmability enhancements in SQL 2008. http://msdn.microsoft.com/en-us/library/cc645577.aspx
- Use this script to analyze your existing data to see if you can gain some benefits using the SPARSE option
Last Update: 2008-12-04
About the author
View all my tips