Answer simple quiz at the end of the blog post and –
Every day one winner from India will get Joes 2 Pros Volume 3.
Every day one winner from United States will get Joes 2 Pros Volume 3.
Sparse Data
Fields with fixed length data types (e.g., int, money) always consume their allotted space irrespective of how much data the field actually contains. This is true even if the field is populated with a null.
Occasionally you will encounter a column in your database which is rarely used. For example, suppose you have a field called [Violation] in a table called [Employee] but very few employees have any violations – perhaps two or three for every 1000 employees. In this case, over 99% of the Violation field values are null. This would be an example of a sparsely populated field.
To demonstrate a sparsely populated field, we will create a simple table with the design shown here. Create and populate the dbo.Bonus table by running this code in the figures below.
Please note that INSERT statements demonstrated will use row constructors, which debuted as a new feature in SQL Server 2008. However, readers who are running SQL Server 2005 must write their INSERT statements using the alternate syntax shown in the lower portion of the last Figure.
Now look at all the records in the table. Since all fields contain fixed length data types (i.e., int, char(2), money), we could have accurately calculated the per row consumption before we even added any data to the table.
Each row of the Bonus table will consume 21 bytes based on the data below.
- Row Header – 4 bytes (true for all rows).
- Fixed Data – 14 bytes (BonusID is an int (4 bytes) and BonusAmount’s data type is money (8 bytes).
- Null Block (aka Null Bitmap) – 3 bytes
The nulls in the money field do not change the space consumption because fixed length data types always use the full amount of space allocated to them. At 21 bytes per row, 1000 rows of the Bonus table would require 21,000 bytes and fill up about 3 data pages (1 data page = 8060 bytes). Currently there are only three records in this table so just 1 data page of 8K has started to fill up.
Analyzing Space Used
A handy tool for checking the storage amount which an object occupies is sp_spaceused. The figure below shows the Bonus table passed into this stored procedure. We see the Bonus table contains three rows and its data has not yet exceeded its first 8 KB page.
If you added enough records the data space used would need more memory pages. Let’s run a loop to add 997 more records to the Bonus table. The first record populated will be row 4. This loop increments each subsequent BonusID value by 1, and the loop runs as long as the BonusID value is <=1000. Once the row containing BonusID 1000 has been entered into the table, the loop will end. One run the Bonus table contains 1000 records.
Since one row occupies 21 bytes, we know these 1000 rows will take up 21 KB of space and should fit within three data pages.
[21000 bytes/(8060 bytes/page) = 2.61 pages]
Since you can use half a data page SQL dedicated 3 data pages to the Bonus table. Let’s rerun the sp_spaceused sproc and confirm the number of data pages.
Using the Sparse Data Option
The sparse data option is a new SQL Server 2008 feature for fields you expect to be predominantly null. Using the sparse data option, you can instruct SQL Server to not have nulls consume space in sparsely populated fields.
To test this we’re going to delete the Bonus table and then re-create it using the same steps we took previously. The only difference will be that the BonusAmount field will be created using the sparse option. Recall we expect the BonusAmount field to contain very little actual data – most records will be null.
Recall we expect the BonusAmount field to contain very little actual data – most records will be null. The Bonus table now contains 1000 records and BonusAmount is a sparse field, but so far we don’t see any difference in the appearance of the table or the data.
Now let’s review how much space the Bonus table is consuming. Let’s confirm the space usage is less in this example. Had you not used the sparse option the consumption would have been 24KB like in our last example.
Note: If you want to setup the sample JProCo database on your system you can watch this video. For this post you will want to run the SQLArchChapter4.0Setup.sql script from Volume 3.
Question 17
You have two fields, of INT and MONEY, in your Bonus table. You have 1000 records and all instances of the money column are null. When you set up the money field, you used the Sparse option. How much space are the 1000 rows of the money field using?
- None
- 4000 bytes
- 8000 bytes
Do not forget to participate in special question over here: Pluralsight Giving Away Free Subscription to Quiz Participants
Rules:
Please leave your answer in comment section below with correct option, explanation and your country of resident.
Every day one winner will be announced from United States.
Every day one winner will be announced from India.
A valid answer must contain country of residence of answerer.
Please check my facebook page for winners name and correct answer.
Winner from United States will get Joes 2 Pros Volume 3.
Winner from India will get Joes 2 Pros Volume 3.
The contest is open till next blog post shows up at which is next day GTM+2.5.
Reference:Â Pinal Dave (https://blog.sqlauthority.com)
96 Comments. Leave new
correct Option is 1. None
because If the value of a column is NULL, it doesn’t consume space at all
Truptee
India
Correct option is: 2
2. 4000 bytes
As There are two field int(4 bytes) and money(8 Bytes) and there are total 1000 Rows.
money field is declared as sparse and it contains null value so it sapce is not added
So the result is 4(int) * 1000(rows) = 4000 Bytes
karan,
India
Answer is : Option 1 None
Country : India
Hi Sir,
The correct option is Option No 1 : None
Money column is created using sparse data option. Which instructs SQL Server to not have nulls consume space in sparsely populated fields.
So the other two options are not valid in the above question which asks how much space is consumed by the 1000 rows of the money field.
P.Anish Shenoy,
INDIA
Correct Answer: 1. None
Since the column is having Null values, designate it as SPARSE will not consume any space; however not-null values will required additional 4 bytes of storage space along with the normal space required by the data-type.
Sudeepta,
India.
Sparse columns are ordinary columns that have an optimized storage for null values.
Storing a null in a sparse column takes up no space at all.
SO
My answer is 1 (i.e.NONE)
Ans is 1) NONE
Answer – Option 1) None
Country – India
Ans is 1. None
Though money column is as sparse so it will not allocated any space for null.
Though all thousand column is null so this column will not use any space for storing data..
Partha
India
Option# 1 is right
INDIA
Option 1) is correct
1) None
As you have explained the money field will not contribute to the space used as it is all null with sparse option
Correct Option is 1. None.
Because using the sparse data option, we instruct SQL Server not to have nulls consume space in sparsely populated fields, therefore, when the money field was set up with the Sparse option, 1000 rows of the money field will not use any space.
(Sale, Nigeria)
Correct Answer is Option 1
Explanation: As it is described in question that table is created with sparse option for money field. So, it will not consume any space for null values in that field and all the values in that field are NULL. Hence, the space used by 1000 rows of money field is using 0 bytes of space.
Country – INDIA (Gujarat)
Option 1) is the correct answer i.e. none
Since money column is defined as sparse and it holds the null value in your example for all 1000 records, it doesn’t occupy any space.
I’m from UK.
Answer is #1 (none).
To prove, two tables are set-up: one with non-null int column + sparse money column, another with just the non-null int column. After adding 1000 rows to both, sp_spaceused says they match in size.
I am from USA.
Correct answer is #1
NULL = no space used
Using the sparse data option, you can instruct SQL Server to not have nulls consume space in sparsely populated fields.
USA
Mike Michalicek
The table will consume space, but the Money column will not consume any space by itself. Based on the way I read the question, the correct answer should be option 1 None.
Country: United States
The correct answer is number 1.
There will be no space consumed since the money field was created with the sparse option and all 1000 records are null.
Country: USA
If you are using sparse option, and sparse explanation say “Using the sparse data option, you can instruct SQL Server to not have nulls consume space in sparsely populated fields.”
So, the fields use 0 space.
The answer is Option 1 : None
Leonardo
Country: Chile
Correct answer is Option
1)None
Sparse column restrict to use additional memory space…..
Somnath Desai
India