<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=612681139262614&amp;ev=PageView&amp;noscript=1">
Skip to content

Need help? Talk to an expert: phone(904) 638-5743

Table Partitioning in SQL Server – Partition Switching

Table Partitioning in SQL Server – Partition Switching

sql-partitionin-switchingInserts, updates and deletes on large tables can be very slow and expensive, cause locking and blocking, and even fill up the transaction log. One of the main benefits of table partitioning is that you can speed up loading and archiving of data by using partition switching.

Partition switching moves entire partitions between tables almost instantly. It is extremely fast because it is a metadata-only operation that updates the location of the data, no data is physically moved. New data can be loaded to separate tables and then switched in, old data can be switched out to separate tables and then archived or purged. All data preparation and manipulation can be done in separate tables without affecting the partitioned table. 

Partition Switching Requirements
There are always two tables involved in partition switching. Data is switched from a source table to a target table. The target table (or target partition) must always be empty.

(The first time I heard about partition switching, I thought it meant “partition swapping“. I thought it was possible to swaptwo partitions that both contained data. This is currently not possible, but I hope it will change in a future SQL Server version.)

Partition switching is easy – as long as the source and target tables meet all the requirements :) There are many requirements, but the most important to remember are:

  • The source and target tables (or partitions) must have identical columns, indexes and use the same partition column
  • The source and target tables (or partitions) must exist on the same filegroup
  • The target table (or partition) must be empty

If all the requirements are not met, SQL Server is happy to tell you exactly what went wrong and provides detailed and informative error messages. Some of the most common examples are listed near the end of this blog post.

Partition Switching Examples
Partitions are switched by using the ALTER TABLE SWITCH statement. You ALTER the source table (or partition) and SWITCH to the target table (or partition). There are four ways to use the ALTER TABLE SWITCH statement:

  1. Switch from a non-partitioned table to another non-partitioned table
  2. Load data by switching in: Switch from a non-partitioned table to a partition in a partitioned table
  3. Archive data by switching out: Switch from a partition in a partitioned table to a non-partitioned table
  4. Switch from a partition in a partitioned table to a partition in another partitioned table

The following examples use code from the previous Table Partitioning Basics blog post. It is important to notice that these examples are meant to demonstrate the different ways of switching partitions, they do not create any indexes and they map all partitions to the [PRIMARY] filegroup. These examples are not meant to be used in real-world projects.

1. SWITCH FROM NON-PARTITIONED TO NON-PARTITIONED

The first way to use the ALTER TABLE SWITCH statement is to switch all the data from a non-partitioned table to an empty non-partitioned table:

Before switch:

sql-partitionin-switching-before-switch

After switch:

sql-partitionin-switching-after-switch

This is probably not used a lot, but it is a great way to start learning the ALTER TABLE SWITCH statement without having to create partition functions and partition schemes:

2. LOAD DATA BY SWITCHING IN: SWITCH FROM NON-PARTITIONED TO PARTITION

The second way to use the ALTER TABLE SWITCH statement is to switch all the data from a non-partitioned table to an empty specified partition in a partitioned table:

Before switch:

sql-partitioning2-switching-non-partition-to-partition-before

After switch:
sql-partitioning2-switching-non-partition-to-partition-after

This is usually referred to as switching in to load data into partitioned tables. The non-partitioned table must specify WITH CHECK constraints to ensure that the data can be switched into the specified partition:

3. ARCHIVE DATA BY SWITCHING OUT: SWITCH FROM PARTITION TO NON-PARTITIONED

The third way to use the ALTER TABLE SWITCH statement is to switch all the data from a specified partition in a partitioned table to an empty non-partitioned table:

Before switch:

sql-partitioning3-switching-partition-to-non-partition-before

After switch:

sql-partitioning3-switching-partition-to-non-partition-after

This is usually referred to as switching out to archive data from partitioned tables:

4. SWITCH FROM PARTITION TO PARTITION

The fourth way to use the ALTER TABLE SWITCH statement is to switch all the data from a specified partition in a partitioned table to an empty specified partition in another partitioned table:

Before switch:

sql-partitioning4-switching-partition-to-partition-before

After switch:
sql-partitioning4-switching-partition-to-partition-after-1

This can be used when data needs to be archived in another partitioned table:

Error messages
SQL Server provides detailed and informative error messages if not all requirements are met before switching partitions. You can see all messages related to ALTER TABLE SWITCH by executing the following query, it is also quite a handy requirements checklist:

Summary

sql-partitionin-switchingPartition switching moves entire partitions between tables almost instantly. New data can be loaded to separate tables and then switched in, old data can be switched out to separate tables and then archived or purged. There are many requirements for switching partitions. It is important to understand and test how partition switching works with filegroups, indexes and constraints.

 

This post is the second in a series of Table Partitioning in SQL Server blog posts. It covers the basics of partition switching. Future blog posts in this series will build upon this information and these examples to explain other and more advanced concepts.

Want to learn more from Cathrine? Check out her blog or follow her on Twitter!

Sign-up now and get instant access

Leave a comment

Free Trial

On-demand learning

Most Recent

private training

Hackathons, enterprise training, virtual monitoring