How do you use the ALTER PARTITION SCHEME statement to move partitions to a different filegroup?
Posted by PaulAnd
Last Updated: July 29, 2024
In SQL Server, the ALTER PARTITION SCHEME statement is used to modify an existing partition scheme, which defines how the partitions of a partitioned table or index are mapped to filegroups. However, if you want to move partitions to a different filegroup, you cannot directly alter the partition scheme itself. Instead, you typically need to use a combination of steps involving the partitioned table or index and the ALTER PARTITION FUNCTION statement. Here is a general approach to moving a partition to a different filegroup: 1. Identify the Partition: First, determine which partition(s) needs to be moved and to which filegroup. 2. Create a New Filegroup: If the target filegroup doesn't already exist, you'll need to create it using the ALTER DATABASE statement. 3. Create a New Partition Scheme: Create a new partition scheme that assigns the specific partitions to the new filegroup. Here’s how you can do that:
ALTER PARTITION SCHEME YourNewPartitionScheme
   NEXT USED YourNewFileGroup;
4. Switch Partition: Use the ALTER TABLE ... SWITCH statement to switch the partitions between the original and the new partitioned table/index. 5. Drop Old Partition: If applicable, though caution is advised; you should be sure that any data moved is no longer needed in its original location before dropping it. Here's a simplified example to illustrate the key steps:
-- Assume we already have a partition scheme and function
CREATE PARTITION FUNCTION pf_MyPartitionFunction (int)
AS
    RANGE LEFT FOR VALUES (100, 200);

CREATE PARTITION SCHEME ps_MyPartitionScheme
AS
    PARTITION pf_MyPartitionFunction TO (FileGroup1, FileGroup2, FileGroup3);

-- Step 1 - Optional: Create new filegroup if it does not exist
ALTER DATABASE YourDatabase
ADD FILEGROUP NewFileGroup;

-- Step 2 - Create a new partition scheme for the new filegroup
CREATE PARTITION SCHEME ps_NewPartitionScheme
AS
    PARTITION pf_MyPartitionFunction TO (FileGroup1, NewFileGroup, FileGroup3);

-- Step 3 - Switch the partition to the new filegroup
BEGIN TRANSACTION;

-- Create a staging table with the same structure as your partitioned table
CREATE TABLE dbo.StagingTable (...); -- Define the structure

-- Insert data from the specific partition you want to move to the staging table
INSERT INTO dbo.StagingTable
SELECT *
FROM dbo.YourPartitionedTable
WHERE PartitionKeyColumn BETWEEN YourRangeLower AND YourRangeUpper; 

-- Now you can drop the old partition, or delete from the original table if required
DELETE FROM dbo.YourPartitionedTable
WHERE PartitionKeyColumn BETWEEN YourRangeLower AND YourRangeUpper;

-- Insert the data back into the new partitioned table using the new scheme
INSERT INTO dbo.YourPartitionedTable
SELECT *
FROM dbo.StagingTable;

-- Clean up
DROP TABLE dbo.StagingTable;

COMMIT TRANSACTION;
This method outlines a general approach for moving partitions within SQL Server, since the system does not provide direct support for moving partitions between filegroups without switching data around. Always remember to backup your data and test your scripts in a development environment before running on production data.
Related Content