Saturday, 16 February 2013

Are You Approaching Your Partition Range Limits?

For those of you who use table partitioning, you know that you need to define a partitioning scheme and function prior to applying partitioning to an index. Personally, I tend to build the function for a couple of years out, and I tend to create them through the end of a calendar year. Now, if I failed to expand a partition range at the end of the year, then come January 1st, all of my data would be written to the same partition. Not the end of the world, no, but it causes all kinds of nasty performance and maintenance issues.

IF OBJECT_ID('tempdb..#Results') IS NOT NULL
    DROP TABLE #Results;

      databaseName  NVARCHAR(128)
    , schemaName    NVARCHAR(128)
    , functionName  NVARCHAR(128)
    , data_space_id INT
    , maxRangeValue SQL_VARIANT

/* Grab results for each database and store in our temp table.
   And no, I don't *need* to select from sys.indexes and perform
   left joins, but I'm overly cautious and want to make sure
   I'm not accidentally missing any databases. :) */

--EXECUTE master.dbo.sp_msforeachdb
EXECUTE sp_foreachdb 'USE ?;
SELECT DB_NAME() AS databaseName
    , AS schemaName
    , AS functionName
    , sps.data_space_id
    , MAX(prv.value) AS maxRangeValue
FROM sys.indexes AS i
LEFT JOIN sys.partition_schemes AS sps WITH (NOLOCK)
    ON i.data_space_id = sps.data_space_id
LEFT JOIN sys.partition_functions AS spf WITH (NOLOCK)
    ON sps.function_id = spf.function_id
LEFT JOIN sys.partition_range_values AS prv WITH (NOLOCK)
    ON spf.function_id = prv.function_id
    , sps.data_space_id;';
    sp_foreachdb was written by SQL MVP Aaron Bertrand and can be downloaded
    Alternatively, you can also use sys.sp_MSforeachdb

/* Make sure we're not missing any major databases */
SELECT * FROM sys.databases WHERE name NOT IN (SELECT databaseName FROM #Results);

/* Retrieve our results */
FROM #Results
ORDER BY maxRangeValue;

With the max limit of partitions to be 1000 in SQL Server 2008 it is very possible to run against this limit depending on how you partition your table/index. Excellent script for watching those limits. But as people move to SP2 of SQL Server 2008 and SP1 on SQL Server 2008 R2 they can take advantage of 15,000 partitions.

No comments:

Post a Comment