SQL Server : Using $partition Function

Recently, I needed to validate date-range value on each partition on a partitioned table.  To do this, I utilized the SQL Server "$Partition" function.  Per MSDN, "$Partition returns the partition number into which a set of partitioning column values would be mapped for any specified partition function."

 

The syntax is:

 

[ database_name. ] $PARTITION.partition_function_name(expression)

 

This handy function can also be used to obtain column information, row counts, and other information you might want to know per partition:

 

–STEP ONE:

–Get the name of your partition function:

SELECT NAME FROM SYS.partition_functions

 

NAME

TableAPartitionFunc

 

–STEP TWO:

–Get the name of the column from which your table is partitioned

SELECT c.name AS PartitionedColumnName

FROM  sys.tables t

JOIN  sys.indexes  i

ON (i.object_id = t.object_id

AND i.index_id < 2)

JOIN  sys.index_columns  ic

ON(ic.partition_ordinal > 0

AND ic.index_id = i.index_id and ic.object_id = t.object_id)

JOIN  sys.columns c

ON (c.object_id = ic.object_id

AND c.column_id = ic.column_id)

WHERE t.object_id = LOWER(object_id(‘yourtablename’))

 

PartitionedColumnName

DatePort

 

–STEP THREE:

–Use acquired data to query your partition values:

 

SELECT DISTINCT $partition.TableAPartitionFunc(DatePort) AS PartitionNumber,

CONVERT(VARCHAR(8), DatePort, 1) AS RangedDate, ‘yourtablename’ AS PartitionedTable

from yourtablename

ORDER BY $partition. TableAPartitionFunc(DatePort)

 

PartitionNumber

RangedDate

PartitionedTable

1

5/13/2013

yourtablename

2

5/14/2013

yourtablename

3

5/15/2013

yourtablename

4

5/16/2013

yourtablename

 

SQL Server : Query For A Quick Overview Of Your Partitions

— =============================================

— This is a basic query intended to provide

— a quick look at partition info.  

— =============================================

 

SELECT b.name as ‘table_name’, a.partition_number,

a.[rows] as ‘row_count’, a.partition_id,

a.object_id, a.hobt_id,

b.schema_id, c.name as ‘schema_name’, 

a.index_id, b.create_date, b.modify_date

FROM  sys.partitions AS a INNER JOIN

sys.objects AS b ON a.object_id = b.object_id

INNER JOIN sys.schemas AS c On c.schema_id = b.schema_id              

WHERE LOWER(b.name) = ‘yourPartitionedTableName’

–pull only first index for a simpler view

and a.index_id < 2

ORDER BY b.name, partition_number;

 

— =============================================

–Helpful info for a deeper dive

— =============================================

 

SELECT * FROM SYS.partition_range_values ;

SELECT * from sys.partitions;

SELECT * FROM SYS.partition_functions;

SELECT * FROM sys.partition_schemes;

SELECT * from sys.schemas;

SELECT * FROM sys.filegroups;

 

–query sys.partitions to view and verify that the table contains partitions as expected

SELECT * FROM sys.partitions where OBJECT_NAME(Object_Id)=‘yourPartitionedTableName’;

SELECT * FROM sys.objects where OBJECT_NAME(Object_Id)=‘yourPartitionedTableName’;

SQL: Introduction To Partitioning

When a database table grows in size to hundreds of gigabytes or more, it can become more difficult to load new data, remove old data, and maintain indexes. Just the sheer size of the table causes simple operations to take much longer than they might otherwise. Data that must be loaded or removed can be very sizable, making INSERT and DELETE operations on the table impractical. Database software, such as is provided with SQL Server, provides table partitioning to make such operations more efficient.

If your database has a very large table, you can “partition” this large table onto separate “filegroups.”  Filegroups are special types of files that allows a user to divide data onto different disks for the purpose of optimizing query performance and maintenance operations.  For example, say you have a tall file cabinet where you keep lists of customer names. As the number of files grow, the longer it takes to locate any given file.  Database tables operate on the same principle. 

The first thing you will need to do when planning out a partitioned table is to decide how you will divide the table up.  As with our file cabinet example, we might decide to create one file cabinet where last names begin with A – F, another cabinet for files G – N, and another for files O – Z.  Likewise, our table might also be divided up using last names as a way to organize.  You can see how faster it would be to locate the last name “Jones” in one of three cabinets instead of a giant cabinet.

As business changes and data grows, partitioning can become more complicated; however, the example above does provide a basic understanding as to how partitioning can save time and resources.