Overview
SQL Server partitions a table or index horizontally using a partition function (the boundary values) and a partition scheme (which filegroup each partition uses). Its strongest feature is partition switching: moving a whole partition in or out as a metadata-only operation. Generic ideas are in table-partitioning.
Core concepts
RANGE LEFTvsRANGE RIGHTdecide whether a boundary value belongs to the partition on its left or right.RANGE RIGHTwith date boundaries at the first of each month is the common pattern.- The partitioning column must be part of the clustered index key and of every unique index, to keep indexes aligned with the table.
- Partition elimination happens when the predicate filters on the partitioning column.
- Switch in/out:
ALTER TABLE src SWITCH PARTITION n TO dst. The target must have identical structure, indexes, filegroup and a check constraint that proves its rows fit the partition range. - Sliding window:
SPLIT RANGEa new boundary at one end,SWITCHthe oldest partition out,MERGE RANGEit away. - Partitioning is not a substitute for indexing (sqlserver-indexes). Standard Edition supports it since SQL Server 2016 SP1.
Worked example
Executed on SQL Server 2022:
CREATE PARTITION FUNCTION pf_month(date) AS RANGE RIGHT FOR VALUES ('2025-02-01','2025-03-01');
CREATE PARTITION SCHEME ps_month AS PARTITION pf_month ALL TO ([PRIMARY]);
CREATE TABLE dbo.Ev(Id int IDENTITY, Created date NOT NULL, Payload varchar(20),
CONSTRAINT PK_Ev PRIMARY KEY CLUSTERED (Created, Id)) ON ps_month(Created);
-- 30,000 rows loaded across January-March
SELECT partition_number, rows FROM sys.partitions WHERE object_id = OBJECT_ID('dbo.Ev') AND index_id = 1;
-- 1 | 10454 2 | 9436 3 | 10110Switching the January partition out. The first attempt failed (error 4972) because the staging table’s check constraint had a lower bound (Created >= '1900-01-01'), which is narrower than partition 1 (everything below 2025-02-01). The staging table must allow exactly the range of the partition:
CREATE TABLE dbo.Ev_Old(Id int NOT NULL, Created date NOT NULL, Payload varchar(20),
CONSTRAINT PK_EvOld PRIMARY KEY CLUSTERED (Created, Id),
CONSTRAINT CK_old CHECK (Created IS NOT NULL AND Created < '2025-02-01')) ON [PRIMARY];
ALTER TABLE dbo.Ev SWITCH PARTITION 1 TO dbo.Ev_Old;
-- Ev_Old 10454 rows, Ev 19546 rows, instantlyPitfalls and production notes
- Create future partitions ahead of time (
ALTER PARTITION SCHEME ... NEXT USED,SPLIT RANGE); splitting a non-empty partition scans and logs data. - Statistics are per index, not per partition, unless you enable incremental statistics; large tables may need them.
- Parameterised queries can prevent elimination if the optimizer cannot use the value; check the plan’s Actual Partition Count.
- Plan the filegroup layout and backups (filegroup backups) with the sliding window.
- Do not partition just for query speed; do it for lifecycle (archive, fast delete) and load management.
Practice questions
- Why must the check constraint on a staging table match the partition range?
- What is the difference between
RANGE LEFTandRANGE RIGHT? - How would you archive data older than 24 months every month with no blocking?
References
- Microsoft Learn, Partitioned tables and indexes
Related
table-partitioning · sqlserver-indexes · sqlserver-storage-tempdb