在 SQL Server 数据库中查找分区架构定义

Posted

技术标签:

【中文标题】在 SQL Server 数据库中查找分区架构定义【英文标题】:Find Partition Schema Definitions in SQL Server Database 【发布时间】:2014-09-12 17:54:42 【问题描述】:

我可以访问数据库,我需要知道数据库中的分区方案定义。即我需要知道分区方案名称,它使用哪个分区功能,分配的分区是哪些文件组,等等......

例如有人这样创建分区方案(取自 msdn):

CREATE PARTITION SCHEME myRangePS1
AS PARTITION myRangePF1
TO (test1fg, test2fg, test3fg, test4fg);

那我想要 名称:myRangePS1, 函数:myRangePF1, 和分区:(test1fg,test2fg,test3fg,test4fg), 是不是分区ALL

我将如何仅使用 SQL 语句来解决这个问题? 我可以通过系统视图sys.partition_scheme查询分区的名称和一些数据,但是还不够。

下面显示了查找分区函数定义的类似解决方案: http://social.msdn.microsoft.com/forums/sqlserver/en-US/d0ce92e3-bf48-455d-bd89-c334654d7e97/how-to-find-partition-function-text-applied-to-a-table

【问题讨论】:

【参考方案1】:

请尝试以下查询: 1)

  select ps.Name AS PartitionScheme, pf.name AS PartitionFunction,fg.name AS FileGroupName  
     from sys.indexes i  
     JOIN sys.partitions p ON i.object_id=p.object_id AND i.index_id=p.index_id  
     join sys.partition_schemes ps on ps.data_space_id = i.data_space_id  
     join sys.partition_functions pf on pf.function_id = ps.function_id  
     join sys.allocation_units au  ON au.container_id = p.hobt_id   
     join sys.filegroups fg  ON fg.data_space_id = au.data_space_id  
    where i.object_id = object_id('TableName') 

或更多详细信息,请使用以下查询(SQL 2008 Internals Book)

2)

SELECT 
ISNULL(quotename(ix.name),'Heap') as IndexName 
,ix.type_desc as type
,prt.partition_number
,prt.data_compression_desc
,ps.name as PartitionScheme
,pf.name as PartitionFunction
,fg.name as FilegroupName
,case when ix.index_id < 2 then prt.rows else 0 END as Rows
,au.TotalMB
,au.UsedMB
,case when pf.boundary_value_on_right = 1 then 'less than' when pf.boundary_value_on_right is null then '' else 'less than or equal to' End as Comparison
,fg.name as FileGroup
,rv.value
FROM sys.partitions prt
inner join sys.indexes ix
on ix.object_id = prt.object_id and
ix.index_id = prt.index_id
inner join sys.data_spaces ds
on ds.data_space_id = ix.data_space_id
left join sys.partition_schemes ps
on ps.data_space_id = ix.data_space_id
left join sys.partition_functions pf
on pf.function_id = ps.function_id
left join sys.partition_range_values rv
on rv.function_id = pf.function_id AND
rv.boundary_id = prt.partition_number
left join sys.destination_data_spaces dds
on dds.partition_scheme_id = ps.data_space_id AND
dds.destination_id = prt.partition_number
left join sys.filegroups fg
on fg.data_space_id = ISNULL(dds.data_space_id,ix.data_space_id)
inner join (select str(sum(total_pages)*8./1024,10,2) as [TotalMB]
,str(sum(used_pages)*8./1024,10,2) as [UsedMB]
,container_id
from sys.allocation_units
group by container_id) au
on au.container_id = prt.partition_id
WHERE prt.OBJECT_ID = object_id(N'dbo.test')
order by ix.type_desc;

【讨论】:

嘿,谢谢!第一个查询缺少关于哪个分区函数部分属于哪个文件组的信息,但第二个查询有此信息【参考方案2】:

我修改了 knkarthick24 的第一个查询以显示与每个文件组关联的分区函数值:

select distinct ps.Name AS PartitionScheme, pf.name AS PartitionFunction,fg.name AS FileGroupName, rv.value AS PartitionFunctionValue
    from sys.indexes i  
    join sys.partitions p ON i.object_id=p.object_id AND i.index_id=p.index_id  
    join sys.partition_schemes ps on ps.data_space_id = i.data_space_id  
    join sys.partition_functions pf on pf.function_id = ps.function_id  
    left join sys.partition_range_values rv on rv.function_id = pf.function_id AND rv.boundary_id = p.partition_number
    join sys.allocation_units au  ON au.container_id = p.hobt_id   
    join sys.filegroups fg  ON fg.data_space_id = au.data_space_id  
where i.object_id = object_id('TableName') 

这是我正在寻找的查询,我希望其他人可以使用它!

【讨论】:

以上是关于在 SQL Server 数据库中查找分区架构定义的主要内容,如果未能解决你的问题,请参考以下文章

sql server分区

HGDB的分区表实现SQL Server的分区视图

SQL Server 2005/2008/2012中应用分布式分区视图

动态 SQL Server 查询循环遍历架构查找主键重复

SQL Server-数据库架构和对象定义数据完整性

SQL Server:查找学生在自定义日期内的连续缺勤计数