sql-serversql-server-2008database-design

Why and when should I use SPARSE COLUMN?


SQL Server 2008's new feature "SPARSE COLUMN" doesn't take any space if the column value is 0 or NULL, but when there is a value it takes 4 times the space a regular (non sparse) column holds.

Why should I go for that at the time of database design? And if I should use that, in what situation will that be? Also how does no space get reserved when a column is defined as sparse, what is the internal implementation?


Solution

  • A sparse column doesn't use 4x the amount of space to store a value, it uses a (fixed) 4 extra bytes per non-null value. (As you've already stated, a NULL takes 0 space.)

    So the "expected savings" depends strongly on what kind of column we're talking about, and your estimate of what ratio will be null vs non-null. Variable width columns (varchars) are probably a little more difficult to predict accurately.

    This Books Online Page has a table showing what percentage of different data types would need to be null for you to end up with a benefit.

    So when should you use a Sparse Column? When you expect a significant percentage of the rows to have a NULL value. Some examples that come to mind: