SQL Server & PostgreSQL DBA & Dev Tips | MyTechMantra

How to Truncate Specific Partitions in SQL Server (Fast & Efficiently)

Learn how to use TRUNCATE TABLE WITH PARTITIONS in SQL Server 2016 to clear specific data ranges instantly with minimal logging. Faster than DELETE and SWITCH

TRUNCATE TABLE WITH PARTITIONS T-SQL Enhancement in SQL Server 2016

TRUNCATE TABLE WITH PARTITIONS is a T-SQL enhancement introduced in SQL Server 2016 that allows DBAs to instantly remove data from specific partitions without affecting the rest of the table. As a minimally logged DDL operation, it is significantly faster than a standard DELETE and simpler than SWITCH PARTITION for large-scale data purging.

Before You Begin:

  • Ensure you are on SQL Server 2016 or higher.
  • Verify that your table is Partitioned.
  • Backup Warning: While TRUNCATE is fast, it is not easily reversible without a log backup or snapshot.
Series

You are reading Part 1 of the T-SQL Enhancements in SQL Server 2016 for Developers and DBAs. View All 10 Parts

Continue exploring T-SQL Enhancements in SQL Server 2016 for Developers and DBAs:

Truncate Table WITH Partitions SQL Server

It is a well known fact that TRUNCATE TABLE is faster than DELETE statement. TRUNCATE TABLE SQL Server uses lesser system resources and transaction log file when compared to a DELETE statement.

Starting SQL Server 2016, Microsoft has introduced a new clause TRUNCATE TABLE WITH PARTITIONS SQL Server which can be used to truncate all rows available within a partition.

Comparison Table for DELETE Statement Vs SWITCH PARTITION Vs TRUNCATE PARTITION

FeatureDELETE StatementSWITCH PARTITIONTRUNCATE PARTITION
Log OverheadHigh (Fully Logged)MinimalMinimal
SpeedSlow (Row-by-row)Fast (Metadata)Fastest (Metadata)
ComplexitySimpleHigh (Requires Staging)Simple (Single Line)

Syntax: TRUNCATE TABLE WITH PARTITIONS

TRUNCATE TABLE [SchemaName].[TableName] 
WITH (PARTITIONS [Partition Number Expression] | [Range]);

Before you use TRUNCATE TABLE WITH PARTITION clause in SQL Server 2016 make sure all the indexes on the table are aligned with its partitions else the statement will fail.




TRUNCATE TABLE vs SWITCH PARTITION Performance

Historically, clearing data required SQL Server partition switching, a complex process involving creating a staging table, matching indexes exactly, and executing multiple metadata changes.

In contrast, TRUNCATE TABLE WITH PARTITIONS simplifies your workflow into a single, high-performance T-SQL command. It bypasses the need for temporary objects while maintaining the benefits of a minimally logged operation. This drastically reduces transaction log bloat and execution time, offering a more efficient alternative to the “Switch-Truncate-Drop” pattern for large-scale data lifecycle management.

Example Truncate Table with Partitions SQL Server

Execute the below sample code to remove Partitions 12, 14, 16, 17 and 18 in SQL Server 2016 using TRUNCATE TABLE WITH PARTITIONS SQL SERVER clause.

TRUNCATE TABLE Sales.TransactionHistory 
WITH (PARTITIONS (12, 14, 16 TO 18))
GO

Troubleshooting Aligned Indexes

Why does TRUNCATE TABLE WITH PARTITIONS fail?

The most common cause of failure is the presence of a non-aligned index. For this T-SQL enhancement to work, every non-clustered index must be created on the same partition scheme and partition function as the base table. If even one “global” index exists that isn’t partitioned, SQL Server will block the operation to maintain index integrity.

The Error Message:

Msg 3756, Level 16, State 1, Line 10 TRUNCATE TABLE statement failed because the table 'SalesHistory' has one or more non-aligned indexes.

How to Fix the Alignment Issue

To resolve this and successfully truncate your partition, you have three options:

  1. Align the Index: Drop and recreate the non-clustered index using the same partition scheme as the table.
  2. Disable the Index: Disable the non-aligned index before truncating and rebuild it afterward (though this may be slow for large datasets).
  3. Drop the Index: Permanently remove any non-essential global indexes that do not support the partitioning strategy.

The “Find Partition” Query: The code to find partition numbers

How to Identify Your Partition Number Before truncating, use this query to verify which partition ID corresponds to your data ranges:

SELECT 
    p.partition_number, 
    p.rows, 
    rv.value AS BoundaryValue
FROM sys.partitions p
LEFT JOIN sys.partition_range_values rv 
    ON p.partition_number = rv.boundary_id
WHERE p.object_id = OBJECT_ID('YourTableName');

Frequently Asked Questions (FAQs) on How to Truncate Specific Partitions in SQL Server (Fast & Efficiently)

1. Does TRUNCATE TABLE WITH PARTITIONS reset the Identity column?

Unlike a full table truncate, truncating a specific partition in SQL Server does not reset the IDENTITY seed value. The identity counter will continue to increment from the last global value. If you need to reset the seed, you must truncate the entire table or use DBCC CHECKIDENT.

2. Can I truncate multiple partitions in a single command?

Yes. SQL Server 2016 allows you to truncate multiple individual partitions or a range. For example, WITH (PARTITIONS (1, 3, 5 TO 7)) will clear partitions 1, 3, 5, 6, and 7 simultaneously.

3. Why do I get Msg 3756 when truncating a partition?

This error occurs because of non-aligned indexes. For a partition truncate to work, all non-clustered indexes must use the same partition scheme and function as the base table. If a “Global Index” exists that covers the whole table without partitioning, the command will fail.

4. Is TRUNCATE TABLE PARTITION faster than SWITCH PARTITION?

While both are metadata-only operations and nearly instantaneous, TRUNCATE TABLE WITH PARTITIONS is simpler. It removes the need to create a temporary staging table, manage index alignment on that staging table, and then drop it, saving you several lines of script and manual effort.

5. Does truncating a partition require a full table lock?

TRUNCATE TABLE always acquires a Schema Modification (Sch-M) lock on the table. While the data deallocation happens at the partition level, the Sch-M lock is global and will briefly prevent other sessions from accessing the table schema or performing DDL operations until the command completes.


Enjoyed this guide? Continue exploring the 2016 T-SQL Series:

Ashish Kumar Mehta

Ashish Kumar Mehta is a database manager, trainer and technical author. He has more than a decade of IT experience in database administration, performance tuning, database development and technical training on Microsoft SQL Server from SQL Server 2000 to SQL Server 2014. Ashish has authored more than 325 technical articles on SQL Server across leading SQL Server technology portals. Over the last few years, he has also developed and delivered many successful projects in database infrastructure; data warehouse and business intelligence; database migration; and upgrade projects for companies such as Hewlett-Packard, Microsoft, Cognizant and Centrica PLC, UK. He holds an engineering degree in computer science and industry standard certifications from Microsoft including MCITP Database Administrator 2005/2008, MCDBA SQL Server 2000 and MCTS .NET Framework 2.0 Web Applications.

Add comment

AdBlocker Message

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.


Newsletter Signup! Join 15,000+ Professionals




Be Social! Like & Follow Us

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.

Advertisement