Partition Switching in Large Fact Tables Note from the Data Whisperer

By Tom Nonmacher

As data warehouses continue to balloon with the exponential rise in data generation, SQL Server admins are faced with the challenge of optimizing their environment for improved performance. One such optimization technique that has proven useful for large fact tables is partition switching. Introduced in SQL Server 2016, partition switching allows for the efficient movement of data between tables without the use of any explicit DELETE or INSERT commands, thereby reducing the system resources used and significantly improving performance.

Partition switching works by swapping partitions between two tables, provided that both tables have identical structures. The original table remains unchanged, while the second table is emptied or filled with data from the original table. This is particularly useful when you need to archive data or move older data to slower, cheaper storage.


-- Create a partition function 
CREATE PARTITION FUNCTION myDateRangePF (datetime)
AS RANGE RIGHT FOR VALUES ('20180101', '20180201', '20180301')
-- Create a partition scheme 
CREATE PARTITION SCHEME myDateRangePS
AS PARTITION myDateRangePF TO ([PRIMARY], [PRIMARY], [PRIMARY], [ARCHIVE])
-- Create a partitioned table 
CREATE TABLE myPartitionedTable (
    id int,
    date datetime,
    data varchar(100)
) ON myDateRangePS (date)

SQL Server 2017 introduced improvements to partition switching by allowing the SWITCH TO command to be used with tables that have different indexes. This gives more flexibility for scenarios where data archiving is required. However, be aware that this may impact the performance of the SWITCH TO operation.

MySQL 5.7 does not support partition switching directly. However, you can achieve a similar effect by renaming tables. It's important to note that this method is not as efficient as SQL Server's partition switching and may require additional steps to ensure data integrity. It's also worth mentioning that in DB2 11.1, you can take advantage of the ALTER TABLE command to move data between partitions.

Azure SQL Database, Microsoft's cloud-based database service, also supports partition switching. It's an excellent solution for managing large fact tables, particularly in scenarios where data needs to be archived or moved to slower storage. Given the pay-as-you-go nature of Azure SQL, partitioning can be a cost-effective way to manage your data.


-- Create a secondary table 
CREATE TABLE mySecondaryTable (
    id int,
    date datetime,
    data varchar(100)
) ON myDateRangePS (date)
-- Switch partition 
ALTER TABLE myPartitionedTable
SWITCH PARTITION 4 TO mySecondaryTable

In conclusion, partition switching is a powerful tool for managing large fact tables. It provides an efficient way to move data between tables without impacting system performance significantly. As with any tool, it's important to understand the implications and use it judiciously. Whether you are working with SQL Server, MySQL, DB2, or Azure SQL, understanding how to leverage partition switching can be a game-changer for your data management strategy.




FB2D03
Please enter the code from the image above in the box below.