SQL Server partition table

To create a partitioned table in SQL Server, you must follow a specific sequence: create a partition function , create a partition scheme , and then apply that scheme to your table [1, 2]. This process splits data into logical segments to improve management and performance of large datasets [1, 2].

1. Create the Partition Function

This defines how the data is split based on a boundary value (like a date or ID) [1, 2].

  • RANGE LEFT : The boundary value is the maximum value in the lower partition [1, 2].
  • RANGE RIGHT : The boundary value is the minimum value in the higher partition (commonly used for dates) [1, 2, 3].
sql 复制代码
-- Example: Partitioning by year using RANGE RIGHT
CREATE PARTITION FUNCTION pfSalesDate (datetime2)
AS RANGE RIGHT FOR VALUES ('2023-01-01', '2024-01-01', '2025-01-01');
-- This creates 4 partitions: < 2023, 2023, 2024, and >= 2025

2. Create the Partition Scheme

This maps the logical partitions created by the function to physical filegroups [1, 2]. You can map all partitions to a single filegroup (like PRIMARY) or distribute them across multiple disks [1, 2].

sql 复制代码
-- Map all partitions to the PRIMARY filegroup
CREATE PARTITION SCHEME psSalesDate
AS PARTITION pfSalesDate
ALL TO ([PRIMARY]);

3. Create the Partitioned Table

Apply the scheme to the table during creation by specifying the partitioning column [1, 2].

sql 复制代码
CREATE TABLE Sales (
    SalesID INT IDENTITY(1,1),
    OrderDate DATETIME2 NOT NULL,
    Amount DECIMAL(18,2)
) ON psSalesDate (OrderDate); -- The table is now partitioned by OrderDate

Key Considerations

  • Partition Elimination : SQL Server only reads the relevant partitions if your query filters by the partition key, significantly boosting performance [1, 2].
  • Maintenance : Partitioning makes it easier to "switch" data in or out for archiving, or to truncate specific time periods without affecting the whole table [1, 2].
  • Clustered Indexes : For an existing table, you can partition it by recreating the clustered index on the partition scheme [1, 2].

We can use the built-in Create Partition Wizard in SQL Server Management Studio (SSMS) to generate this script without writing manual code [1, 2].

Using the SSMS Partition Wizard

  1. Open the Wizard : Right-click your table, select Storage > Create Partition... [1, 2].
  2. Select Column: Choose your date column.
  3. Set Boundaries : On the Map Partitions page, click the Set Boundaries... button [1].
  4. Configure Range :
    • Start Date : 2020-01-01
    • End Date : 2030-01-01
    • Date Range : Select Daily [1].
  5. Script It : On the final page, select Script to New Query Window rather than running it immediately. This allows you to review the thousands of generated boundary lines [1, 2].

To maintain a partitioned table---especially one with daily partitions ---you primarily use Sliding Windowoperations. This allows you to add new days and archive old ones without moving data row-by-row.

1. Adding a New Partition (SPLIT)

To prepare for tomorrow's data, you "split" the last partition. Warning: The target filegroup must be empty or the operation will be slow.

sql 复制代码
-- 1. Update the Scheme to tell it where the next partition goes
ALTER PARTITION SCHEME psDaily 
NEXT USED [PRIMARY];

-- 2. Split the Function to add the new boundary (e.g., Jan 2, 2030)
ALTER PARTITION FUNCTION pfDaily() 
SPLIT RANGE ('2030-01-02');

2. Removing Old Data (TRUNCATE or SWITCH)

If you want to delete a specific day's data instantly:

  • Option A: Truncate a specific partition (Fastest, SQL 2016+)

    sql 复制代码
    TRUNCATE TABLE Sales WITH (PARTITIONS (1)); -- Removes all rows in Partition 1
  • Option B: Switch to an Archive table (Best for moving data to history)

    sql 复制代码
    -- Move Partition 1 data to an identical empty table
    ALTER TABLE Sales SWITCH PARTITION 1 TO Sales_Archive;

3. Merging Partitions (MERGE)

After archiving or deleting data, you "merge" the empty partition boundary to keep the total number of partitions manageable.

sql 复制代码
-- Removes the boundary for Jan 1, 2020, merging that range into the next one
ALTER PARTITION FUNCTION pfDaily() 
MERGE RANGE ('2020-01-01');

4. Checking Partition Health

Use this query to see exactly how many rows are in each day and which partition number they belong to:

sql 复制代码
SELECT 
    p.partition_number,
    f.name AS [PartitionFunction],
    rv.value AS [BoundaryValue],
    p.rows
FROM sys.partitions p
JOIN sys.indexes i ON p.object_id = i.object_id AND p.index_id = i.index_id
JOIN sys.partition_schemes s ON i.data_space_id = s.data_space_id
JOIN sys.partition_functions f ON s.function_id = f.function_id
LEFT JOIN sys.partition_range_values rv ON f.function_id = rv.function_id 
    AND p.partition_number = rv.boundary_id + 1
WHERE p.object_id = OBJECT_ID('Sales') AND i.index_id <= 1;

5. Index Maintenance

You can rebuild indexes for a single partition instead of the whole table, which saves time and transaction log space.

sql 复制代码
ALTER INDEX PK_Sales ON Sales 
REBUILD PARTITION = 500; -- Rebuilds only the 500th day/partition
相关推荐
就叫飞六吧2 天前
docker快速启动sqlserver实例并自动测试shell脚本
docker·容器·sqlserver
雾岛听风6913 天前
Sql server
数据库·sql·sqlserver
满昕欢喜4 天前
回顾与总结
数据库·sqlserver
wenha5 天前
数据库隔离级别
数据库·mysql·sqlserver·隔离级别
~小先生~5 天前
sqlserver 外键、级联使用
数据库·sqlserver
Mr_pyx6 天前
Java 注解(Annotation)详解:从基础到 APT 实战
java·数据库·sqlserver
弱水三千 只取一瓢饮9 天前
sqlserver 从数据库A的备份文件,还原到数据库B中
数据库·sqlserver
百锦再9 天前
时序数据库选型指南:大数据时代的“数据基建”与 IoTDB 的工业原生之路
大数据·数据库·mysql·oracle·sqlserver·时序数据库·iotdb
UpYoung!10 天前
【数据库工具】DBeaver——轻量化多功能数据库连接工具DBeaver Ultimate 24.0.0版详细下载安装指南
数据库·mysql·sqlserver·数据库开发·数据库管理·dbeaver·数据库工具
代码雕刻家10 天前
MySQL与SQL Server的基本指令
数据库·mysql·sqlserver