查询以确定表中剩余的未使用分区

时间:2014-09-26 21:13:29

标签: sql sql-server

是否有人知道查询以确定表中剩余的未使用分区数?任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

下面是列出表的空分区以及其他相关信息的查询。此查询是从分区详细信息SSMS自定义报告(https://ssmspartcustomrpt.codeplex.com/)中收集的。您还可以使用开箱即用的SSMS数据库级别分区报告来获取此信息。

SELECT
    OBJECT_NAME(p.object_id) AS ObjectName, 
    i.name AS IndexName, 
    p.index_id AS IndexID, 
    ds.name AS PartitionScheme, 
    p.partition_number AS PartitionNumber, 
    fg.name AS FileGroupName, 
    prv_left.value AS LowerBoundaryValue, 
    prv_right.value AS UpperBoundaryValue, 
    CASE pf.boundary_value_on_right WHEN 1 THEN 'RIGHT' ELSE 'LEFT' END AS PartitionFunctionRange, 
    p.rows AS Rows
FROM
    sys.partitions AS p INNER JOIN
    sys.indexes AS i ON i.object_id = p.object_id AND i.index_id = p.index_id INNER JOIN
    sys.data_spaces AS ds ON ds.data_space_id = i.data_space_id INNER JOIN
    sys.partition_schemes AS ps ON ps.data_space_id = ds.data_space_id INNER JOIN
    sys.partition_functions AS pf ON pf.function_id = ps.function_id INNER JOIN
    sys.destination_data_spaces AS dds2 ON dds2.partition_scheme_id = ps.data_space_id AND dds2.destination_id = p.partition_number INNER JOIN
    sys.filegroups AS fg ON fg.data_space_id = dds2.data_space_id LEFT OUTER JOIN
    sys.partition_range_values AS prv_left ON ps.function_id = prv_left.function_id AND prv_left.boundary_id = p.partition_number - 1 LEFT OUTER JOIN
    sys.partition_range_values AS prv_right ON ps.function_id = prv_right.function_id AND prv_right.boundary_id = p.partition_number
WHERE
    p.object_id = OBJECT_ID(N'dbo.MyTable')
    AND p.rows = 0;