New SQL Server 2016 Truncate Table With Partitions

I have a table with different partitions and would like to truncate rows in the particular partitions in a table rather than truncating whole table. How can we do this?

This task is fairly easy in sql server 2016. Microsoft introduced a new clause called “With Partitions” in truncate table command.

Let’s see how it works.

For the demonstration i have created a sample database “SamplePartition” with three Filegroups FG1, FG2 and FG3.

USE Master
GO

CREATE DATABASE SamplePartition
ON PRIMARY
(NAME='SamplePartition_1',
FILENAME=
'E:\PartitionDB\FG1\SamplePartition_1.mdf',
SIZE=2,
MAXSIZE=100,
FILEGROWTH=1 ),
FILEGROUP FG2
(NAME = 'SamplePartition_2',
FILENAME =
'E:\PartitionDB\FG2\SamplePartition_2.ndf',
SIZE = 2,
MAXSIZE=100,
FILEGROWTH=1 ),
FILEGROUP FG3
(NAME = 'SamplePartition_3',
FILENAME =
'E:\PartitionDB\FG3\SamplePartition_3.ndf',
SIZE = 2,
MAXSIZE=100,
FILEGROWTH=1 )
GO

Now, we have a database with three data files on three different file groups.

 Use SamplePartition
GO
-- Confirm Filegroups
SELECT name as [File Group Name]
FROM sys.filegroups
WHERE type = 'FG'
GO
-- Confirm Datafiles
SELECT name as [DB File Name],physical_name as [DB File Path]
FROM sys.database_files
where type_desc = 'ROWS'
GO

1

Create partition function

Use SamplePartition
GO
CREATE PARTITION FUNCTION salesYearPartitions (datetime)
AS RANGE RIGHT FOR VALUES ( '2015-01-01', '2016-01-01')
GO

Create Partition Scheme:

Use SamplePartition
GO
CREATE PARTITION SCHEME Test_PartitionScheme
AS PARTITION salesYearPartitions
TO ([PRIMARY], FG2, FG3 )
GO

Create Partition Table:

Use SamplePartition
GO
CREATE TABLE SalesArchival
(SaleTime datetime PRIMARY KEY,
ItemName varchar(50))
ON Test_PartitionScheme (SaleTime);
GO

Inserting data to partition table:

Use SamplePartition
GO
INSERT INTO SalesArchival (SaleTime, ItemName)
SELECT '2013-03-25','Item1' UNION ALL
SELECT '2014-10-01','Item2' UNION ALL
SELECT '2015-01-01','Item1' UNION ALL
SELECT '2015-08-09','Item3' UNION ALL
SELECT '2015-12-30','Item2' UNION ALL
SELECT '2016-01-01','Item1' UNION ALL
SELECT '2016-05-24','Item3'
GO

Check the data in different partitions

Use SamplePartition
GO
select partition_id, index_id, partition_number, Rows
FROM sys.partitions
WHERE OBJECT_NAME(OBJECT_ID)='SalesArchival'
GO

2

With this we have data spread across different partitions

Let us truncate partition number 2

TRUNCATE TABLE SalesArchival
WITH (PARTITIONS(2));

I have truncated the rows in partition 2. Let’s see the result

3.png

There you go! We have truncated the rows in partition 2.

If you would like to truncate the range of partitions you can give the range like this

TRUNCATE TABLE SalesArchival
WITH (PARTITIONS(2 to 3));
Restrictions:

You cannot use TRUNCATE TABLE on tables that:

  • Are referenced by a FOREIGN KEY constraint. (You can truncate a table that has a foreign key that references itself.)
  • Participate in an indexed view.
  • Are published by using transactional replication or merge replication.

References:

https://msdn.microsoft.com/en-us/library/ms177570.aspx 

How to create quick partition table with example

Hope you like the post!

Ramasankar Molleti

LinkedIn: LinkedIn Profile

Twitter: Twitter

 

 

Published by Ramasankar

As a Principal Cloud Architect with over 18 years of experience, I am dedicated to revolutionizing IT landscapes through cutting-edge cloud solutions. My expertise spans Cloud Architecture, Security Architecture, Solution Design, Cloud Migration, Database Transformation, Development, and Big Data Analytics.Currently, I spearhead cloud initiatives with a focus on Infrastructure, Containerization, Security, Big Data, Machine Learning, and Artificial Intelligence. I collaborate closely with development teams to architect, build, and manage robust cloud ecosystems that drive business growth and technological advancement.Core Competencies: • Cloud Platforms: AWS, Google Cloud Platform, Microsoft Azure • Technologies: Kubernetes, Serverless Computing, Microservices • Databases: MS SQL Server, PostgreSQL, Oracle, MongoDB, Amazon Redshift, DynamoDB, Aurora • Industries: Finance, Retail, Manufacturing. Throughout my career, I’ve had the privilege of working with industry leaders such as OCC, Gate Gourmet, Walgreens, and Johnson Controls, gaining invaluable insights across diverse sectors.As a lifelong learner and knowledge sharer, I take pride in being the first in my organization to complete all major AWS certifications. I am passionate about mentoring and guiding fellow professionals in their cloud journey, fostering a culture of continuous learning and innovation.Let’s connect and explore how we can leverage cloud technologies to transform your business: • LinkedIn: https://www.linkedin.com/in/ramasankar-molleti-23b13218/ • Book a mentorship session: [1:1] Together, let’s architect the future of cloud computing and drive technological excellence. Disclaimer The views expressed on this website/blog are mine alone and do not reflect the views of my company. All postings on this blog are provided “AS IS” with no warranties, and confers no rights. The owner of https://ramasankarmolleti.com will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of this information.

Leave a comment