未加星标

SQL SERVER Performance Benefit of Using SPARSE Columns?

字体大小 | |
[数据库(mssql) 所属分类 数据库(mssql) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

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:

SQL SERVER 2008 Introduction to SPARSE Columns SQL SERVER 2008 Introduction to SPARSE Columns Part 2 SQL SERVER Error Msg 511 using SQL Server Sparse columns SQL SERVER Errors and Limitations of Working with SPARSE Columns in SQL Server

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)
SQL SERVER   Performance Benefit of Using SPARSE Columns?

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 ( http://blog.sqlauthority.com )

本文数据库(mssql)相关术语:熊片数据库 mssql数据库 oracle数据库 pubmed数据库 access数据库 万方数据库

主题: SQLRIA360SQL ServerRIMRYSaaS
分页:12
转载请注明
本文标题:SQL SERVER Performance Benefit of Using SPARSE Columns?
本站链接:http://www.codesec.net/view/482027.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(mssql) | 评论(0) | 阅读(39)