Find Partition Schema Definitions in SQL Server Database

c9x0cxw0  于 2023-11-16  发布在  SQL Server
关注(0)|答案(3)|浏览(187)

I have access to a database and I need to know the Partition Scheme definitions in the database. i.e. I need to know the partition scheme name, which Partition function is it using, what file groups are the partitions assigned, etc...

For example someone creates a partition scheme as so (taken from msdn):

  1. CREATE PARTITION SCHEME myRangePS1
  2. AS PARTITION myRangePF1
  3. TO (test1fg, test2fg, test3fg, test4fg);

Then I want the name: myRangePS1, the function: myRangePF1, and the partitions: (test1fg, test2fg, test3fg, test4fg), Whether it is partition ALL or not

How would I go about this using SQL statements only? I can query the names and some data about partitions by using the system view sys.partition_scheme, but it is not enough.

The below shows a similar solution on finding the definition of Partition functions: http://social.msdn.microsoft.com/forums/sqlserver/en-US/d0ce92e3-bf48-455d-bd89-c334654d7e97/how-to-find-partition-function-text-applied-to-a-table

hjqgdpho

hjqgdpho1#

I have modified knkarthick24's first query to show Partition function values associated to each file group:

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

This is the query I was looking for and I hope other people can make use of this!

lbsnaicq

lbsnaicq2#

Please try this query: 1)

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

or for more detailed information use the below query( SQL 2008 Internals Book)

  1. SELECT
  2. ISNULL(quotename(ix.name),'Heap') as IndexName
  3. ,ix.type_desc as type
  4. ,prt.partition_number
  5. ,prt.data_compression_desc
  6. ,ps.name as PartitionScheme
  7. ,pf.name as PartitionFunction
  8. ,fg.name as FilegroupName
  9. ,case when ix.index_id < 2 then prt.rows else 0 END as Rows
  10. ,au.TotalMB
  11. ,au.UsedMB
  12. ,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
  13. ,fg.name as FileGroup
  14. ,rv.value
  15. FROM sys.partitions prt
  16. inner join sys.indexes ix
  17. on ix.object_id = prt.object_id and
  18. ix.index_id = prt.index_id
  19. inner join sys.data_spaces ds
  20. on ds.data_space_id = ix.data_space_id
  21. left join sys.partition_schemes ps
  22. on ps.data_space_id = ix.data_space_id
  23. left join sys.partition_functions pf
  24. on pf.function_id = ps.function_id
  25. left join sys.partition_range_values rv
  26. on rv.function_id = pf.function_id AND
  27. rv.boundary_id = prt.partition_number
  28. left join sys.destination_data_spaces dds
  29. on dds.partition_scheme_id = ps.data_space_id AND
  30. dds.destination_id = prt.partition_number
  31. left join sys.filegroups fg
  32. on fg.data_space_id = ISNULL(dds.data_space_id,ix.data_space_id)
  33. inner join (select str(sum(total_pages)*8./1024,10,2) as [TotalMB]
  34. ,str(sum(used_pages)*8./1024,10,2) as [UsedMB]
  35. ,container_id
  36. from sys.allocation_units
  37. group by container_id) au
  38. on au.container_id = prt.partition_id
  39. WHERE prt.OBJECT_ID = object_id(N'dbo.test')
  40. order by ix.type_desc;
展开查看全部
xxb16uws

xxb16uws3#

I modified the knkarthick24 script to make it a little more user-friendly in understanding the function definition. In addition, my script returns data for all tables that have been partitioned the user:

  1. SELECT
  2. ISNULL(quotename(ix.name),'Heap') as IndexName
  3. ,ix.type_desc as type
  4. ,prt.partition_number
  5. ,prt.data_compression_desc
  6. ,ps.name as PartitionScheme
  7. ,pf.name as PartitionFunction
  8. ,case when ix.index_id < 2 then prt.rows else 0 END as Rows
  9. ,au.TotalMB
  10. ,au.UsedMB
  11. ,s.[name]+'.'+t.[name] [TableName]
  12. ,c.name [PartByColName]
  13. ,case when (pf.boundary_value_on_right = 1 and rv.value is not null) then '<' when (pf.boundary_value_on_right = 0 and rv.value is not null) then '<=' else '' End as term
  14. ,rv.value
  15. ,fg.name as FilegroupName
  16. FROM sys.partitions prt
  17. inner join sys.indexes ix
  18. on ix.object_id = prt.object_id and ix.index_id = prt.index_id
  19. inner join sys.tables t
  20. on t.object_id = ix.object_id AND ix.type IN (0,1)
  21. JOIN sys.index_columns AS ic
  22. ON ic.[object_id] = ix.[object_id] AND ic.index_id = ix.index_id AND ic.partition_ordinal >= 1
  23. JOIN sys.columns AS c
  24. ON t.[object_id] = c.[object_id] AND ic.column_id = c.column_id
  25. inner join sys.schemas s
  26. ON s.schema_id=t.schema_id
  27. inner join sys.data_spaces ds
  28. on ds.data_space_id = ix.data_space_id
  29. left join sys.partition_schemes ps
  30. on ps.data_space_id = ix.data_space_id
  31. left join sys.partition_functions pf
  32. on pf.function_id = ps.function_id
  33. left join sys.partition_range_values rv
  34. on rv.function_id = pf.function_id AND rv.boundary_id = prt.partition_number
  35. left join sys.destination_data_spaces dds
  36. on dds.partition_scheme_id = ps.data_space_id AND dds.destination_id = prt.partition_number
  37. left join sys.filegroups fg
  38. on fg.data_space_id = ISNULL(dds.data_space_id,ix.data_space_id)
  39. 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
  40. on au.container_id = prt.partition_id
  41. where pf.is_system=0
  42. order by ps.name,rv.value,fg.name
展开查看全部

相关问题