SQL SERVER – 2008 – Introduction to SPARSE Columns

I have been writing recently about how SQL Server 2008 is better in terms of Data Stage and Backup Management. I have received very good replies from many users and have requested to write more about it. Today we will look into another interesting concept of SPARSE column. The reason I like this feature because it is way better in terms of how columns are managed in SQL Server.

SPARSE column are better at managing NULL and ZERO values in SQL Server. It does not take any space in database at all. If column is created with SPARSE clause with it and it contains ZERO or NULL it will be take lesser space then regular column (without SPARSE clause).

In SQL Server 2008 maximum column allowed per table is 1024. All the SPARSE columns does not count to this limit of 1024. The maximum limit of SPARSE column is 100,000. In summary any table can have maximum of 100,000 SPARSE and 1024 regular columns.

Let us see following example of how SPARSE column saves space in database table.
CREATE TABLE UnSparsed(ID INT IDENTITY(1,1),
FirstCol INT,
SecondCol VARCHAR(100),
ThirdCol SmallDateTime)
GO
CREATE TABLE Sparsed(ID INT IDENTITY(1,1),
FirstCol INT SPARSE,
SecondCol VARCHAR(100) SPARSE,
ThirdCol SmallDateTime SPARSE)
GO
DECLARE @idx INT = 0
WHILE @idx < 50000
BEGIN
INSERT INTO
UnSparsed VALUES (NULL,NULL, NULL)
INSERT INTO Sparsed VALUES (NULL, NULL, NULL)
SET @idx+=1
END
GO
sp_spaceused 'UnSparsed'
GO
sp_spaceused 'Sparsed'
GO
DROP TABLE UnSparsed
GO
DROP TABLE Sparsed
GO

SQL SERVER - 2008 - Introduction to SPARSE Columns sparse

UPDATE : Read Advantages and Disadvantages of SPARSE Column SQL SERVER – 2008 – Introduction to SPARSE Columns – Part 2

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

SPARSE Columns, SQL Data Storage, SQL Scripts
Previous Post
SQL SERVER – 2008 – Two Convenient Features Inline Assignment – Inline Operations
Next Post
SQL SERVER – 2005 – Two Important Security Update

Related Posts

27 Comments. Leave new

  • This is very shallow introduction. So what is the disadvantage?

    Reply
    • Optimized storage for null values. Sparse columns reduce the space requirements for null values at the cost of more overhead to retrieve nonnull values

      Reply
  • Heiko Hatzfeld
    July 14, 2008 6:11 pm

    Hi…

    I am not quite sure if sparse columns are that great. I mean SQL2008 does come with an option to compress row data.

    How would this feature work combined with sparse Cols?

    And I dont think the cost of diskspace is that high these days. You could save a few bytes here and there, but what would you really gain? You can have more columns per row, but I see 1024 and think do we realy need more in a realtional DB? I can see more in a cube, but then most columns would store redundant data,and row compression should be very efficient.

    You mention a performance hit? How big is it? That would be much more interesting.

    Reply
  • This is very small introduction, plz give more details

    Reply
  • The row compression and page compression do compress Null value. The compression ratio of sparse is usually less than the page compression. However, Sparse has less CPU overhead than row compress and page compression because it does not compress the not null value, but only ignore the Null values. I will post an example of comparing the storage size and the performance different for these two techniques.

    Reply
  • I have a question regarding compression in sql servwer 2008. i saw a demo recently excel 2010 loads 100 million records from sql server db and on click of a column, sorting happens with in no time, can you tell me on this compression technique ?

    Reply
  • I love your site, so this is my small contribution to it.

    This could be written better: “it will be take lesser space then regular column”

    It probably should say, “it will take less space than a regular column”.

    Thanks!
    Clay

    Reply
  • Piyush Sachan
    March 14, 2011 6:53 pm

    Please see dis snap..when we insert any not null value dan its giving just opposite result…!!!!

    set nocount on
    DECLARE @idx INT = 0
    WHILE @idx < 1000
    BEGIN
    INSERT INTO UnSparsed VALUES (4,null, null)
    INSERT INTO Sparsed VALUES (4, null, null)
    SET @idx+=1
    END
    set nocount off
    GO
    sp_spaceused 'UnSparsed'
    GO
    sp_spaceused 'Sparsed'

    Reply
  • Piyush Sachan
    March 14, 2011 7:06 pm

    Please see sparse columns in both tables…its have advantage

    CREATE TABLE UnSparsed(ID INT IDENTITY(1,1),
    FirstCol INT,
    SecondCol VARCHAR(100),
    ThirdCol SmallDateTime…!!!!!!!!!!!!!!!)
    GO
    CREATE TABLE Sparsed(ID INT IDENTITY(1,1),
    FirstCol INT ,
    SecondCol VARCHAR(100) ,
    ThirdCol SmallDateTime SPARSE….!!!!!!!!!!!!!!!!!!1)
    GO
    set nocount on
    DECLARE @idx INT = 0
    WHILE @idx < 5000
    BEGIN
    INSERT INTO UnSparsed VALUES (4,'ff', null)
    INSERT INTO Sparsed VALUES (4, 'ff', null)
    SET @idx+=1
    END
    set nocount off
    GO
    sp_spaceused 'UnSparsed'
    GO
    sp_spaceused 'Sparsed'

    Reply
  • Piyush Sachan
    March 14, 2011 7:09 pm

    In my first comment ..second table have all three columns sparse

    Thanks & Regards
    Piyush Sachan

    Reply
  • it will be take lesser space if column have many null zero
    and else

    Reply
  • Thanks Pinal Dave,
    This blog is very helpful for SQL Developers to sparse column and regular coloumn.

    Reply
  • Harikesh Yadav
    August 25, 2011 5:35 pm

    sparse COLUMN is giving space benefit for NULL values
    But it is taking more space for zeros.

    Reply
  • I’m trying to use the following code to dynamically create a temp table with 2 numeric columns for each input lot returned from the included query. The query returns 364 rows so the code should create 728 SPARSE columns. When I try to run this code with the SPARSE column definition, I get the error:Cannot create a row of size 9563 which is greater than the allowable maximum row size of 8060, but if I remove the SPARSE code, the table is created/updated fine. Why can’t I create the table with SPARSE columns?

    IF (SELECT OBJECT_ID(‘tempdb..#tmpVintage’)) IS NOT NULL
    DROP TABLE #tmpVintage

    CREATE TABLE #tmpVintage(VntSeq INTEGER NULL,
    VntVintage VARCHAR(4) NOT NULL,
    VntTranDate DATETIME NOT NULL,
    VntIssueRcpt CHAR(1) NOT NULL,
    VntLotKey INTEGER NOT NULL,
    VntPodKey INTEGER NOT NULL,
    VntPodCount TINYINT NOT NULL,
    VntTagKey INTEGER NOT NULL,
    VntTranQty DECIMAL(18,5) NOT NULL,
    VntTranUOM VARCHAR(2) NOT NULL,
    VntTranRunQty DECIMAL(18,5) NULL,
    VntQty DECIMAL(18,5) NULL,
    VntRun DECIMAL(18,5) NULL,
    VntPct DECIMAL(18,5) NULL,
    VntSparseCols XML COLUMN_SET FOR ALL_SPARSE_COLUMNS)

    — ADD THE COLUMNS FOR EACH INPUT LOT – TO BE USED FOR KEEPING RUNNING TOTALS BY INPUT LOT
    SELECT @SQL =
    STUFF((SELECT DISTINCT
    ‘ALTER TABLE #tmpVintage ADD [VntRun’ + CAST(triInputLotKey AS VARCHAR) + ‘] DECIMAL(18,5) SPARSE NULL, [VntPct’ + CAST(triInputLotKey AS VARCHAR) + ‘] DECIMAL(18,5) SPARSE NULL ‘
    FROM #TraceInputs
    WHERE triInputLotKey 0
    ORDER BY ‘ALTER TABLE #tmpVintage ADD [VntRun’ + CAST(triInputLotKey AS VARCHAR) + ‘] DECIMAL(18,5) SPARSE NULL, [VntPct’ + CAST(triInputLotKey AS VARCHAR) + ‘] DECIMAL(18,5) SPARSE NULL ‘
    FOR XML PATH(”)
    ), 1, 0, ”)
    EXECUTE (@SQL)

    Reply
  • Hi Pinal,

    Sparse column with zero value is taking space similar to non-null value.Do we have an advantage of using zeroes for sparse columns?

    Thanks & Regards,
    Sreelekha

    Reply
  • Kathleen Steele
    November 29, 2011 2:12 am

    Hi – what is the difference between a null in a varchar and a null in a sparse varchar field. The varchar has 2 bytes of info and no space for the null, correct?

    Thanks!

    Reply
  • I am getting error like “incorrect syntax near sparse”
    when i try to create sparsed table.

    Reply
  • very nice.

    Reply
  • To shefalee –
    You are getting an error because you have executed in MS SQL Server 2005. you try in 2008, surely it will work.

    Reply
  • Hello sir,
    if we insert ‘NULL’ then SPARSE working fine, but if we insert 0 then it’s take more space compare to notsparse columns .
    plz tell me which one better

    wait your response

    Reply
  • venkat_t919
    May 8, 2013 8:07 pm

    Hi Pinal,

    I have seen the below code in so many examples, you are using for insert # records

    DECLARE @idx INT = 0
    WHILE @idx < 50000
    BEGIN
    INSERT INTO Sparsed VALUES (NULL,NULL, NULL)
    SET @idx+=1
    END
    GO

    instead of the above code we can use below code also – ct?

    INSERT INTO Sparsed VALUES (NULL,NULL, NULL)
    Go 50000

    if it's correct, may I know is there any difference between the above two quries..?

    Thanks,
    Venkat_t919

    Reply

Leave a Reply