SQL Server provides Sparse Columns to efficiently store NULL values while minimizing storage consumption. Sparse columns are ideal when a significant percentage of rows contain NULL values in a column.

1. What Are Sparse Columns?

Sparse columns are ordinary columns optimized for NULL storage. When a column is declared as SPARSE, it does not consume storage for NULL values, making them beneficial when a large number of rows have NULLs.

2. Declaring Sparse Columns

To use sparse columns, declare them with the SPARSE attribute.

Example. Creating a Table with Sparse Columns.

CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY,
    Name VARCHAR(100) NOT NULL,
    PhoneNumber VARCHAR(20) SPARSE NULL,
    Address NVARCHAR(255) SPARSE NULL
);

PhoneNumber and Address will not consume storage when NULL.

When storing non-NULL values, they use more storage than regular columns.

3. Storage Considerations

The impact on storage depends on the data type.

When to Use Sparse Columns?

Example of Storage Cost for INT Data Type.

Storage cost

4. Using Sparse Columns with Column Sets

SQL Server provides Column Sets to handle sparse columns dynamically.

Example. Using Column Set for Dynamic Queries.

CREATE TABLE EmployeeData (
    EmployeeID INT PRIMARY KEY,
    Name VARCHAR(100) NOT NULL,
    PhoneNumber VARCHAR(20) SPARSE NULL,
    Address NVARCHAR(255) SPARSE NULL,
    AdditionalData XML COLUMN_SET FOR ALL_SPARSE_COLUMNS
);

AdditionalData (XML) aggregates all sparse column values into a single XML column dynamically.

Retrieving Data Using Column Set

SELECT EmployeeID, AdditionalData FROM EmployeeData;

The Column Set simplifies handling dynamic attributes.

5. Querying Sparse Columns Efficiently

Use Filtered Indexes to optimize queries on sparse columns.

Example. Creating a Filtered Index.

CREATE INDEX IX_Employees_PhoneNumber
ON Employees(PhoneNumber)
WHERE PhoneNumber IS NOT NULL;

This improves query performance for non-NULL sparse column searches.

Example. Query with Index Utilization.

SELECT Name, PhoneNumber
FROM Employees
WHERE PhoneNumber IS NOT NULL;

The filtered index ensures efficient lookups.

6. Checking Sparse Column Storage Space

You can analyze storage savings using sys.dm_db_index_physical_stats.

Check Space Savings.

SELECT name, is_sparse, max_length
FROM sys.columns
WHERE object_id = OBJECT_ID('Employees');

This shows which columns are SPARSE.

7. When NOT to Use Sparse Columns

Avoid sparse columns when.

8. Test Tables with sparse and without parse columns

Create two tables as below.

Tables

Add random data in both tables.

Random Data

Check Table space.

Table Space

Sparse columns in SQL Server are a powerful way to optimize NULL storage, reduce space usage, and improve performance. They work best when a high percentage of values are NULL and can be efficiently queried using filtered indexes and column sets.