Table Partitioning in SQL Server
Table Partitioning in SQL Server
Basics
This post is part 1 of 2 in the series Table Partitioning in SQL Server
There are many benefits of partitioning large tables. You can speed up loading
and archiving of data, you can perform maintenance operations on individual
partitions instead of the whole table, and you may be able to improve query
performance. However, implementing table partitioning is not a trivial task
and you need a good understanding of how it works to implement and use it
correctly.
This example illustration is used throughout this blog post to explain basic
concepts. The table contains data from every day in 2012, 2013, 2014 and
2015, and there is one partition per year. To simplify the example, only the first
and last day in each year is shown.
In the example illustration the date column is used as the partition column.
SQL Server places rows in the correct partition based on the values in the date
column. All rows with dates before or in 2012 are placed in the first partition,
all rows with dates in 2013 are placed in the second partition, all rows with
dates in 2014 are placed in the third partition, and all rows with dates in 2015
or after are placed in the fourth partition. If the partition column value is NULL,
the rows are placed in the first partition.
The partition function defines how to partition data based on the partition
column. The partition function does not explicitly define the partitions and
which rows are placed in each partition. Instead, the partition function
specifies boundary values, the points between partitions. The total number of
partitions is always the total number of boundary values + 1.
In the example illustration there are three boundary values. The first boundary
value is between 2012 and 2013, the second boundary value is between 2013
and 2014, and the third boundary value is between 2014 and 2015. The three
boundary values create four partitions. (The first partition also includes all
rows with dates before 2012 and the last partition also includes all rows after
2015, but the example is kept simple with only four years for now.)
Left and right partitions make more sense if the table is rotated:
→ →
Range Left and Range Right using Dates
The first boundary value is between 2012 and 2013. This can be created in two
ways, either by specifying a range left partition function with December 31st
as the boundary value, or as a range right partition function with January 1st
as the boundary value:
→ →
Partition functions are created as either range left or range right, it is not
possible to combine both in the same partition function. In a range left
partition function, all boundary values are upper boundaries, they are the last
values in the partitions. If you partition by year, you use December 31st. If you
partition by month, you use January 31st, February 28th / 29th, March 31st,
April 30th and so on. In a range right partition function, all boundary values
are lower boundaries, they are the first values in the partitions. If you partition
by year, you use January 1st. If you partition by month, you use January 1st,
February 1st, March 1st, April 1st and so on:
→
Range Left and Range Right using the Wrong Dates
If the wrong dates are used as boundary values, the partitions incorrectly span
two time periods:
→
What is a Partition Scheme?
The partition scheme maps the logical partitions to physical filegroups. It is
possible to map each partition to its own filegroup or all partitions to one
filegroup.
A filegroup contains one or more data files that can be spread on one or more
disks. Filegroups can be set to read-only, and filegroups can be backed up and
restored individually. There are many benefits of mapping each partition to its
own filegroup. Less frequently accessed data can be placed on slower disks
and more frequently accessed data can be placed on faster disks. Historical,
unchanging data can be set to read-only and then be excluded from regular
backups. If data needs to be restored it is possible to restore the partitions
with the most critical data first.
/* --------------------------------------------------
-- https://www.itprotoday.com/sql-server/virtual-auxiliary-table-numbers
-------------------------------------------------- */
GO
WITH
GO
/* ------------------------------------------------------------
------------------------------------------------------------ */
-- Drop objects if they already exist
AS PARTITION pfSales
ALL TO ([Primary]);
SalesDate DATE,
Quantity INT
) ON psSales(SalesDate);
SELECT
OBJECT_SCHEMA_NAME(pstats.object_id) AS SchemaName
,OBJECT_NAME(pstats.object_id) AS TableName
,ps.name AS PartitionSchemeName
,ds.name AS PartitionFilegroupName
,pf.name AS PartitionFunctionName
,prv.value AS PartitionBoundaryValue
,c.name AS PartitionKey
,CASE
WHEN pf.boundary_value_on_right = 0
END AS PartitionRange
,pstats.partition_number AS PartitionNumber
,pstats.row_count AS PartitionRowCount
,p.data_compression_desc AS DataCompression
Summary
The partition function defines how to partition a table based on the values in
the partition column. The partitioned table is created on the partition scheme
that uses the partition function to map the logical partitions to physical
filegroups.
This post is the first in a series of Table Partitioning in SQL Server blog posts. It
covers the basics of partitioned tables, partition columns, partition functions
and partition schemes. Future blog posts in this series will build upon this
information and these examples to explain other and more advanced
concepts.