A table with hundreds of millions of rows is hard to maintain. Table partitioning keeps it as one table for your queries, but SQL Server stores the rows in separate slices (partitions), usually by date. Old data can be moved out in an instant, and queries that filter on the partition column can skip slices.
Part of the SQL Server for Beginners series. This article is the written version of lesson 18 of the Bigland Network video course. All examples were run in SQL Server Management Studio on a test instance. Run every example on a test server, not on production.
Watch lesson 18 as a video or open it on YouTube. The written lesson below follows the same steps and the same sample data.
Contents
- 1. The big idea
- 2. Partition function and scheme
- 3. Create the table and load rows
- 4. Partition elimination
- 5. SPLIT and SWITCH (the sliding window)
- 6. The unique index rule
- 7. Mistakes and best practices
- 8. Practice and clean up
1. The big idea
Three building blocks: the partition function decides where the cuts are (boundary values), the partition scheme decides which filegroup stores each slice, and the table is created ON the scheme with a partition column. N boundary values always give N + 1 partitions.
2. Partition function and scheme
CREATE PARTITION FUNCTION pf_Year (DATE)
AS RANGE RIGHT FOR VALUES ('2025-01-01', '2026-01-01');
CREATE PARTITION SCHEME ps_Year
AS PARTITION pf_Year ALL TO ([PRIMARY]);
SELECT boundary_id, value AS boundary
FROM sys.partition_range_values
WHERE function_id = (SELECT function_id FROM sys.partition_functions WHERE name = 'pf_Year')
ORDER BY boundary_id;
| boundary_id | boundary |
|---|---|
| 1 | 2025-01-01 |
| 2 | 2026-01-01 |
RANGE RIGHT means the boundary date belongs to the partition on its right: partition 1 = before 2025, partition 2 = the year 2025, partition 3 = 2026 and later.
3. Create the table and load rows
CREATE TABLE dbo.SalesP (
Id INT NOT NULL,
SaleDate DATE NOT NULL,
Amount INT,
CONSTRAINT PK_SalesP PRIMARY KEY CLUSTERED (SaleDate, Id))
ON ps_Year (SaleDate);
INSERT INTO dbo.SalesP (Id, SaleDate, Amount) VALUES
(1, '2024-03-10', 100), (2, '2024-11-05', 200),
(3, '2025-02-14', 150), (4, '2025-08-20', 300),
(5, '2026-01-15', 250), (6, '2026-06-30', 400),
(7, '2026-09-09', 120);
SELECT Id, SaleDate, Amount,
$PARTITION.pf_Year(SaleDate) AS part
FROM dbo.SalesP
ORDER BY Id;
| Id | SaleDate | Amount | part |
|---|---|---|---|
| 1 | 2024-03-10 | 100 | 1 |
| 2 | 2024-11-05 | 200 | 1 |
| 3 | 2025-02-14 | 150 | 2 |
| 4 | 2025-08-20 | 300 | 2 |
| 5 | 2026-01-15 | 250 | 3 |
| 6 | 2026-06-30 | 400 | 3 |
| 7 | 2026-09-09 | 120 | 3 |
Rows per partition (from sys.partitions): 2, 2 and 3.
SELECT partition_number, rows
FROM sys.partitions
WHERE object_id = OBJECT_ID('dbo.SalesP') AND index_id = 1
ORDER BY partition_number;
4. Partition elimination
SELECT COUNT(*) AS sales, SUM(Amount) AS total
FROM dbo.SalesP
WHERE SaleDate >= '2025-01-01' AND SaleDate < '2026-01-01';
Result: 2 sales, total 450. SQL Server only has to read partition 2. Always filter on the partition column, otherwise every slice is read.
5. SPLIT and SWITCH (the sliding window)
ALTER PARTITION SCHEME ps_Year NEXT USED [PRIMARY];
ALTER PARTITION FUNCTION pf_Year() SPLIT RANGE ('2027-01-01');
-- now 4 partitions: 2, 2, 3 and 0 rows
CREATE TABLE dbo.SalesP_Archive (
Id INT NOT NULL,
SaleDate DATE NOT NULL,
Amount INT,
CONSTRAINT PK_SalesP_Archive PRIMARY KEY CLUSTERED (SaleDate, Id))
ON [PRIMARY];
ALTER TABLE dbo.SalesP SWITCH PARTITION 1 TO dbo.SalesP_Archive;
SELECT 'SalesP' AS tbl, COUNT(*) AS n FROM dbo.SalesP
UNION ALL
SELECT 'SalesP_Archive', COUNT(*) FROM dbo.SalesP_Archive;
| tbl | n |
|---|---|
| SalesP | 5 |
| SalesP_Archive | 2 |
SWITCH copies nothing: SQL Server changes the ownership of the slice, so it takes almost no time even for millions of rows. The target table must be empty and have exactly the same columns and clustered key.
6. The unique index rule
CREATE UNIQUE INDEX ux_SalesP_Id
ON dbo.SalesP (Id)
ON ps_Year (SaleDate);
Error 1908: the partitioning column SaleDate must be part of the key of a unique index. Each partition only checks uniqueness inside itself.
7. Mistakes and best practices
- Partitioning a small table: more work, no gain.
- A unique index that leaves out the partition column.
- Not filtering on the partition column, so all slices are read.
- Forgetting to add the next range (SPLIT) before new data arrives.
- Believing partitioning replaces good indexes.
- Best practice: partition by a date or an ever increasing number, add future partitions ahead of time, keep partitions a sensible size, archive with SWITCH instead of a huge DELETE, and test on a copy first.
8. Practice and clean up
- Which partition does the date 2026-06-30 go to?
- Count the rows of SalesP in each partition.
- What is the total Amount in the archive table?
-- 1 (partition 3)
SELECT $PARTITION.pf_Year('2026-06-30') AS part;
-- 2 (partition 2: 2 rows, partition 3: 3 rows)
SELECT $PARTITION.pf_Year(SaleDate) AS part, COUNT(*) AS n
FROM dbo.SalesP
GROUP BY $PARTITION.pf_Year(SaleDate)
ORDER BY part;
-- 3 (300)
SELECT SUM(Amount) AS archived_total
FROM dbo.SalesP_Archive;
-- clean up: tables first, then the scheme, then the function
DROP TABLE dbo.SalesP_Archive;
DROP TABLE dbo.SalesP;
DROP PARTITION SCHEME ps_Year;
DROP PARTITION FUNCTION pf_Year;
Frequently asked questions
Does partitioning make every query faster?
No. It helps queries that filter on the partition column (partition elimination) and, above all, it makes loading and archiving big data easier. It does not replace good indexes.
Is partitioning the same as sharding?
No. Partitioning slices one table inside one database. Sharding spreads data over several servers.
Can I partition an existing table?
Yes: rebuild its clustered index ON the partition scheme. Test it on a copy first, it moves all the data.
What to learn next
The next lesson, lesson 19: Full Text Search, covers full text search: catalogs, CONTAINS, word forms and CONTAINSTABLE. Previous lesson: Lesson 17: Temporal Tables and Row History.
The whole series
- Lesson 1: SELECT, WHERE and ORDER BY
- Lesson 2: JOINs
- Lesson 3: GROUP BY, HAVING and Subqueries
- Lesson 4: INSERT, UPDATE, DELETE and Transactions
- Lesson 5: CREATE TABLE, Data Types and Constraints
- Lesson 6: Indexes, Views and Stored Procedures
- Lesson 7: CTEs and Window Functions
- Lesson 8: Built-in Functions
- Lesson 9: Functions, Temp Tables and Table Variables
- Lesson 10: Triggers and Audit Tables
- Lesson 11: Security: Logins, Users and Roles
- Lesson 12: Backup, Restore and Recovery Models
- Lesson 13: Query Performance Tuning
- Lesson 14: SQL Server Agent Jobs and Maintenance
- Lesson 15: Encryption and Auditing Basics
- Lesson 16: Working with JSON
- Lesson 17: Temporal Tables and Row History
- Lesson 18: Partitioning and Large Tables (this article)
- lesson 19: Full Text Search
- lesson 20: Query Store and Slow Queries
Sources and further reading
- Microsoft Learn, Transact-SQL reference (Database Engine), on learn.microsoft.com: the official syntax for the statements used here.
- Microsoft Learn, table partitioning, partition functions, SPLIT and SWITCH, for the full details of each feature.
More tutorials like this
Msg 208 Invalid Object Name in SQL Server: Causes and Fix
Fix SQL Server Msg 208 Invalid object name: check the spelling, the database and the schema, with real SSMS examples and scripts to find…
SQL Server Query Store Tutorial: Find Slow Queries, Compare Plans and Force a Plan
Turn on Query Store, find the most expensive queries with the sys.query_store views and the SSMS reports, compare the plans of one query and…
SQL Server Full Text Search Tutorial: CONTAINS, FORMSOF and CONTAINSTABLE
Search words inside text columns fast: create a full text catalog and index, use CONTAINS, word forms, phrases and rank the best matches with…
