Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 November SQL Server 2016: New COMPRESS and DECOMPRESS function for the Column level Compression

SQL Server 2016: New COMPRESS and DECOMPRESS function for the Column level Compression

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

SQL Server 2016 introduced the COMPRESS() and DECOMPRESS() function which we can use to compress column data of a table.

The Compression of the column is not a good always and not a good for all the column. For that, there are different ways for compression.

But we can use COMPRESS() on those columns which size are very big and which are not most in use.
The reason is, compressed column requires to be decompressed using DECOMPRESS() while selecting the data which will reduce query performance.

If we are facing a size related problem, we can think for COMPRESS() on those big size columns which are not most in use.

Please check the below demonstration on COMPRESS() and DECOMPRESS():

Create a sample table:

1
2
3
4
5
6
7
CREATE TABLE tbl_DumpData
(
ID INT
,RandomData NVARCHAR(MAX)
,CONSTRAINT pk_tbl_DumpData_ID PRIMARY KEY(ID)
)
GO

Insert dummy data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
;WITH CTE AS
(
SELECT 1 ID
UNION all
SELECT ID + 1
FROM CTE
WHERE ID + 1 <= 1000000
)
INSERT INTO tbl_DumpData(ID,RandomData)
SELECT
ID
,CONCAT(CAST(NEWID() AS NVARCHAR(MAX)), ' - I am follower of dbrnd.com') AS RandomData
FROM CTE
OPTION (MAXRECURSION 0)
GO

Create a sample table to store Compressed Data:

1
2
3
4
5
6
7
CREATE TABLE tbl_DumpData_Compressed
(
ID INT
,RandomData NVARCHAR(MAX)
,CONSTRAINT pk_tbl_DumpData_Compressed_ID PRIMARY KEY(ID)
)
GO

Insert Compressed data from the original table:

1
2
3
INSERT INTO tbl_DumpData_Compressed
SELECT ID, COMPRESS (RandomData) AS RandomData
FROM tbl_DumpData

Compare the size of both table:
You will get to know that, compressed table size is smaller than original table

1
2
3
4
SP_Spaceused 'tbl_DumpData'
GO
SP_Spaceused 'tbl_DumpData_Compressed'
GO

SQL Compress Decompress

Now, do DECOMPRESS and select your data:

1
2
3
4
SELECT
ID
,CAST(DECOMPRESS(RandomData) AS NVARCHAR(MAX)) AS RandomData
FROM tbl_DumpData_Compressed
Nov 6, 2017Anvesh Patel
SQL Puzzle: SQL Advance Query - Reduce duplicate column data by merging ID with itGreenplum: How to reset the Priority of Running Statement or Transaction?

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

November 6, 2017 SQL ServerAnvesh Patel, COMPRESS, database, database research and development, dbrnd, DECOMPRESS, SQL Query, SQL Server, SQL Server 2016, SQL Server Administrator, SQL Server Error, SQL Server Monitoring, SQL Server Performance Tuning, SQL Server Programming, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....