CREATE PARTITION FUNCTION Left_Partition (int) AS RANGE LEFT FOR VALUES (1,10,100) CREATE PARTITION FUNCTION Right_Partition (int) AS RANGE RIGHT FOR VALUES (1,10,100)
SELECT $PARTITION.Left_Partition (10) SELECT $PARTITION.Right_Partition (10)
--Place all partitions into the PRIMARY filegroup CREATE PARTITION SCHEME Primary_Left_Scheme AS PARTITION Left_Partition --Partition must currently exist in database ALL TO ([PRIMARY]) --Place all partitions into the different filegroups CREATE PARTITION SCHEME Different_Left_Scheme AS PARTITION Left_Partition --Partition must currently exist in database TO (Filegroup1, Filegroup2, Filegroup3, Filegroup4) --Filegroups must currently exist in database --Place multiple partitions into the different filegroups CREATE PARTITION SCHEME Multiple_Left_Scheme AS PARTITION Left_Partition --Partition must currently exist in database TO (Filegroup1, Filegroup2, Filegroup1, Filegroup2) --Filegroups must currently exist in database
--Prepare database IF OBJECT_ID('Partitioned_Table') IS NOT NULL DROP TABLE Partitioned_Table GO IF EXISTS(SELECT [name] FROM sys.partition_schemes WHERE [name] = 'Primary_Left_Scheme') DROP PARTITION SCHEME Primary_Left_Scheme GO IF EXISTS(SELECT [name] FROM sys.partition_functions WHERE [name] = 'Left_Partition') DROP PARTITION FUNCTION Left_Partition GO --Create partitioned table CREATE PARTITION FUNCTION Left_Partition (int) AS RANGE LEFT FOR VALUES (1,10,100) --Place all partitions into the PRIMARY filegroup CREATE PARTITION SCHEME Primary_Left_Scheme AS PARTITION Left_Partition ALL TO ([PRIMARY]) CREATE TABLE Partitioned_Table ( col1 INT ,col2 VARCHAR(15) ) ON Primary_Left_Scheme (col1) --Determine where values will be placed (this is not required) --You should try to do this before executing the code SELECT $PARTITION.Left_Partition (1) SELECT $PARTITION.Left_Partition (2) SELECT $PARTITION.Left_Partition (3) SELECT $PARTITION.Left_Partition (4) SELECT $PARTITION.Left_Partition (10) SELECT $PARTITION.Left_Partition (11) SELECT $PARTITION.Left_Partition (12) SELECT $PARTITION.Left_Partition (13) SELECT $PARTITION.Left_Partition (14) SELECT $PARTITION.Left_Partition (100) SELECT $PARTITION.Left_Partition (101) SELECT $PARTITION.Left_Partition (102) SELECT $PARTITION.Left_Partition (103) SELECT $PARTITION.Left_Partition (104) --Insert data into partitioned table INSERT INTO Partitioned_Table VALUES (1,'Description') INSERT INTO Partitioned_Table VALUES (2,'Description') INSERT INTO Partitioned_Table VALUES (3,'Description') INSERT INTO Partitioned_Table VALUES (4,'Description') INSERT INTO Partitioned_Table VALUES (10,'Description') INSERT INTO Partitioned_Table VALUES (11,'Description') INSERT INTO Partitioned_Table VALUES (12,'Description') INSERT INTO Partitioned_Table VALUES (13,'Description') INSERT INTO Partitioned_Table VALUES (14,'Description') INSERT INTO Partitioned_Table VALUES (100,'Description') INSERT INTO Partitioned_Table VALUES (101,'Description') INSERT INTO Partitioned_Table VALUES (102,'Description') INSERT INTO Partitioned_Table VALUES (103,'Description') INSERT INTO Partitioned_Table VALUES (104,'Description') --View the distribution of data in the partitions SELECT ps.partition_number ,ps.row_count FROM sys.dm_db_partition_stats ps INNER JOIN sys.partitions p ON ps.partition_id = p.partition_id AND p.[object_id] = OBJECT_ID('Partitioned_Table')
--Determine where values live before new partition SELECT $PARTITION.Left_Partition (501) --should return a value of 4 --Create new partition ALTER PARTITION FUNCTION Left_Partition () SPLIT RANGE(500) --Determine where values live after new partition SELECT $PARTITION.Left_Partition (501) --should return a value of 5
--Determine where values live after new partition SELECT $PARTITION.Left_Partition (5) --should return a value of 2 SELECT $PARTITION.Left_Partition (1) --should return a value of 1 SELECT $PARTITION.Left_Partition (10) --should return a value of 2 --Add new filegroups to the partitioning scheme ALTER PARTITION SCHEME Primary_Left_Scheme NEXT USED [PRIMARY] --Create new partition ALTER PARTITION FUNCTION Left_Partition () SPLIT RANGE(5) --Determine where values live after new partition SELECT $PARTITION.Left_Partition (5) --should return a value of 2 SELECT $PARTITION.Left_Partition (1) --should return a value of 1 SELECT $PARTITION.Left_Partition (10) --should return a value of 3
--Prepare database IF OBJECT_ID('multiple_partition') IS NOT NULL DROP TABLE multiple_partition GO IF EXISTS(SELECT [name] FROM sys.partition_schemes WHERE [name] = 'Primary_Left_Scheme') DROP PARTITION SCHEME Primary_Left_Scheme GO IF EXISTS(SELECT [name] FROM sys.partition_functions WHERE [name] = 'Left_Partition') DROP PARTITION FUNCTION Left_Partition GO --Create partitioned table CREATE PARTITION FUNCTION Left_Partition (int) AS RANGE LEFT FOR VALUES (1,10,100) --Place all partitions into the PRIMARY filegroup CREATE PARTITION SCHEME Primary_Left_Scheme AS PARTITION Left_Partition --Partition must currently exist in database ALL TO ([PRIMARY]) CREATE TABLE multiple_partition ( col1 INT PRIMARY KEY CLUSTERED ,col2 VARCHAR(15) ) ON Primary_Left_Scheme (col1) INSERT INTO multiple_partition VALUES (1,'Description') INSERT INTO multiple_partition VALUES (2,'Description') INSERT INTO multiple_partition VALUES (3,'Description') INSERT INTO multiple_partition VALUES (4,'Description') INSERT INTO multiple_partition VALUES (10,'Description') INSERT INTO multiple_partition VALUES (11,'Description') INSERT INTO multiple_partition VALUES (12,'Description') INSERT INTO multiple_partition VALUES (13,'Description') INSERT INTO multiple_partition VALUES (14,'Description') INSERT INTO multiple_partition VALUES (100,'Description') INSERT INTO multiple_partition VALUES (101,'Description') INSERT INTO multiple_partition VALUES (102,'Description') INSERT INTO multiple_partition VALUES (103,'Description') INSERT INTO multiple_partition VALUES (104,'Description') --Verify partitions SELECT OBJECT_NAME(ps.[object_id]) ,ps.partition_number ,ps.row_count FROM sys.dm_db_partition_stats ps INNER JOIN sys.partitions p ON ps.partition_id = p.partition_id AND p.[object_id] = OBJECT_ID('multiple_partition') --Check where data would be placed SELECT $PARTITION.Left_Partition (1) SELECT $PARTITION.Left_Partition (10) SELECT $PARTITION.Left_Partition (100) SELECT $PARTITION.Left_Partition (101) --Merge two partitions ALTER PARTITION FUNCTION Left_Partition() MERGE RANGE (10) --Verify partitions SELECT OBJECT_NAME(ps.[object_id]) ,ps.partition_number ,ps.row_count FROM sys.dm_db_partition_stats ps INNER JOIN sys.partitions p ON ps.partition_id = p.partition_id AND p.[object_id] = OBJECT_ID('multiple_partition') --Check where data would be placed SELECT $PARTITION.Left_Partition (1) SELECT $PARTITION.Left_Partition (10) SELECT $PARTITION.Left_Partition (100) SELECT $PARTITION.Left_Partition (101)
--Prepare database IF OBJECT_ID(‘multiple_partition’) IS NOT NULL DROP TABLE multiple_partition GO IF OBJECT_ID(‘single_partition’) IS NOT NULL DROP TABLE single_partition GO IF EXISTS(SELECT [name] FROM sys.partition_schemes WHERE [name] = ‘Primary_Left_Scheme’) DROP PARTITION SCHEME Primary_Left_Scheme GO IF EXISTS(SELECT [name] FROM sys.partition_functions WHERE [name] = ‘Left_Partition’) DROP PARTITION FUNCTION Left_Partition GO --Create single partition table CREATE TABLE single_partition ( col1 INT PRIMARY KEY CLUSTERED ,col2 VARCHAR(15) ) --Table must have a CHECK Constraint ALTER TABLE single_partition WITH CHECK ADD CONSTRAINT CK_single_partition CHECK (col1 > 100) INSERT INTO single_partition VALUES (101,’Description’) INSERT INTO single_partition VALUES (102,’Description’) INSERT INTO single_partition VALUES (103,’Description’) INSERT INTO single_partition VALUES (104,’Description’) --Create partitioned table CREATE PARTITION FUNCTION Left_Partition (int) AS RANGE LEFT FOR VALUES (1,10,100) --Place all partitions into the PRIMARY filegroup CREATE PARTITION SCHEME Primary_Left_Scheme AS PARTITION Left_Partition --Partition must currently exist in database ALL TO ([PRIMARY]) CREATE TABLE multiple_partition ( col1 INT PRIMARY KEY CLUSTERED ,col2 VARCHAR(15) ) ON Primary_Left_Scheme (col1) INSERT INTO multiple_partition VALUES (1,’Description’) INSERT INTO multiple_partition VALUES (2,’Description’) INSERT INTO multiple_partition VALUES (3,’Description’) INSERT INTO multiple_partition VALUES (4,’Description’) INSERT INTO multiple_partition VALUES (10,’Description’) INSERT INTO multiple_partition VALUES (11,’Description’) INSERT INTO multiple_partition VALUES (12,’Description’) INSERT INTO multiple_partition VALUES (13,’Description’) INSERT INTO multiple_partition VALUES (14,’Description’) INSERT INTO multiple_partition VALUES (100,’Description’) --Verify partitions SELECT OBJECT_NAME(ps.[object_id]) ,ps.partition_number ,ps.row_count FROM sys.dm_db_partition_stats ps INNER JOIN sys.partitions p ON ps.partition_id = p.partition_id AND p.[object_id] IN (OBJECT_ID(‘multiple_partition’), OBJECT_ ID(‘single_partition’)) --Move the single table into the partitioned table ALTER TABLE single_partition SWITCH TO multiple_partition PARTITION 4 --Verify partitions SELECT OBJECT_NAME(ps.[object_id]) ,ps.partition_number ,ps.row_count FROM sys.dm_db_partition_stats ps INNER JOIN sys.partitions p ON ps.partition_id = p.partition_id AND p.[object_id] IN (OBJECT_ID(‘multiple_partition’), OBJECT_ID(‘single_partition’))
--Prepare database IF OBJECT_ID('active_data') IS NOT NULL DROP TABLE active_data GO IF OBJECT_ID('archive_data') IS NOT NULL DROP TABLE archive_data GO IF EXISTS(SELECT [name] FROM sys.partition_schemes WHERE [name] = 'Active_Scheme') DROP PARTITION SCHEME Active_Scheme GO IF EXISTS(SELECT [name] FROM sys.partition_functions WHERE [name] = 'Active_Partition') DROP PARTITION FUNCTION Active_Partition GO IF EXISTS(SELECT [name] FROM sys.partition_schemes WHERE [name] = 'Archive_Scheme') DROP PARTITION SCHEME Archive_Scheme GO IF EXISTS(SELECT [name] FROM sys.partition_functions WHERE [name] = 'Archive_Partition') DROP PARTITION FUNCTION Archive_Partition GO --Create active function CREATE PARTITION FUNCTION Active_Partition (int) AS RANGE LEFT FOR VALUES (1,10,100) --Create archive function CREATE PARTITION FUNCTION Archive_Partition (int) AS RANGE LEFT FOR VALUES (100,200,300) --Place all partitions into the PRIMARY filegroup CREATE PARTITION SCHEME Active_Scheme AS PARTITION Active_Partition --Partition must currently exist in database ALL TO ([PRIMARY]) --Place all partitions into the PRIMARY filegroup CREATE PARTITION SCHEME Archive_Scheme AS PARTITION Archive_Partition --Partition must currently exist in database ALL TO ([PRIMARY]) CREATE TABLE active_data ( col1 INT PRIMARY KEY CLUSTERED ,col2 VARCHAR(15) ) ON Active_Scheme (col1) CREATE TABLE archive_data ( col1 INT PRIMARY KEY CLUSTERED ,col2 VARCHAR(15) ) ON Archive_Scheme (col1) INSERT INTO active_data VALUES (1,'Description') INSERT INTO active_data VALUES (2,'Description') INSERT INTO active_data VALUES (3,'Description') INSERT INTO active_data VALUES (4,'Description') INSERT INTO active_data VALUES (10,'Description') INSERT INTO active_data VALUES (11,'Description') INSERT INTO active_data VALUES (12,'Description') INSERT INTO active_data VALUES (13,'Description') INSERT INTO active_data VALUES (14,'Description') INSERT INTO active_data VALUES (100,'Description') INSERT INTO archive_data VALUES (200,'Description') INSERT INTO archive_data VALUES (300,'Description') INSERT INTO archive_data VALUES (400,'Description') --Verify partitions SELECT OBJECT_NAME(ps.[object_id]) ,ps.partition_number ,ps.row_count FROM sys.dm_db_partition_stats ps INNER JOIN sys.partitions p ON ps.partition_id = p.partition_id AND p.[object_id] IN (OBJECT_ID('active_data'),OBJECT_ID('archive_ data')) --Switch ownership of partition to another table ALTER TABLE active_data SWITCH PARTITION 3 TO archive_data PARTITION 1 --Verify partitions SELECT OBJECT_NAME(ps.[object_id]) ,ps.partition_number ,ps.row_count FROM sys.dm_db_partition_stats ps INNER JOIN sys.partitions p ON ps.partition_id = p.partition_id AND p.[object_id] IN (OBJECT_ID('active_data'),OBJECT_ID('archive_ data'))
--Prepare database IF OBJECT_ID('active_data') IS NOT NULL DROP TABLE active_data GO IF OBJECT_ID('archive_data') IS NOT NULL DROP TABLE archive_data GO IF EXISTS(SELECT [name] FROM sys.partition_schemes WHERE [name] = 'Active_Scheme') DROP PARTITION SCHEME Active_Scheme GO IF EXISTS(SELECT [name] FROM sys.partition_functions WHERE [name] = 'Active_Partition') DROP PARTITION FUNCTION Active_Partition GO --Create active function CREATE PARTITION FUNCTION Active_Partition (int) AS RANGE LEFT FOR VALUES (1,10,100) --Place all partitions into the PRIMARY filegroup CREATE PARTITION SCHEME Active_Scheme AS PARTITION Active_Partition --Partition must currently exist in database ALL TO ([PRIMARY]) CREATE TABLE active_data ( col1 INT PRIMARY KEY CLUSTERED ,col2 VARCHAR(15) ) ON Active_Scheme (col1) CREATE TABLE archive_data ( col1 INT PRIMARY KEY CLUSTERED ,col2 VARCHAR(15) ) INSERT INTO active_data VALUES (1,'Description') INSERT INTO active_data VALUES (2,'Description') INSERT INTO active_data VALUES (3,'Description') INSERT INTO active_data VALUES (4,'Description') INSERT INTO active_data VALUES (10,'Description') INSERT INTO active_data VALUES (11,'Description') INSERT INTO active_data VALUES (12,'Description') INSERT INTO active_data VALUES (13,'Description') INSERT INTO active_data VALUES (14,'Description') INSERT INTO active_data VALUES (100,'Description') --Verify partitions SELECT OBJECT_NAME(ps.[object_id]) ,ps.partition_number ,ps.row_count FROM sys.dm_db_partition_stats ps INNER JOIN sys.partitions p ON ps.partition_id = p.partition_id AND p.[object_id] IN (OBJECT_ID('active_data'),OBJECT_ID('archive_ data')) --Switch ownership of partition to another table ALTER TABLE active_data SWITCH PARTITION 3 TO archive_data --Verify partitions SELECT OBJECT_NAME(ps.[object_id]) ,ps.partition_number ,ps.row_count FROM sys.dm_db_partition_stats ps INNER JOIN sys.partitions p ON ps.partition_id = p.partition_id AND p.[object_id] IN (OBJECT_ID('active_data'),OBJECT_ID('archive_ data')) --Drop all archive data without logging DROP TABLE archive_data GO --Verify partitions SELECT OBJECT_NAME(ps.[object_id]) ,ps.partition_number ,ps.row_count FROM sys.dm_db_partition_stats ps INNER JOIN sys.partitions p ON ps.partition_id = p.partition_id AND p.[object_id] IN (OBJECT_ID('active_data'),OBJECT_ID('archive_ data'))
--Prepare database IF OBJECT_ID('multiple_partition') IS NOT NULL DROP TABLE multiple_partition GO IF EXISTS(SELECT [name] FROM sys.partition_schemes WHERE [name] = 'Primary_Left_Scheme') DROP PARTITION SCHEME Primary_Left_Scheme GO IF EXISTS(SELECT [name] FROM sys.partition_functions WHERE [name] = 'Left_Partition') DROP PARTITION FUNCTION Left_Partition GO --Create partitioned table CREATE PARTITION FUNCTION Left_Partition (int) AS RANGE LEFT FOR VALUES (1,10,100) --Place all partitions into the PRIMARY filegroup CREATE PARTITION SCHEME Primary_Left_Scheme AS PARTITION Left_Partition --Partition must currently exist in database ALL TO ([PRIMARY]) CREATE TABLE multiple_partition ( col1 INT ,col2 VARCHAR(15) ) ON Primary_Left_Scheme (col1) --Create partitioned non-clustered index CREATE NONCLUSTERED INDEX cl_multiple_partition ON multiple_ partition(col1) INSERT INTO multiple_partition VALUES (1,'Description') INSERT INTO multiple_partition VALUES (2,'Description') INSERT INTO multiple_partition VALUES (3,'Description') INSERT INTO multiple_partition VALUES (4,'Description') INSERT INTO multiple_partition VALUES (10,'Description') INSERT INTO multiple_partition VALUES (11,'Description') INSERT INTO multiple_partition VALUES (12,'Description') INSERT INTO multiple_partition VALUES (13,'Description') INSERT INTO multiple_partition VALUES (14,'Description') INSERT INTO multiple_partition VALUES (100,'Description') INSERT INTO multiple_partition VALUES (101,'Description') INSERT INTO multiple_partition VALUES (102,'Description') INSERT INTO multiple_partition VALUES (103,'Description') INSERT INTO multiple_partition VALUES (104,'Description') --Verify partitions SELECT OBJECT_NAME(ps.[object_id]) ,ps.partition_number ,ps.row_count FROM sys.dm_db_partition_stats ps INNER JOIN sys.partitions p ON ps.partition_id = p.partition_id AND p.[object_id] = OBJECT_ID('multiple_partition') --Verify index partitions SELECT partition_id, index_id FROM sys.partitions pt WHERE pt.[object_id] = OBJECT_ID('multiple_partition')
--Prepare database IF OBJECT_ID('multiple_partition') IS NOT NULL DROP TABLE multiple_partition GO IF EXISTS(SELECT [name] FROM sys.partition_schemes WHERE [name] = 'Primary_Left_Scheme') DROP PARTITION SCHEME Primary_Left_Scheme GO IF EXISTS(SELECT [name] FROM sys.partition_schemes WHERE [name] = 'Index_primary_Left_Scheme') DROP PARTITION SCHEME Index_primary_Left_Scheme GO IF EXISTS(SELECT [name] FROM sys.partition_functions WHERE [name] = 'Left_Partition') DROP PARTITION FUNCTION Left_Partition GO IF EXISTS(SELECT [name] FROM sys.partition_functions WHERE [name] = 'Index_Left_Partition') DROP PARTITION FUNCTION Index_Left_Partition GO --Create partitioned index function CREATE PARTITION FUNCTION Index_Left_Partition (int) AS RANGE LEFT FOR VALUES (10,50,100) --Create partitioned table CREATE PARTITION FUNCTION Left_Partition (int) AS RANGE LEFT FOR VALUES (1,10,100) --Place all index partitions into the PRIMARY filegroup CREATE PARTITION SCHEME Index_primary_Left_Scheme AS PARTITION Index_Left_Partition --Partition must currently exist in database ALL TO ([PRIMARY]) --Place all partitions into the PRIMARY filegroup CREATE PARTITION SCHEME Primary_Left_Scheme AS PARTITION Left_Partition --Partition must currently exist in database ALL TO ([PRIMARY]) CREATE TABLE multiple_partition ( col1 INT ,col2 INT ) ON Primary_Left_Scheme (col1) --Create non-aligned partitioned nonclustered index CREATE NONCLUSTERED INDEX cl_multiple_partition ON multiple_ partition(col2) ON Index_primary_Left_Scheme (col2) INSERT INTO multiple_partition VALUES (1,10) INSERT INTO multiple_partition VALUES (2,10) INSERT INTO multiple_partition VALUES (3,10) INSERT INTO multiple_partition VALUES (4,10) INSERT INTO multiple_partition VALUES (10,50) INSERT INTO multiple_partition VALUES (11,50) INSERT INTO multiple_partition VALUES (12,50) INSERT INTO multiple_partition VALUES (13,50) INSERT INTO multiple_partition VALUES (14,50) INSERT INTO multiple_partition VALUES (100,100) INSERT INTO multiple_partition VALUES (101,100) INSERT INTO multiple_partition VALUES (102,100) INSERT INTO multiple_partition VALUES (103,100) INSERT INTO multiple_partition VALUES (104,100) --Verify row count on partitioned data SELECT OBJECT_NAME(ps.[object_id]) ,ps.partition_number ,ps.row_count FROM sys.dm_db_partition_stats ps INNER JOIN sys.partitions p ON ps.partition_id = p.partition_id AND p.[object_id] = OBJECT_ID('multiple_partition') AND p.index_id = 0 --Verify row count on partitioned index --Row counts will not match those found in the data partitions SELECT OBJECT_NAME(ps.[object_id]) ,ps.partition_number ,ps.row_count FROM sys.dm_db_partition_stats ps INNER JOIN sys.partitions p ON ps.partition_id = p.partition_id AND p.[object_id] = OBJECT_ID('multiple_partition') AND p.index_id <> 0
ALTER INDEX cl_multiple_partition ON multiple_partition REBUILD Partition = 2