Monday 10 June 2013

Looking inside Partition (how many rows are stroed in which file groups?)


-- reference:
http://davidpeterhansen.com/view-partitions-in-sql-server/

SELECT
 s.name as SchemaName
 ,t.name as TableName
 ,c.name as PartitionColumn
 ,CASE boundary_value_on_right WHEN 1 THEN 'RIGHT' ELSE 'LEFT' END as PartionRangeType
 ,ps.name as PartitionName
 ,pf.name as PartitionFunction
 ,p.partition_number AS PartitionNumber
 ,CASE boundary_value_on_right WHEN 1 THEN '<' ELSE '<=' END as PartionComparisionType
 ,rv.value as PartitionValue
 ,fg.name AS FileGroupName
 ,p.rows AS RowsInPartition
 ,au.total_pages AS PagesInPartition
 ,au.total_pages*8/1024.0 AS MBsInPartition
 ,CONVERT(VARCHAR(6), CONVERT(INT, SUBSTRING (au.first_page, 6, 1) +SUBSTRING (au.first_page, 5, 1)))
  +':'
  + CONVERT (VARCHAR(20), CONVERT (INT, SUBSTRING (au.first_page, 4, 1) +SUBSTRING (au.first_page, 3, 1) +SUBSTRING (au.first_page, 2, 1) +SUBSTRING (au.first_page, 1, 1)))
 AS PartitionFirstPage
 FROM sys.schemas s
   INNER JOIN sys.tables t
   ON t.schema_id=s.schema_id
   -- check table storage type
   INNER JOIN  sys.indexes i
   ON (i.object_id = t.object_id
   -- 0: heap
   -- 1: clusterd
   and i.index_id in (0,1))
   INNER JOIN sys.partitions p
   ON p.object_id = i.object_id
   AND p.index_id = i.index_id
   INNER JOIN  sys.index_columns  ic
   ON (-- identify partioned column
   ic.partition_ordinal > 0
   and ic.index_id = i.index_id
   and ic.object_id = t.object_id)
   INNER JOIN  sys.columns c
   ON c.object_id = ic.object_id and c.column_id = ic.column_id
   INNER JOIN sys.system_internals_allocation_units au
   ON p.partition_id = au.container_id
   INNER JOIN sys.partition_schemes ps
   ON ps.data_space_id = i.data_space_id
   INNER JOIN sys.partition_functions pf
   ON pf.function_id = ps.function_id
   INNER JOIN sys.destination_data_spaces dds
   ON dds.partition_scheme_id = ps.data_space_id AND dds.destination_id = p.partition_number
   INNER JOIN sys.filegroups fg
   ON dds.data_space_id = fg.data_space_id
   LEFT JOIN sys.partition_range_values rv
   ON pf.function_id = rv.function_id AND p.partition_number = rv.boundary_id 
 WHERE -- only look for heap or clustered index
   i.index_id IN (0, 1)
 ORDER BY 1,2,7






No comments:

Post a Comment