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 天前
sqlserver模糊查询问题
java·数据库·sqlserver
zxrhhm3 天前
SQLServer限制特定数据库的CPU使用率,确保关键业务系统有足够的资源
数据库·sqlserver
VIV-3 天前
医院病房管理系统的数据库设计(SQL Server)
数据库·sql·sqlserver
APguantou5 天前
NCRE-三级数据库技术-第9章-安全管理
数据库·安全·sqlserver
APguantou8 天前
NCRE-三级数据库技术-第12章-备份与数据库恢复
数据库·sqlserver
WarPigs9 天前
SQL Server笔记
服务器·数据库·sqlserver
遇见你...9 天前
A02 Spring-IOC和DI注解开发
数据库·spring·sqlserver
无人机90110 天前
Delphi网络编程综合实战:多协议网络工具开发(TCP/UDP/HTTP三合一)
sqlserver·eureka·时序数据库·etcd
lifewange11 天前
Java 自动化测试参数化实现
java·数据库·sqlserver