SQL SERVER – Performance Benefit of Using SPARSE Columns?

I have written a number of blogs around working with SPARSE Columns in the past and a primer to what SPARSE Columns can be got from the list mentioned:

As you get through these, there are a number of other considerations one needs to take. In a recent consulting assignment, I had the luxury of having a discussion with the developers around using SPARSE columns. It was a SaaS based application and many of the columns in their table design would be NULL. Hence, once I saw the dataset, I talked about SPARSE Columns.

Immediately, the developer and DBA reverted back saying is it always performant to use SPARSE columns in their design? There are edge cases where this would fail – we will reserve this for the future? Here is a classic example to showcase the same.

To showcase this, let us try to create and prepare our environment:

Create Database TestDB
GO
USE TestDB
GO
CREATE TABLE sparse_tbl1 (Col1 INT SPARSE, Col2 INT)
GO
INSERT INTO sparse_tbl1 VALUES (null, 7)
INSERT INTO sparse_tbl1 VALUES (3, null)

Now the specifics of data inserted and visualizing come here.

--run DBCC IND to find the page we stored this data on. Look for PageType=1
DBCC IND('TestDB', 'sparse_tbl1', 1)
GO
DBCC TRACEON (3604);
GO
--run DBCC PAGE
DBCC PAGE ('TestDB', 1, 312, 3)

I have taken the output and also shown below as text.

Slot 0 Offset 0x60 Length 11
Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP Record Size = 11
………….. <>
Slot 1 Offset 0x6b Length 27
Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNS
Record Size = 27

Let me read you through the same. Let’s look at the DBCC PAGE output of the data inserted. In the first row we inserted a NULL in the sparse column and the value 7 in the non-sparse column. As a result the row length is 11 bytes overall and only Column 2 has been stored.

Now examine the second row (slot 1). Here we inserted a non-null value in the sparse column and a NULL in the ordinary column.  The row length is now 27 bytes. We see two columns, the first one being a sparse column. I have highlighted the actually data value in green. As you can observe, 16 bytes were gained in using NULL in a sparse column versus using a NULL in a non-sparse column in this example using INT data type.

In the above example we found that NULL values used in a sparse column lead to more efficient space usage. Let us examine the opposite scenario, i.e how much space is used up when non-null data is stored in a Sparse column – this will be discussed in a different blog.

Reference: Pinal Dave (https://blog.sqlauthority.com)

Exit mobile version