Technical Articles by Srđan Ljuština

Mastering SQL Server Table Partitioning: Scaling Databases for Performance

⏱️ 30-Minute Deep Dive

A Comprehensive Guide with Visual Diagrams, Real-World Examples, and Best Practices


About This Article

This article represents my journey of learning and implementing SQL Server table partitioning for managing large-scale databases. These are my detailed notes and insights from the learning process, which I hope will be valuable to others working with high-volume data systems.


Table of Contents

  1. Executive Summary
  2. Introduction
  3. Understanding Table Partitioning
  4. Partitioning Fundamentals
  5. Partition Function Types
  6. Creating Partitioned Tables
  7. Partition Strategies
  8. Partition Maintenance
  9. Performance Optimization
  10. Visual Representations
  11. Best Practices
  12. Common Scenarios
  13. Troubleshooting
  14. Conclusion
  15. Further Reading

Executive Summary

Overview

This comprehensive guide explores SQL Server table partitioning, a powerful technique for managing large tables by dividing them into smaller, more manageable pieces. Partitioning improves query performance, simplifies maintenance operations, and enables efficient data archiving for tables with millions or billions of rows.

Key Takeaways

What You’ll Learn

Who Should Read This


Introduction

Imagine managing a table with billions of rows representing years of transaction history. Simple queries take minutes to execute, backups run for hours, and index maintenance locks the table for extended periods. Your users complain about slow response times, and your maintenance windows keep expanding.

This is where table partitioning becomes essential. By dividing your massive table into smaller, manageable partitions, you can dramatically improve query performance, enable efficient data archiving, and perform maintenance on individual partitions without affecting the entire table.

SQL Server’s table partitioning feature transforms how you manage large-scale data, making previously impossible operations practical and efficient.


Understanding Table Partitioning

What is Table Partitioning?

Table partitioning is a database design technique that horizontally divides a large table into smaller, more manageable pieces called partitions. Each partition stores a subset of the table’s data based on a partition key (usually a column like date, ID, or region).

╔═══════════════════════════════════════════════════════════╗
║        🎯 NON-PARTITIONED VS PARTITIONED TABLE 🎯        ║
╚═══════════════════════════════════════════════════════════╝

NON-PARTITIONED TABLE
┌─────────────────────────────────────────────────────┐
│  Sales Table (1 Billion Rows)                      │
│  ┌──────────────────────────────────────────────┐  │
│  │ 2020 data  (250M rows)                       │  │
│  │ 2021 data  (250M rows)                       │  │
│  │ 2022 data  (250M rows)                       │  │
│  │ 2023 data  (250M rows)                       │  │
│  └──────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────┘
     ❌ All data in one piece
     ❌ Queries scan entire table
     ❌ Maintenance affects everything

────────────────────────────────────────────────────────

PARTITIONED TABLE
┌─────────────────────────────────────────────────────┐
│  Sales Table (4 Partitions)                        │
├──────────────┬──────────────┬──────────────┬────────┤
│ Partition 1  │ Partition 2  │ Partition 3  │ Part 4 │
│ 2020 data    │ 2021 data    │ 2022 data    │ 2023   │
│ (250M rows)  │ (250M rows)  │ (250M rows)  │ (250M) │
└──────────────┴──────────────┴──────────────┴────────┘
     ✅ Data logically organized
     ✅ Query only needed partitions
     ✅ Maintain one partition at a time

Why Partition Tables?

Problem 1: Query Performance

Problem 2: Maintenance Operations

Problem 3: Data Archiving

Problem 4: Backup and Restore

Real-World Analogy

Think of table partitioning like organizing a library by year:

Non-Partitioned (One Big Room):

Partitioned (Separate Rooms by Decade):


Partitioning Fundamentals

Partition Function

A partition function defines how data is divided into partitions using boundary values.

-- Example: Partition by year
CREATE PARTITION FUNCTION PF_SalesByYear (DATE)
AS RANGE RIGHT FOR VALUES
    ('2020-01-01', '2021-01-01', '2022-01-01', '2023-01-01');

What this creates:

Partition 1: < 2020-01-01
Partition 2: >= 2020-01-01 AND < 2021-01-01
Partition 3: >= 2021-01-01 AND < 2022-01-01
Partition 4: >= 2022-01-01 AND < 2023-01-01
Partition 5: >= 2023-01-01

Partition Scheme

A partition scheme maps partitions to filegroups (storage locations).

-- Map partitions to filegroups
CREATE PARTITION SCHEME PS_SalesByYear
AS PARTITION PF_SalesByYear
TO (FG_2019, FG_2020, FG_2021, FG_2022, FG_2023);

Partitioned Table

Create table using the partition scheme:

CREATE TABLE Sales (
    SaleID INT IDENTITY(1,1),
    SaleDate DATE NOT NULL,
    CustomerID INT,
    Amount DECIMAL(18,2),
    CONSTRAINT PK_Sales PRIMARY KEY (SaleID, SaleDate)
)
ON PS_SalesByYear(SaleDate);  -- Partition on SaleDate

The Three-Step Process

╔═══════════════════════════════════════════════════════════╗
║         🎯 TABLE PARTITIONING THREE STEPS 🎯             ║
╚═══════════════════════════════════════════════════════════╝

STEP 1: CREATE PARTITION FUNCTION
┌────────────────────────────────────────────┐
│ Defines: How to split data               │
│ Input:   Boundary values                 │
│ Output:  Partition ranges                │
└────────────────────────────────────────────┘
              │
              ▼
STEP 2: CREATE PARTITION SCHEME
┌────────────────────────────────────────────┐
│ Defines: Where to store each partition   │
│ Input:   Partition function + filegroups │
│ Output:  Physical storage mapping        │
└────────────────────────────────────────────┘
              │
              ▼
STEP 3: CREATE PARTITIONED TABLE
┌────────────────────────────────────────────┐
│ Creates: Table on partition scheme        │
│ Input:   Table definition + scheme        │
│ Output:  Partitioned table                │
└────────────────────────────────────────────┘

Partition Function Types

Range Left vs Range Right

SQL Server supports two partition function types:

RANGE LEFT: Boundary value belongs to LEFT partition RANGE RIGHT: Boundary value belongs to RIGHT partition

╔═══════════════════════════════════════════════════════════╗
║          🎯 RANGE LEFT vs RANGE RIGHT 🎯                 ║
╚═══════════════════════════════════════════════════════════╝

RANGE LEFT (boundary value in left partition)
─────────────────────────────────────────────
Boundary: 100, 200, 300

Partition 1: <= 100  (includes 100)
Partition 2: > 100 AND <= 200  (includes 200)
Partition 3: > 200 AND <= 300  (includes 300)
Partition 4: > 300

────────────────────────────────────────────────────────────

RANGE RIGHT (boundary value in right partition)
─────────────────────────────────────────────
Boundary: 100, 200, 300

Partition 1: < 100
Partition 2: >= 100 AND < 200  (includes 100)
Partition 3: >= 200 AND < 300  (includes 200)
Partition 4: >= 300  (includes 300)

Example:

-- RANGE LEFT
CREATE PARTITION FUNCTION PF_RangeLeft (INT)
AS RANGE LEFT FOR VALUES (100, 200, 300);

-- RANGE RIGHT (more common for dates)
CREATE PARTITION FUNCTION PF_RangeRight (INT)
AS RANGE RIGHT FOR VALUES (100, 200, 300);

Choosing the Right Boundary

For Dates: Use RANGE RIGHT

-- RANGE RIGHT for dates (recommended)
CREATE PARTITION FUNCTION PF_ByMonth (DATE)
AS RANGE RIGHT FOR VALUES
    ('2024-01-01', '2024-02-01', '2024-03-01');

-- Result:
-- Partition 1: < 2024-01-01 (old data)
-- Partition 2: Jan 2024 (>= 2024-01-01 AND < 2024-02-01)
-- Partition 3: Feb 2024 (>= 2024-02-01 AND < 2024-03-01)
-- Partition 4: Mar+ 2024 (>= 2024-03-01)

Why RANGE RIGHT for dates?


Creating Partitioned Tables

Step-by-Step Implementation

Step 1: Create Filegroups (Optional but Recommended)

-- Create filegroups for each partition
ALTER DATABASE SalesDB
ADD FILEGROUP FG_Sales_2020;

ALTER DATABASE SalesDB
ADD FILEGROUP FG_Sales_2021;

ALTER DATABASE SalesDB
ADD FILEGROUP FG_Sales_2022;

ALTER DATABASE SalesDB
ADD FILEGROUP FG_Sales_2023;

-- Add files to filegroups
ALTER DATABASE SalesDB
ADD FILE (
    NAME = Sales_2020,
    FILENAME = 'D:\Data\Sales_2020.ndf',
    SIZE = 100MB,
    FILEGROWTH = 50MB
) TO FILEGROUP FG_Sales_2020;

-- Repeat for other filegroups...

Step 2: Create Partition Function

CREATE PARTITION FUNCTION PF_SalesByYear (DATE)
AS RANGE RIGHT FOR VALUES
    ('2020-01-01', '2021-01-01', '2022-01-01', '2023-01-01');

Step 3: Create Partition Scheme

CREATE PARTITION SCHEME PS_SalesByYear
AS PARTITION PF_SalesByYear
TO (FG_Sales_2020, FG_Sales_2021, FG_Sales_2022,
    FG_Sales_2023, PRIMARY);
-- PRIMARY for future data beyond 2023

Step 4: Create Partitioned Table

CREATE TABLE Sales (
    SaleID BIGINT IDENTITY(1,1),
    SaleDate DATE NOT NULL,
    CustomerID INT NOT NULL,
    ProductID INT NOT NULL,
    Quantity INT NOT NULL,
    UnitPrice DECIMAL(18,2) NOT NULL,
    TotalAmount AS (Quantity * UnitPrice) PERSISTED,
    CONSTRAINT PK_Sales PRIMARY KEY (SaleID, SaleDate)
) ON PS_SalesByYear(SaleDate);

Note: Partition key (SaleDate) must be part of PRIMARY KEY!

Date-Based Partitioning Example

Monthly Partitioning for High-Volume Tables:

-- Partition function for monthly partitions
CREATE PARTITION FUNCTION PF_TransactionsByMonth (DATETIME2)
AS RANGE RIGHT FOR VALUES
(
    '2024-01-01', '2024-02-01', '2024-03-01', '2024-04-01',
    '2024-05-01', '2024-06-01', '2024-07-01', '2024-08-01',
    '2024-09-01', '2024-10-01', '2024-11-01', '2024-12-01'
);

-- Partition scheme (all on PRIMARY for simplicity)
CREATE PARTITION SCHEME PS_TransactionsByMonth
AS PARTITION PF_TransactionsByMonth
ALL TO ([PRIMARY]);

-- Partitioned table
CREATE TABLE Transactions (
    TransactionID BIGINT IDENTITY(1,1),
    TransactionDate DATETIME2 NOT NULL,
    AccountID INT NOT NULL,
    TransactionType VARCHAR(50),
    Amount DECIMAL(18,2),
    CONSTRAINT PK_Transactions
        PRIMARY KEY (TransactionID, TransactionDate)
) ON PS_TransactionsByMonth(TransactionDate);

Numeric Partitioning Example

Partition by ID Range:

-- Partition by customer ID ranges
CREATE PARTITION FUNCTION PF_CustomersByID (INT)
AS RANGE RIGHT FOR VALUES
(
    1000000,    -- 0 to 999,999
    2000000,    -- 1M to 1.999M
    3000000,    -- 2M to 2.999M
    4000000     -- 3M to 3.999M
    -- 4M+
);

CREATE PARTITION SCHEME PS_CustomersByID
AS PARTITION PF_CustomersByID
ALL TO ([PRIMARY]);

CREATE TABLE Customers (
    CustomerID INT NOT NULL,
    FirstName NVARCHAR(100),
    LastName NVARCHAR(100),
    Email NVARCHAR(255),
    CreatedDate DATETIME2,
    CONSTRAINT PK_Customers PRIMARY KEY (CustomerID)
) ON PS_CustomersByID(CustomerID);

Partition Strategies

Range Partitioning

Most common strategy - partition by continuous ranges.

Use Cases:

-- Example: Order history by year
CREATE PARTITION FUNCTION PF_OrdersByYear (DATE)
AS RANGE RIGHT FOR VALUES
    ('2020-01-01', '2021-01-01', '2022-01-01', '2023-01-01');

List Partitioning

Partition by discrete values (region, category, status).

Note: SQL Server doesn’t have native LIST partitioning, but you can simulate it:

-- Simulate list partitioning by region
-- Use integer mapping: 1=North, 2=South, 3=East, 4=West

CREATE PARTITION FUNCTION PF_ByRegion (INT)
AS RANGE RIGHT FOR VALUES (2, 3, 4);

-- Partitions:
-- 1: RegionID = 1 (North)
-- 2: RegionID = 2 (South)
-- 3: RegionID = 3 (East)
-- 4: RegionID >= 4 (West and others)

Hash Partitioning

Distribute data evenly across partitions using a hash function.

-- Simulated hash partitioning
-- Use computed column with hash function

CREATE TABLE Orders (
    OrderID BIGINT,
    CustomerID INT,
    OrderDate DATE,
    -- Computed column for partitioning
    PartitionKey AS (OrderID % 10) PERSISTED,
    CONSTRAINT PK_Orders PRIMARY KEY (OrderID, PartitionKey)
) ON PS_HashPartition(PartitionKey);

CREATE PARTITION FUNCTION PF_Hash (INT)
AS RANGE RIGHT FOR VALUES (1, 2, 3, 4, 5, 6, 7, 8, 9);
-- Creates 10 partitions (0-9)

Partition Maintenance

Sliding Window Pattern

The sliding window pattern maintains a fixed number of active partitions by adding new ones and removing old ones.

╔═══════════════════════════════════════════════════════════╗
║           🎯 SLIDING WINDOW PATTERN 🎯                   ║
╚═══════════════════════════════════════════════════════════╝

MONTH 1: Initial State
┌──────┬──────┬──────┬──────┬──────┐
│ Jan  │ Feb  │ Mar  │ Apr  │ May  │
└──────┴──────┴──────┴──────┴──────┘

MONTH 2: End of May - Add June, Remove January
        ┌──────┬──────┬──────┬──────┬──────┐
        │ Feb  │ Mar  │ Apr  │ May  │ Jun  │
        └──────┴──────┴──────┴──────┴──────┘
         ↑ Add new partition (Jun)
   Remove oldest (Jan) ↓

MONTH 3: End of June - Add July, Remove February
               ┌──────┬──────┬──────┬──────┬──────┐
               │ Mar  │ Apr  │ May  │ Jun  │ Jul  │
               └──────┴──────┴──────┴──────┴──────┘

Maintains: Rolling 5-month window
Benefits:
• Fixed storage size
• Automatic archiving
• No DELETE operations

Adding New Partitions

Method 1: Split Existing Partition

-- Add new partition for 2025
ALTER PARTITION SCHEME PS_SalesByYear
NEXT USED FG_Sales_2025;  -- Optional: specify filegroup

ALTER PARTITION FUNCTION PF_SalesByYear()
SPLIT RANGE ('2025-01-01');

-- Result: Creates new partition for data >= 2025-01-01

Method 2: Pre-create Empty Partitions

-- Create partition function with future boundaries
CREATE PARTITION FUNCTION PF_SalesWithFuture (DATE)
AS RANGE RIGHT FOR VALUES
(
    '2023-01-01', '2024-01-01', '2025-01-01',  -- Active
    '2026-01-01', '2027-01-01'                  -- Future (empty)
);

Removing Old Partitions

Method 1: Merge Empty Partition

-- First, remove/archive data from oldest partition
-- Then merge the boundary

ALTER PARTITION FUNCTION PF_SalesByYear()
MERGE RANGE ('2020-01-01');

-- Result: Merges partition 1 and 2
-- Data that was >= 2020 is now in same partition as < 2020

Method 2: Switch Out Before Merge (Recommended)

-- Step 1: Create staging table with same structure
CREATE TABLE Sales_Archive_2020 (
    -- Same structure as Sales table
    SaleID BIGINT,
    SaleDate DATE NOT NULL,
    CustomerID INT,
    Amount DECIMAL(18,2),
    CONSTRAINT PK_Sales_Archive_2020 PRIMARY KEY (SaleID, SaleDate)
) ON FG_Sales_2020;  -- Same filegroup

-- Step 2: Switch partition to staging table
ALTER TABLE Sales
SWITCH PARTITION 1 TO Sales_Archive_2020;
-- Instant operation! No data movement

-- Step 3: Now merge the empty partition
ALTER PARTITION FUNCTION PF_SalesByYear()
MERGE RANGE ('2020-01-01');

-- Step 4: Archive or drop staging table
-- Backup Sales_Archive_2020 to tape/blob storage
-- DROP TABLE Sales_Archive_2020;

Switching Partitions

Partition switching is the killer feature - instant data movement!

-- Switch partition 5 from Sales to Archive table
ALTER TABLE Sales
SWITCH PARTITION 5 TO Sales_Archive;

-- Requirements for SWITCH to work:
-- 1. Both tables same structure
-- 2. Both on same filegroup (for that partition)
-- 3. Constraints and indexes compatible
-- 4. No FK constraints pointing to table

Complete Sliding Window Example:

-- Monthly sliding window maintenance
-- Run at end of each month

DECLARE @NewMonth DATE = '2024-12-01';
DECLARE @OldMonth DATE = '2024-06-01';

-- Step 1: Add new partition for next month
ALTER PARTITION SCHEME PS_SalesByMonth
NEXT USED FG_Sales_Current;

ALTER PARTITION FUNCTION PF_SalesByMonth()
SPLIT RANGE (@NewMonth);

-- Step 2: Switch out old partition
CREATE TABLE Sales_Archive_202406 (
    SaleID BIGINT,
    SaleDate DATE NOT NULL,
    CustomerID INT,
    Amount DECIMAL(18,2),
    CONSTRAINT PK_Archive PRIMARY KEY (SaleID, SaleDate)
) ON FG_Sales_Archive;

ALTER TABLE Sales
SWITCH PARTITION $PARTITION.PF_SalesByMonth(@OldMonth)
TO Sales_Archive_202406;

-- Step 3: Merge old partition boundary
ALTER PARTITION FUNCTION PF_SalesByMonth()
MERGE RANGE (@OldMonth);

-- Step 4: Archive old data
BACKUP DATABASE SalesDB
FILEGROUP = 'FG_Sales_Archive'
TO DISK = 'E:\Backups\Sales_202406.bak';

DROP TABLE Sales_Archive_202406;

Performance Optimization

Partition Elimination

SQL Server automatically eliminates irrelevant partitions from query execution.

-- Query with partition elimination
SELECT *
FROM Sales
WHERE SaleDate >= '2023-01-01'
  AND SaleDate < '2023-02-01';

-- Execution plan shows:
-- Partitions scanned: 1 of 5
-- I/O reduced by 80%!

Viewing Partition Elimination:

-- Check actual execution plan
-- Look for "Actual Partition Count" vs "Estimated Partition Count"

-- Query DMVs for partition access
SELECT
    OBJECT_NAME(i.object_id) AS TableName,
    p.partition_number,
    p.rows,
    fg.name AS FileGroupName
FROM sys.partitions p
JOIN sys.indexes i ON p.object_id = i.object_id
    AND p.index_id = i.index_id
JOIN sys.allocation_units au ON p.partition_id = au.container_id
JOIN sys.filegroups fg ON au.data_space_id = fg.data_space_id
WHERE i.object_id = OBJECT_ID('Sales')
ORDER BY p.partition_number;

Aligned Indexes

Keep indexes aligned with table partitioning for best performance.

-- Aligned index (recommended)
CREATE NONCLUSTERED INDEX IX_Sales_Customer
ON Sales(CustomerID, SaleDate)
ON PS_SalesByYear(SaleDate);  -- Same partition scheme!

-- Non-aligned index (avoid if possible)
CREATE NONCLUSTERED INDEX IX_Sales_Customer_NonAligned
ON Sales(CustomerID)
ON [PRIMARY];  -- Different partition scheme

Benefits of Aligned Indexes:

Partition-Level Statistics

Update statistics per partition for faster maintenance:

-- Update statistics for specific partition
UPDATE STATISTICS Sales
WITH RESAMPLE
ON PARTITIONS (5);  -- Only partition 5

-- Rebuild index for specific partition
ALTER INDEX IX_Sales_Customer
ON Sales
REBUILD PARTITION = 5;

-- Benefits:
-- - Shorter maintenance window
-- - Less locking
-- - Can maintain partitions in parallel

Visual Representations

Diagram 1: Partitioning Architecture

╔═══════════════════════════════════════════════════════════╗
║       🎯 SQL SERVER PARTITIONING ARCHITECTURE 🎯         ║
╚═══════════════════════════════════════════════════════════╝

APPLICATION LAYER
       │
       │ SELECT * FROM Sales
       │ WHERE SaleDate = '2023-05-15'
       ▼
┌──────────────────────────────────────────┐
│   QUERY OPTIMIZER                        │
│   • Analyzes query predicate             │
│   • Determines relevant partitions       │
│   • Applies partition elimination        │
└──────────────┬───────────────────────────┘
               │
               ▼
┌──────────────────────────────────────────┐
│   PARTITION FUNCTION                     │
│   PF_SalesByYear(DATE)                   │
│   ┌────────────────────────────┐         │
│   │ Boundaries:                │         │
│   │ 2020-01-01, 2021-01-01,   │         │
│   │ 2022-01-01, 2023-01-01    │         │
│   └────────────────────────────┘         │
└──────────────┬───────────────────────────┘
               │
               ▼
┌──────────────────────────────────────────┐
│   PARTITION SCHEME                       │
│   PS_SalesByYear                         │
│   Maps partitions → filegroups           │
└──────────────┬───────────────────────────┘
               │
               ▼
┌──────────────────────────────────────────┐
│   PHYSICAL STORAGE                       │
├──────────┬──────────┬──────────┬─────────┤
│ FG_2020  │ FG_2021  │ FG_2022  │ FG_2023 │
│ Disk 1   │ Disk 2   │ Disk 3   │ Disk 4  │
│ 250M     │ 250M     │ 250M     │ 250M    │
│ rows     │ rows     │ rows     │ rows    │
└──────────┴──────────┴──────────┴─────────┘
     ↑ Only this partition scanned for query!

Diagram 2: Sliding Window Pattern

╔═══════════════════════════════════════════════════════════╗
║         🎯 MONTHLY SLIDING WINDOW PROCESS 🎯             ║
╚═══════════════════════════════════════════════════════════╝

INITIAL STATE (Maintain 6 months)
┌────────┬────────┬────────┬────────┬────────┬────────┐
│  Jul   │  Aug   │  Sep   │  Oct   │  Nov   │  Dec   │
└────────┴────────┴────────┴────────┴────────┴────────┘

END OF DECEMBER - Maintenance Process:

STEP 1: ADD NEW PARTITION
┌────────┬────────┬────────┬────────┬────────┬────────┬────────┐
│  Jul   │  Aug   │  Sep   │  Oct   │  Nov   │  Dec   │  Jan   │← SPLIT
└────────┴────────┴────────┴────────┴────────┴────────┴────────┘

STEP 2: SWITCH OUT OLDEST
  Archive ↓
┌────────┐ ┌────────┬────────┬────────┬────────┬────────┬────────┐
│  Jul   │ │  Aug   │  Sep   │  Oct   │  Nov   │  Dec   │  Jan   │
└────────┘ └────────┴────────┴────────┴────────┴────────┴────────┘
  SWITCH   ↑ Main table

STEP 3: MERGE BOUNDARY
        MERGE ↓
         ┌────────┬────────┬────────┬────────┬────────┬────────┐
         │  Aug   │  Sep   │  Oct   │  Nov   │  Dec   │  Jan   │
         └────────┴────────┴────────┴────────┴────────┴────────┘

FINAL STATE (6 months maintained)
┌────────┬────────┬────────┬────────┬────────┬────────┐
│  Aug   │  Sep   │  Oct   │  Nov   │  Dec   │  Jan   │
└────────┴────────┴────────┴────────┴────────┴────────┘

Diagram 3: Partition Switch Operation

╔═══════════════════════════════════════════════════════════╗
║         🎯 PARTITION SWITCH OPERATION 🎯                 ║
╚═══════════════════════════════════════════════════════════╝

BEFORE SWITCH
─────────────
Main Table (Sales)                Archive Table (Sales_2020)
┌────────────────────────┐        ┌────────────────────────┐
│ Partition 1 (2020)     │        │        (Empty)         │
│ 50 million rows        │        │        0 rows          │
│ Filegroup: FG_2020     │        │ Filegroup: FG_2020     │
└────────────────────────┘        └────────────────────────┘

EXECUTE SWITCH
──────────────
ALTER TABLE Sales SWITCH PARTITION 1 TO Sales_2020;
                    ↓
         Metadata swap only!
         No data movement!
         Instant operation!

AFTER SWITCH
────────────
Main Table (Sales)                Archive Table (Sales_2020)
┌────────────────────────┐        ┌────────────────────────┐
│        (Empty)         │        │ Partition 1 (2020)     │
│        0 rows          │        │ 50 million rows        │
│ Filegroup: FG_2020     │        │ Filegroup: FG_2020     │
└────────────────────────┘        └────────────────────────┘

Time elapsed: < 1 second!
Locks: Schema modification lock only
Logging: Minimal

Best Practices

Choosing Partition Keys

Good Partition Key Characteristics:

✅ Used in WHERE clauses frequently
✅ Evenly distributed data
✅ Stable (doesn't change)
✅ Business-logical boundaries
✅ Enables efficient archiving

❌ Avoid skewed distributions
❌ Avoid frequently updated columns
❌ Avoid too many partitions (< 1000)
❌ Avoid too few partitions (> 10-15)

Examples:

-- ✅ Good: Date column for time-series data
CREATE PARTITION FUNCTION PF_ByDate (DATE)
AS RANGE RIGHT FOR VALUES ('2023-01-01', '2024-01-01');

-- ✅ Good: Sequential ID with even distribution
CREATE PARTITION FUNCTION PF_ByCustomerID (INT)
AS RANGE RIGHT FOR VALUES (1000000, 2000000, 3000000);

-- ❌ Bad: Status column (uneven distribution)
CREATE PARTITION FUNCTION PF_ByStatus (VARCHAR(20))
AS RANGE RIGHT FOR VALUES ('Active', 'Inactive');
-- Problem: 99% data in 'Active', 1% in 'Inactive'

-- ❌ Bad: Updated column
CREATE PARTITION FUNCTION PF_ByLastModified (DATETIME)
AS RANGE RIGHT FOR VALUES (...);
-- Problem: Records move between partitions on update

Filegroup Strategy

Strategy 1: Separate Filegroups (Best for Large Databases)

-- Each partition on different filegroup
-- Benefits:
-- - Backup/restore individual partitions
-- - Place on different disks for I/O distribution
-- - Better manageability

CREATE PARTITION SCHEME PS_Advanced
AS PARTITION PF_SalesByYear
TO (FG_2020, FG_2021, FG_2022, FG_2023, FG_2024);

Strategy 2: All on PRIMARY (Simplest)

-- All partitions on PRIMARY
-- Benefits:
-- - Simpler management
-- - Good for smaller databases
-- - Still get query performance benefits

CREATE PARTITION SCHEME PS_Simple
AS PARTITION PF_SalesByYear
ALL TO ([PRIMARY]);

Strategy 3: Hot/Warm/Cold (Tiered Storage)

-- Recent data on fast SSD, old data on slow HDD
CREATE PARTITION SCHEME PS_Tiered
AS PARTITION PF_SalesByYear
TO (
    FG_Cold_HDD,      -- 2020 data
    FG_Warm_HDD,      -- 2021 data
    FG_Warm_SSD,      -- 2022 data
    FG_Hot_SSD,       -- 2023 data
    FG_Hot_SSD        -- 2024+ data
);

Index Alignment

Always align indexes with table partitioning:

-- ✅ Aligned index (recommended)
CREATE INDEX IX_Aligned
ON Sales(CustomerID, SaleDate)
ON PS_SalesByYear(SaleDate);
-- Benefits: Partition-level maintenance, faster switching

-- ❌ Non-aligned (avoid)
CREATE INDEX IX_NonAligned
ON Sales(CustomerID)
ON [PRIMARY];
-- Problem: Can't switch partitions, slower maintenance

Monitoring and Maintenance

Query Partition Information:

-- View partition details
SELECT
    OBJECT_NAME(p.object_id) AS TableName,
    p.partition_number AS PartitionNum,
    prv.value AS BoundaryValue,
    p.rows AS RowCount,
    au.total_pages * 8 / 1024 AS SizeMB,
    fg.name AS FileGroupName
FROM sys.partitions p
LEFT JOIN sys.partition_range_values prv
    ON p.partition_id = prv.boundary_id
JOIN sys.allocation_units au
    ON p.partition_id = au.container_id
JOIN sys.filegroups fg
    ON au.data_space_id = fg.data_space_id
WHERE p.object_id = OBJECT_ID('Sales')
  AND p.index_id IN (0,1)  -- Heap or clustered index
ORDER BY p.partition_number;

Monitor Partition Sizes:

-- Alert if partition size exceeds threshold
WITH PartitionSizes AS (
    SELECT
        p.partition_number,
        SUM(au.total_pages) * 8 / 1024 AS SizeMB
    FROM sys.partitions p
    JOIN sys.allocation_units au
        ON p.partition_id = au.container_id
    WHERE p.object_id = OBJECT_ID('Sales')
    GROUP BY p.partition_number
)
SELECT *
FROM PartitionSizes
WHERE SizeMB > 10000  -- Alert if > 10GB
ORDER BY partition_number;

Common Scenarios

Time-Based Data Archiving

Scenario: Retain 2 years of online data, archive older data

-- Initial setup
CREATE PARTITION FUNCTION PF_OrderArchive (DATE)
AS RANGE RIGHT FOR VALUES
(
    '2022-01-01',  -- Old data
    '2023-01-01',  -- Last year
    '2024-01-01'   -- Current year
);

CREATE PARTITION SCHEME PS_OrderArchive
AS PARTITION PF_OrderArchive
TO (FG_Archive, FG_History, FG_Current, FG_Current);

-- Monthly maintenance job
-- Add new month, archive old month
DECLARE @NewMonth DATE = DATEADD(MONTH, 1, EOMONTH(GETDATE()));
DECLARE @ArchiveMonth DATE = DATEADD(YEAR, -2, DATEADD(MONTH, 1, EOMONTH(GETDATE())));

-- Add new partition
ALTER PARTITION SCHEME PS_OrderArchive NEXT USED FG_Current;
ALTER PARTITION FUNCTION PF_OrderArchive() SPLIT RANGE (@NewMonth);

-- Archive old partition
-- Switch to archive table, backup, merge

Multi-Tenant Applications

Scenario: Separate data by tenant ID for isolation and performance

-- Partition by TenantID ranges
CREATE PARTITION FUNCTION PF_ByTenant (INT)
AS RANGE RIGHT FOR VALUES
(
    1000,  -- Tenants 1-999
    2000,  -- Tenants 1000-1999
    3000,  -- Tenants 2000-2999
    4000   -- Tenants 3000-3999
);

CREATE TABLE CustomerData (
    DataID BIGINT IDENTITY,
    TenantID INT NOT NULL,
    CustomerName NVARCHAR(200),
    Data XML,
    CONSTRAINT PK_Data PRIMARY KEY (DataID, TenantID)
) ON PS_ByTenant(TenantID);

-- Benefits:
-- - Tenant data isolation
-- - Backup/restore per tenant
-- - Easier tenant migration
-- - Better query performance with partition elimination

Geographic Data Distribution

Scenario: Partition by region for multi-region application

-- Map regions to partition numbers
-- 1=US, 2=EU, 3=APAC, 4=Other

CREATE PARTITION FUNCTION PF_ByRegion (TINYINT)
AS RANGE RIGHT FOR VALUES (2, 3, 4);

CREATE PARTITION SCHEME PS_ByRegion
AS PARTITION PF_ByRegion
TO (FG_US, FG_EU, FG_APAC, FG_Other);

CREATE TABLE Orders (
    OrderID BIGINT,
    RegionID TINYINT NOT NULL,
    OrderDate DATE,
    CustomerID INT,
    CONSTRAINT PK_Orders PRIMARY KEY (OrderID, RegionID)
) ON PS_ByRegion(RegionID);

-- Can place filegroups on servers in respective regions
-- for better performance

Troubleshooting

Performance Issues

Problem: Queries still slow after partitioning

Diagnosis:

-- Check if partition elimination is working
SET STATISTICS IO ON;
SET STATISTICS TIME ON;

SELECT *
FROM Sales
WHERE SaleDate = '2023-05-15';

-- Look for:
-- - "Partitions accessed: 1" (good)
-- - "Partitions accessed: all" (bad - no elimination)

Solutions:

-- ✅ Include partition key in WHERE clause
SELECT * FROM Sales
WHERE SaleDate >= '2023-01-01';  -- Good

-- ❌ Avoid functions on partition key
SELECT * FROM Sales
WHERE YEAR(SaleDate) = 2023;  -- Bad - no elimination

-- ✅ Use SARGable predicates
SELECT * FROM Sales
WHERE SaleDate >= '2023-01-01'
  AND SaleDate < '2024-01-01';  -- Good

Partition Imbalance

Problem: One partition much larger than others

Diagnosis:

-- Find partition sizes
SELECT
    p.partition_number,
    p.rows,
    au.total_pages * 8 / 1024 AS SizeMB,
    CAST(100.0 * p.rows / SUM(p.rows) OVER() AS DECIMAL(5,2)) AS PctOfTotal
FROM sys.partitions p
JOIN sys.allocation_units au ON p.partition_id = au.container_id
WHERE p.object_id = OBJECT_ID('Sales')
  AND p.index_id IN (0,1)
ORDER BY p.rows DESC;

Solutions:

Lock Contention

Problem: Partition switch operations blocked

Diagnosis:

-- Check for blocking
SELECT
    blocking.session_id AS BlockingSessionId,
    blocked.session_id AS BlockedSessionId,
    blocked.wait_type,
    blocked.wait_time,
    blocked.wait_resource
FROM sys.dm_exec_requests blocked
JOIN sys.dm_exec_requests blocking
    ON blocked.blocking_session_id = blocking.session_id
WHERE blocked.blocking_session_id > 0;

Solutions:

-- Ensure no active transactions on table
-- Disable foreign keys temporarily
ALTER TABLE Orders NOCHECK CONSTRAINT ALL;

-- Perform switch
ALTER TABLE Sales SWITCH PARTITION 5 TO Sales_Archive;

-- Re-enable constraints
ALTER TABLE Orders CHECK CONSTRAINT ALL;

Conclusion

SQL Server table partitioning is a powerful feature for managing large tables, improving query performance, and simplifying maintenance operations. By dividing massive tables into smaller, manageable partitions, you can:

Key Benefits Recap

Performance:

Manageability:

Scalability:

Implementation Guidelines

  1. Start Simple: Begin with yearly or monthly partitions
  2. Measure First: Benchmark before and after partitioning
  3. Align Indexes: Keep indexes aligned with table partitioning
  4. Monitor Regularly: Track partition sizes and query performance
  5. Automate Maintenance: Use jobs for sliding window operations
  6. Plan Filegroups: Consider separate filegroups for flexibility

When to Use Partitioning

Good Candidates:

Not Recommended:

Remember: Table partitioning requires SQL Server Enterprise Edition (or Azure SQL Database). The benefits often justify the cost for large-scale systems, but evaluate your specific needs and workload patterns.


Further Reading

Official Documentation

Best Practices

Performance Tuning

Tools and Scripts

Case Studies


Happy Partitioning! 🚀

This article represents my journey of learning SQL Server table partitioning. I hope it helps you build scalable, high-performance database solutions.