简体   繁体   中英

How can I list the partitions described in a partition function?

I created a partition function like

CREATE PARTITION FUNCTION PF_HASH_BY_VALUE (BIGINT) AS RANGE LEFT 
FOR VALUES (100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000, 900000)
GO

But, once created, I would like to list all the partitions described in it. How is it possible?

I'm aware of how to use the functions with the values of a table, but I would like to just list what I just defined.

SELECT 
    $PARTITION.PF_HASH_BY_VALUE([MY_VALUE]) as Partition_Number, 
    COUNT(*) as Row_Count
FROM 
    MATH.[dbo].[TBL_PRIMES]
GROUP BY 
    $PARTITION.PF_HASH_BY_VALUE([MY_VALUE]);

There's always one more partition than there are boundary values in the partition function. The additional partition is on the left in RANGE RIGHT partition function, and on the right for RANGE LEFT.

So something like this:

SELECT 
  tbl.name table_name,
  indx.name index_name,
  prt.value boundary_value,
  p.partition_number,
  prt.boundary_id,
  prt.value boundary_value,
  p.rows
FROM 
  sys.tables AS tbl
  JOIN sys.indexes AS indx 
    ON indx.object_id = tbl.object_id
  JOIN sys.partition_schemes ps 
    ON indx.data_space_id = ps.data_space_id
  JOIN sys.partition_functions pf 
    ON ps.Function_id = pf.Function_id 
  JOIN sys.partitions p 
    on p.object_id = indx.object_id
    and p.index_id = indx.index_id
  LEFT JOIN sys.partition_range_values prt 
    on prt.function_id = pf.function_id
    and prt.boundary_id + case when pf.boundary_value_on_right = 1 then 1 else 0 end = p.partition_number 
WHERE tbl.name  = 'MyTable'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM