Lesson 2 of 2 Table and Index Partitioning

Switching, Truncating and Sliding Partitions

This lesson explains partition switching and truncation, the sliding window pattern and partition-level maintenance.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the requirements for switching partitions.
  • Explain the steps of a sliding window.
  • Choose partition operations to archive or remove data quickly.

Switching and truncating partitions

ALTER TABLE source SWITCH PARTITION n TO target [PARTITION m] moves a partition's data between tables as a metadata change, in milliseconds regardless of size. The source and target must have identical columns, types and indexes, be on the same filegroup, and the target partition (or table) must be empty; when switching into a partitioned table, a CHECK constraint on the staging table must prove that its rows fit the target range. TRUNCATE TABLE ... WITH (PARTITIONS (2, 4 TO 6)) removes the data of specific partitions without logging each row.

-- Archive January: switch the partition out to an empty table with the same structure
ALTER TABLE Sales.Events SWITCH PARTITION 2 TO Sales.Events_Archive_2026_01;

-- Or simply remove it
TRUNCATE TABLE Sales.Events WITH (PARTITIONS (2));

-- Which partition holds a value?
SELECT $PARTITION.pfMonthly('2026-02-15') AS PartitionNumber;   -- 3
Select all that apply

Requirements for SWITCH

Exam skills 1.1.6

Identify what must be true to switch a partition out to an archive table.

Log in to try this activity Interactive activities are free with an account.

The sliding window

A sliding window keeps a fixed number of recent periods in a table. Each period, the same steps run in order, as the following sequence shows.

  1. Prepare the next period

    Set the scheme's NEXT USED filegroup (PRIMARY in Azure SQL Database) and run ALTER PARTITION FUNCTION ... SPLIT RANGE ('2026-04-01') while the new range is still empty.

  2. Load new data

    Bulk-load the new period into a staging table with a matching CHECK constraint, then switch it in, or insert directly into the empty partition.

  3. Remove the oldest period

    Switch the oldest partition out to an archive table, or truncate it.

  4. Merge the empty boundary

    Run ALTER PARTITION FUNCTION ... MERGE RANGE ('2026-01-01') to remove the now-empty oldest range.

Splitting or merging ranges that contain data moves rows and is fully logged, so these operations are always performed on empty partitions.

The following diagram shows the four steps of each period.

The sliding window on a partitioned table. Each period, the next range is split while it is still empty, the new period is loaded or switched in, the oldest partition is switched out to an archive table or truncated, and the now-empty oldest range is merged. Splitting and merging only empty ranges avoids moving rows, which would be fully logged.
Split and merge only empty ranges.
Put in order

A sliding window period

Exam skills 1.1.6

Put the monthly sliding window steps in order.

Log in to try this activity Interactive activities are free with an account.

Partition-level maintenance and statistics

Indexes can be rebuilt for one partition with ALTER INDEX ... REBUILD PARTITION = n, and data compression can differ per partition, such as PAGE compression for old partitions. Incremental statistics (STATISTICS_INCREMENTAL = ON) let statistics be updated for changed partitions only. sys.partitions and sys.dm_db_partition_stats show row counts per partition, which helps verify that data lands where expected.

Worked example

Removing old data quickly

A 4-billion-row telemetry table keeps 13 months of data. A nightly DELETE of the oldest month takes hours, fills the transaction log and blocks inserts. The table is partitioned by month and all indexes are aligned. What should replace the DELETE?

Truncate the oldest partition with TRUNCATE TABLE ... WITH (PARTITIONS (n)), or switch it out to an archive table if the data must be kept, and then merge the empty boundary. Both are metadata operations that finish in seconds with minimal logging. Deleting in smaller batches would reduce blocking but would still log every row, and rebuilding indexes does not remove data.

Check yourself

Why are SPLIT and MERGE run only on empty ranges in a sliding window?

Show answer

Splitting or merging ranges that contain data moves rows and is fully logged, which is slow; on empty ranges they are metadata operations.

Exam Tips

Remember for the exam

  • Remove a whole period: TRUNCATE TABLE ... WITH (PARTITIONS (n)) or SWITCH out.
  • SWITCH requires identical structures, aligned indexes and an empty target.
  • SPLIT and MERGE only empty ranges to avoid data movement.
  • Find a value's partition: $PARTITION.function(value).