Presentation is loading. Please wait.

Presentation is loading. Please wait.

Partition Switching Joe Tempel.

Similar presentations


Presentation on theme: "Partition Switching Joe Tempel."— Presentation transcript:

1 Partition Switching Joe Tempel

2 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

3 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

4 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: Old News (has been around since SQL 2005)

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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:

15 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 DataCreate Columnstore Index on StagingSwitch Data to Primary Table

16 Benefits Faster Less Logging
Minimizes the amount of time a lock is on the primary table

17 DEMO

18 Larger Scale Test Results Larger data set on spinning disk: Partition switching took 15% as long as DELETE and INSERT

19 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) } [ ; ]

20 DEMO

21 Additional Resources A ton of material on when partitioning is and is not advantageous


Download ppt "Partition Switching Joe Tempel."

Similar presentations


Ads by Google