Site sponsored by: Idera The gold standard of SQL Server performance monitoring & diagnostics.
SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Quiz
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


Article Topics

All Articles
Performance Tuning
Audit
Business Intelligence
Clustering
Reporting Services
Developer
General DBA
ASP.NET / ADO.NET

Write for Us

Share your SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

Recover Data Using Database Snapshots
Analyze and Fix Index Fragmentation in SQL Server 2008
Powerful Geographical Visualisations made easy with SQL 2008 Spatial (Part 2) ...
Backup User Databases Using a Maintenance Plan

More     
 
Latest FAQ's

How to alter a User Defined Data Type?
How to unzip a File in SSIS?
How to view previous query plans?
ALTER TABLE SWITCH statement failed because the object '%.*ls' is not ...

More     
   
Latest Software Reviews

Spotlight on ApexSQL Doc 2008
ApexSQL Enforce
Embarcadero Change Manager
SQL Server DBA Dashboard

More     

articles >> general dba >> Using Column Sets with Sparse Columns ...

Using Column Sets with Sparse Columns

By : Dinesh Priyankara
Jul 14, 2008

Although the cost of storage is comparatively low, we still need to consider saving space by using various techniques such as compression and archiving.  When you think of space savings the first thing you think of is the file system however space savings can be applied to the databases as well.  When we create a database we ensure that the data files are created with the appropriate size and growth rate. We periodically analyze the sizes of our databases and perform shrinking operations. We may be performing these tasks for different purposes, but in a way, these tasks help us to ensure optimal storage for our databases. Microsoft SQL Server provides us various techniques for reducing the space used by a database. SQL Server 2008 introduces a technique for addressing nullable columns that provides optimal storage for nullable columns. This new feature in SQL Server 2008 is known as SPARSE columns. This article does not cover much about the features of SPARSE columns, but it does cover the usage of sparse columns with Column Sets, and the things you need to know and consider when using them.

This article contains:
What is a SPARSE Column?
What is a Column Set?
Inserting and updating data in a column set
Tracking modifications with triggers
Applying security on a column set

What is a Sparse Column?
The sparse column is an ordinary column just like other columns but it reduces the storage requirement for null values. A nullable column can be made as sparse column by adding the SPARSE keyword when the table is created or altered. Once the column is a SPARSE column SQL Server will not allocate space for null values. Note that in using this feature it adds an overhead for data retrieval of non-null values. Therefore you need to carefully apply this feature for columns by calculating the space that can be saved. It is recommended to make the column a SPARSE column only if the space that could be saved is at least 20 to 40 percent. BOL provides a table that contains the required null percentages in the columns for each data type in order to make the columns a SPARSE column. Refer to the article "Using Sparse Columns" in BOL for more info.

What is a Column Set?
The Column set is a column that represents all the sparse columns added to the table as a xml-typed column. It is not physically available in the table, it is just like a calculated column but it allows you to make modifications on it. It is recommended to have a column set only if you have lot of sparse columns in you table because it speeds up the modifications and the retrieval if the column set is used instead of the sparse columns individually. You can get more information about column sets if you refer to "Using Column Set" in BOL.

The following code shows the way of creating a column set for a table.

Code 1:  Creating a table with sparse columns and a column set.

CREATE TABLE [dbo].[Customers]
(
    [Id] int PRIMARY KEY,
    [FirstName] varchar(50) NOT NULL,
    [LastName] varchar(50) NOT NULL,
    [Gender] bit SPARSE NULL, -- 1 = male, 2 = female
    [Telephone] varchar(15) SPARSE NULL,
    [MonthlyIncome] money SPARSE NULL,
    [Comments] varchar(1000) SPARSE NULL
    [AllSparseColumns] xml COLUMN_SET FOR ALL_SPARSE_COLUMNS
)

I have added the SPARSE keywords for all nullable columns, but as I mentioned earlier, the percentage of null values should be analyzed before making them sparse columns. Note that you need to add this column when you create the table. SQL Server does not allow you to add a column set by using the ALTER TABLE statement, but it does allows you to have a column set column without having sparse columns. The columns that are added later as sparse columns will be available with the added column set. See the code given below;

Code 2:  Creating a table with a column set, without making any columns as sparse columns.

-- adding column set without sparse columns
CREATE TABLE [dbo].[Customers_1]
(
    [Id] int PRIMARY KEY,
    [FirstName] varchar(50) NOT NULL,
    [LastName] varchar(50) NOT NULL,
    [Gender] bit NULL, -- 1 = male, 2 = female
    [Telephone] varchar(15) NULL,
    [MonthlyIncome] money NULL,
    [Comments] varchar(1000) NULL,
    [AllSparseColumns] xml COLUMN_SET FOR ALL_SPARSE_COLUMNS
)

-- inserting a record
INSERT INTO dbo.Customers_1
    ([Id], [FirstName], [LastName], [Gender], [Telephone], [MonthlyIncome], [Comments])
VALUES
    (1, 'Dinesh', 'Priyankara', 1, '777395871', 20000, 'no comments')

-- this returns null
SELECT AllSparseColumns FROM dbo.Customers_1

-- Make the Gender column as a sparse column
ALTER TABLE [dbo].[Customers_1]
    ALTER COLUMN [Gender] bit SPARSE NULL
GO

-- Make the Telephone column as a sparse column
ALTER TABLE [dbo].[Customers_1]
    ALTER COLUMN [Telephone] varchar(15) SPARSE NULL

-- Now it returns values of sparse columns as a xml
SELECT AllSparseColumns FROM dbo.Customers_1


    Next Page>>    








Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | QDPMA Performance Tuning | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 1999-2008 by T10 Media. All rights reserved