SQL Server Partitioning an Active Table with Truncate

By | In SQL Server | September 01st, 2016

There are a number of interesting capabilities that get added in almost every new version of SQL Server for performance and scalability needs. As these features get added, they are often underappreciated and sometimes hardly used at all by many developers. One such feature that I have been tracking for close to ten years is Partitioning with SQL Server. When it first surfaced in SQL Server 2005, there was enough hype around this capability that it matured, eventually becoming one of the silver bullets I use with my customers. For mission critical large databases, this is a killer feature everyone should explore.

If you ask me, when SQL Server 2016 was released, I didn’t think there was much that could be done to enhance the capabilities of Partitioning. But in the most recent version, there has been an interesting addition that was added –the ability to Truncate a specific partition.

Why is this important? The premise for this feature is that we have the unique ability to partition data based on some state / date range. Many times it is important to remove unwanted data that has become stale after a period of time. A typical example is to remove the transaction records from a table which contains data more than three years old. If these data are not used or can ever change, DBAs always want an easy way to remove these records without using the DELETE statement. This is because the DELETE statement would bloat the Log files and if the amount of data is large, the table can easily get locked out blocking queries running on the table.

This is where partitioning comes in. Personally, I see this as an awesome addition to SQL Server 2016 that makes such critical processes seamless from an archival / removing dead data requirements.

Let me show you the construct you can use to visualize how this works. Here, I am showing you the number of rows in various partitions and then after the truncate statement, we can see only a specific partition rows have been cleared.

The output for this would look like:

Truncate Table

In this example, we have cleared just a single partition. The same can be done to clear multiple partitions using the following construct.

If you have continuous partitions to be cleared, we can do this by using a range type of construct. A typical script for this would look like:

As you can see there are a number of ways we can use the TRUNCATE with Partitioned Tables in SQL Server 2016. I am sure you will find it useful and use it in your environments too, as you upgrade to SQL Server 2016.

Datavail Script: Terms & Conditions

By using this software script (“Script”), you are agreeing to the following terms and condition, as a legally enforceable contract, with Datavail Corporation (“Datavail”). If you do not agree with these terms, do not download or otherwise use the Script. You (which includes any entity whom you represent or for whom you use the Script) and Datavail agree as follows:

1. CONSIDERATION. As you are aware, you did not pay a fee to Datavail for the license to the Script. Consequently, your consideration for use of the Script is your agreement to these terms, including the various waivers, releases and limitations of your rights and Datavail’s liabilities, as setforth herein.

2. LICENSE. Subject to the terms herein, the Script is provided to you as a non-exclusive, revocable license to use internally and not to transfer, sub-license, copy, or create derivative works from the Script, not to use the Script in a service bureau and not to disclose the Script to any third parties. No title or other ownership of the Script (or intellectual property rights therein) is assigned to you.

3. USE AT YOUR OWN RISK; DISCLAIMER OF WARRANTIES. You agree that your use of the Script and any impacts on your software, databases, systems, networks or other property or services are solely and exclusively at your own risk. Datavail does not make any warranties, and hereby expressly disclaims any and all warranties, implied or express, including without limitation, the following: (1) performance of or results from the Script, (2) compatibility with any other software or hardware, (3) non-infringement or violation of third party’s intellectual property or other property rights, (4) fitness for a particular purpose, or (5) merchantability.

4. LIMITATION ON LIABILITY; RELEASE. DATAVAIL SHALL HAVE NO, AND YOU WAIVE ANY, LIABILITY OR DAMAGES UNDER THIS AGREEMENT.

You hereby release Datavail from any claims, causes of action, losses, damages, costs and expenses resulting from your downloading or other use of the Script.

5. AGREEMENT. These terms and conditions constitute your complete and exclusive legal agreement between you and Datavail.

Contact Us
Eric Russo
Senior Vice President of Database Services
Eric Russo is SVP of Database Services overseeing all of Datavail’s database practices including project and managed services for MS SQL, Oracle, Oracle EBS, MySQL, MongoDB, SharePoint and DB2. He is also the Product Owner for Datavail Delta, a database monitoring tool. He has 21 years’ experience in technology including 16 years in database management. His management success and style has attracted top DBAs from around the world to create one of the most talented and largest SQL Server teams. He has been with Datavail since 2008: previous to that his work experiences include DBA Manager at StrataVia, Senior Web Developer at Manifest Information Systems and SQL Server DBA at Clark County, Nevada.

Leave a Reply

Your email address will not be published.
Required fields are marked (*).