Skip to content

Instantly share code, notes, and snippets.

@ChrisMoney
Created March 14, 2025 00:02
Show Gist options
  • Save ChrisMoney/c22fe12e9d83ae120f8e09744f579992 to your computer and use it in GitHub Desktop.
Save ChrisMoney/c22fe12e9d83ae120f8e09744f579992 to your computer and use it in GitHub Desktop.
Partition sql database code
--///////////////////////////////////////////////
-- TRIAGE
--/////////////////////////////////////////////
-- Check table sizes
SELECT t.name AS TableName, SUM(p.rows) AS RowCounts
FROM sys.tables AS t
INNER JOIN sys.partitions AS p ON t.object_id = p.object_id
WHERE p.index_id IN (0,1) -- Heap or Clustered Index
GROUP BY t.name
ORDER BY RowCounts DESC;
-- check for existing partitions
SELECT * FROM sys.partition_schemes;
-- Determine best partition strategy on the largest tables
SELECT t.name AS TableName, c.name AS ColumnName, ty.name AS DataType
FROM sys.tables AS t
INNER JOIN sys.columns AS c ON t.object_id = c.object_id
INNER JOIN sys.types AS ty ON c.user_type_id = ty.user_type_id
WHERE t.name IN ('RecipientProfiles', 'PayerProfiles')
AND ty.name IN ('datetime', 'bigint', 'int')
ORDER BY t.name;
--//////////////////////////////////////////////
-- IMPLEMENTATION
--/////////////////////////////////////////////
-- ////////////////////////
-- Partition RecipientProfiles
--////////////////////////
-- Create Partition Function for RecipientProfiles
CREATE PARTITION FUNCTION pf_RecipientProfiles (int)
AS RANGE LEFT FOR VALUES (100000, 250000, 500000);
-- Create RecipientProfiles Partition Scheme
CREATE PARTITION SCHEME ps_RecipientProfiles
AS PARTITION pf_RecipientProfiles
ALL TO ([PRIMARY]); -- All partitions will reside in the default filegroup
-- Create New RecipientProfiles Partition Table
CREATE TABLE RecipientProfiles_Partitioned (
ID INT NOT NULL,
PayerProfileID INT,
-- other columns...
PRIMARY KEY CLUSTERED (ID) ON ps_RecipientProfiles(ID)
);
-- Move old RecipientProfiles data to new partitioned table
INSERT INTO RecipientProfiles_Partitioned SELECT * FROM RecipientProfiles;
-- Swap Tables
EXEC sp_rename 'RecipientProfiles', 'RecipientProfiles_Old';
EXEC sp_rename 'RecipientProfiles_Partitioned', 'RecipientProfiles';
-- ////////////////////////
-- Partition PayerProfiles
--////////////////////////
-- Create Partition Function for PayerProfiles
CREATE PARTITION FUNCTION pf_PayerProfiles (int)
AS RANGE LEFT FOR VALUES (10000, 30000, 60000);
-- Create a Partition Scheme for PayerProfiles
CREATE PARTITION SCHEME ps_PayerProfiles
AS PARTITION pf_PayerProfiles
ALL TO ([PRIMARY]);
-- Create a Partition Table for PayerProfiles
CREATE TABLE PayerProfiles_Partitioned (
CustomerProfileID INT NOT NULL,
TinType INT,
-- other columns...
PRIMARY KEY CLUSTERED (CustomerProfileID) ON ps_PayerProfiles(CustomerProfileID)
);
-- Move old PayerProfiles data to new partitioned table
INSERT INTO PayerProfiles_Partitioned SELECT * FROM PayerProfiles;
-- Swap Tables
EXEC sp_rename 'PayerProfiles', 'PayerProfiles_Old';
EXEC sp_rename 'PayerProfiles_Partitioned', 'PayerProfiles';
--////////////////////////////////////////////////////////////////
-- Verfiy Partitions
--///////////////////////////////////////////////////////////////
-- Check partitioned tables
SELECT t.name AS TableName, p.partition_id, p.rows
FROM sys.partitions p
JOIN sys.tables t ON p.object_id = t.object_id
WHERE p.index_id IN (0,1);
-- View partition function details
SELECT * FROM sys.partition_functions;
--///////////////////////////////////////////////////////////
-- Monthly Index Maintenance
--/////////////////////////////////////////////////////////
ALTER INDEX ALL ON RecipientProfiles REBUILD PARTITION = 2;
--////////////////////////////////////////////////////////
-- Archive old data when needed
--///////////////////////////////////////////////////////
ALTER TABLE RecipientProfiles SWITCH PARTITION 1 TO ArchiveTable;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment