简体   繁体   English

如何列出分区函数中描述的分区?

[英]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.附加分区在 RANGE RIGHT 分区功能的左侧,右侧是 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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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