Partition Switching Joe Tempel
Who Am I? Joe Tempel Degree in Environmental Science IT professional for 19+ years Consultant for Digineer (a MN based consulting firm) for 5 years Many Hats Developer, Data Architect, Product Manager, Business Analyst… Interests (Other than SQL Server) Running Trekking and Rock Climbing Cooking Craft Beers
What is Partitioning? Data in a table is split into groups horizontally Groups of rows are mapped into individual partitions Data in partitioned tables can be spread across more than one filegroup in a database Requires a partition function and a partition scheme Function defines the datatype and values used as dividing lines for the partitions Scheme defines where each partition will be stored
What is Partition Switching? When your data is in partitioned tables, you can use the Transact-SQL ALTER TABLE...SWITCH statement to quickly and efficiently transfer subsets of your data in the following ways: Assigning a table as a partition to an already existing partitioned table. Switching a partition from one partitioned table to another. Reassigning a partition to form a single table. SOURCE: https://technet.microsoft.com/en-us/library/ms191160(v=sql.105).aspx Old News (has been around since SQL 2005)
How Does it Work? If I have tables with the identical data structure that use the same partition scheme, I can move data using partition switching. Table_Staging 1 2 3 4 5 6 7 Table 1 2 3 4 5 6 7 Table_Delete 1 2 3 4 5 6 7 Contains Data Empty Partition
How Does it Work? I can load and modify data in a staging table… Table_Staging 1 2 3 4 5 6 7 Table 1 2 3 4 5 6 7 Table_Delete 1 2 3 4 5 6 7 Contains Data Empty Partition
How Does it Work? …issue a switch command… Table_Staging 1 2 3 4 5 6 7 Table 1 2 3 4 5 6 7 Table_Delete 1 2 3 4 5 6 7 ALTER TABLE [Table_Staging] SWITCH PARTITION 7 to [Table] PARTITION 7; Contains Data Empty Partition
How Does it Work? …and the data will be ‘moved’ instantly. Table_Staging 1 2 3 4 5 6 7 Table 1 2 3 4 5 6 7 Table_Delete 1 2 3 4 5 6 7 Contains Data Empty Partition
How Does it Work. I can only switch data into an empty partition How Does it Work? I can only switch data into an empty partition. If I stage data that I want to replace existing data with, I need to make room. Table_Staging 1 2 3 4 5 6 7 Table 1 2 3 4 5 6 7 Table_Delete 1 2 3 4 5 6 7 Contains Data Empty Partition
How Does it Work? I can switch data out of my primary table… Table_Staging 1 2 3 4 5 6 7 Table 1 2 3 4 5 6 7 Table_Delete 1 2 3 4 5 6 7 ALTER TABLE [Table] SWITCH PARTITION 1 to [Table_Delete] PARTITION 1; SWITCH PARTITION 2 to [Table_Delete] PARTITION 2; Contains Data Empty Partition
How Does it Work? …switch the data from staging to my primary table… Table_Staging 1 2 3 4 5 6 7 Table 1 2 3 4 5 6 7 Table_Delete 1 2 3 4 5 6 7 ALTER TABLE [Table_Staging] SWITCH PARTITION 1 to [Table] PARTITION 1; SWITCH PARTITION 2 to [Table] PARTITION 2; Contains Data Empty Partition
How Does it Work? …and then truncate Table_Delete… Table_Staging 1 2 3 4 5 6 7 Table 1 2 3 4 5 6 7 Table_Delete 1 2 3 4 5 6 7 TRUNCATE TABLE Table_Delete; Contains Data Empty Partition
How Does it Work? …done. Table_Staging 1 2 3 4 5 6 7 Table 1 2 3 4 5 6 7 Table_Delete 1 2 3 4 5 6 7 Contains Data Empty Partition
A Few Notes Staging and delete tables do not need to be partitioned (if you work with data that does not span partitions in the primary table) Before you can switch data into a table with indexes, you need to make sure that the source table has identical indexes. You can switch data from an indexed table to an unindexed table (Table_Delete from our example does not need to be index aligned) For additional requirements see: https://technet.microsoft.com/en-us/library/ms191160(v=sql.105).aspx
Why Would I Use Partition Switching? Data Warehouse updates Archiving old data Updating a SQL 2012 table with a columnstore index Drop Columnstore Index on Staging Stage DataCreate Columnstore Index on StagingSwitch Data to Primary Table
Benefits Faster Less Logging Minimizes the amount of time a lock is on the primary table
DEMO
Larger Scale Test Results Larger data set on spinning disk: Partition switching took 15% as long as DELETE and INSERT
SPLIT and MERGE You can increase the number of partitions by using SPLIT You can decrease the number of partitions by using MERGE ALTER PARTITION FUNCTION partition_function () { SPLIT RANGE (boundary_value) | MERGE RANGE (boundary_value) } [ ; ]
DEMO
Additional Resources http://www.brentozar.com/sql/table-partitioning-resources/ A ton of material on when partitioning is and is not advantageous https://technet.microsoft.com/en-us/library/ms191160(v=sql.105).aspx https://msdn.microsoft.com/en-us/library/ms190787.aspx