DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. SQL Server: Compress and Decompress

SQL Server: Compress and Decompress

Here's a walkthrough tutorial on using compression in SQL Server and other compression-related features.

Rajat Jaiswal user avatar by
Rajat Jaiswal
·
Oct. 27, 16 · Tutorial
Like (1)
Save
Tweet
Share
8.86K Views

Join the DZone community and get the full member experience.

Join For Free

You might be aware of the Gzip Compression algorithm. If not then try this link. 

So, SQL Server 2016 introduced these two awesome functions for compressing and decompressing your data.

Before SQL Server 2016, we had data compression features like page and row compression (I even wrote about those here), but those are different from this column value compression.

In the SQL Server 2016 compression function, data compression is done via the Gzip algorithm and returns VARBINARY(MAX).

Below is the simple syntax of Compress:

Compress (Expression)

Here, the expression can be nvarchar(n), nvarchar(max), varchar(n), varchar(max), varbinary(n), varbinary(max), char(n), nchar(n), or binary(n).

The Decompress function is just the opposite of Compress. It is used to decompress the value of VARBINARY, which is converted using the compression function. The only tweak you need to make is that you need to cast the output of Decompress in a specific data type to make it readable (if using varchar or nvarchar compression).

Decompress' syntax is:

Decompress (Compressed string)

Let’s understand this with an example.

Indiandotnet_Compress_Decompress_Feature_SQL

In this example, I have taken three tables with the exact same schema and data.

  1. IndiandotnetFriends.

  2. IndiandotnetFriends_Compress.

  3. IndiandotneFriends_Decompress.

You can see in the screenshot that we are inserting the same data. As the name suggests, in the first table, we have normal data from Adventureworks’ Person table. Now, in the second table, we are inserting the compressed value of FirstName, and in the third table, we are inserting decompressed value of FirstName from the compressed table.

Now, let’s check compress and decompress table data:

Check_Compressed_Decompress_Data

Now, you might be thinking that the output isn't readable. You're right. To make data readable from the Decompress table, we need to type cast.

See the picture below:

Decompressed_value

Now that we know how to use Compress and Decompress, let's go over the benefits. If you check out the picture below, you will, perhaps unsurprisingly, find that the compressed data is shorter than the normal and decompressed versions.

DataLength_Indiandotnet

 

Obviously, compression helps you somewhere in the overall performance of your application. The main benefit is that you can pass the compressed data to your .NET application and decompress it using GzipStream as well. The only thing which we need to take care of is type casting. Suppose your compressed base column is VARCHAR — then you need to typecast again in VARCHAR.

Now, the next question is where we can use these functions. We can use them to compress large objects, like binary data in which we save JPGs, PDFs, Word docs, etc. 

In any case, I hope you're excited to use these functions in your own work.

sql Database Data (computing)

Published at DZone with permission of Rajat Jaiswal, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Upgrade Guide To Spring Data Elasticsearch 5.0
  • How To Create and Edit Excel XLSX Documents in Java
  • Mr. Over, the Engineer [Comic]
  • Real-Time Stream Processing With Hazelcast and StreamNative

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: