SQL Server › Partitioning

How do you determine which partition a specific row of data belongs to, and how can this help with troubleshooting?

Intermediate

Answer

You can use the $PARTITION system function along with the name of the partition function, passing in the value from the partitioning column, which returns the specific partition number that value belongs to, helping you verify your understanding of the partition boundaries or troubleshoot unexpected query behavior.
SELECT $PARTITION.SalesDateRange(SaleDate) AS PartitionNumber, *
FROM Sales;
Real-world example A developer troubleshooting an unexpectedly slow query uses the $PARTITION function to confirm that certain SaleDate values were actually falling into a different partition than originally expected, revealing a boundary configuration mistake.

Common follow-ups: How do you view how many rows currently exist in each partition of a table?;What tools help visualize the current partition boundaries defined on a table?