这是 Kendra Little 对分区的一个很好的入门。它应该可以帮助您回答是否进行分区的问题。 http://www.brentozar.com/archive/2012/03/how-decide-if-should-use-table-partitioning/
我的一个建议是确保命中表的每个查询都在谓词中使用分区消除。
至于文件组,请记住分区方案将分区映射到文件组。如果您想为每个租户执行 1 个文件组,这可能会变得复杂。
对于 SQL Server 2005 - 2008 R2,1,000 个分区是一个表可以包含的最大值。在 2012 年,他们将限制增加到 15,000 个分区。如果您需要更多,请将分区值隔开,并让范围确定数据将进入哪个分区。
这是一个表值函数,可用于按分区导出空间使用情况:
CREATE FUNCTION tvfPartitionAllocationDetails (@schema_name sysname, @table_name sysname)
RETURNS TABLE
AS
RETURN
select f.data_space_id,
f.NAME AS file_group_name,
SCHEMA_NAME(t.schema_id) AS table_schema,
t.name AS table_name,
[HOBT?] = CASE pst.index_id WHEN 0 THEN 'HEAP' WHEN 1 THEN 'B-TREE' END,
p.partition_number,
ps.name AS partition_scheme_name,
pf.name AS partition_function_name,
partition_function_range = CASE pf.boundary_value_on_right WHEN 1 THEN 'RIGHT' WHEN 0 THEN 'LEFT' END,
left_prv.value AS left_range,
right_prv.value AS right_value,
ISNULL(STR(CAST(left_prv.value AS BIGINT)), '-INF')
+ CASE WHEN pf.boundary_value_on_right = 0 THEN ' < '
ELSE ' <= '
END + 'X' + CASE WHEN pf.boundary_value_on_right = 0 THEN ' <= '
ELSE ' < '
END + ISNULL(STR(CAST(right_prv.value AS BIGINT)), 'INF') AS range_desc
,SUM(used_page_count) * 8 [TableSpaceUsed(KB)]
,(SELECT SUM(ISNULL(used_page_count,0)) * 8 FROM sys.dm_db_partition_stats WHERE object_id = p.OBJECT_ID AND partition_number = p.partition_number AND index_id > 1) [NCIndexSpaceUsed(KB)]
,SUM(used_page_count) used_page_count
,row_count
from sys.dm_db_partition_stats pst
INNER JOIN sys.partitions p ON pst.partition_id = p.partition_id
JOIN sys.tables t
ON p.object_id = t.object_id
JOIN sys.indexes i
ON p.object_id = i.object_id
AND p.index_id = i.index_id
JOIN sys.allocation_units au
ON p.hobt_id = au.container_id
JOIN sys.filegroups f
ON au.data_space_id = f.data_space_id
LEFT JOIN sys.partition_schemes ps
ON ps.data_space_id = i.data_space_id
LEFT JOIN sys.partition_functions pf
ON ps.function_id = pf.function_id
LEFT JOIN sys.partition_range_values left_prv
ON left_prv.function_id = ps.function_id
AND left_prv.boundary_id + 1 = p.partition_number
LEFT JOIN sys.partition_range_values right_prv
ON right_prv.function_id = ps.function_id
AND right_prv.boundary_id = p.partition_number
where pst.object_id = object_id(quotename(@schema_name) + '.' + quotename(@table_name))
AND used_page_count > 0
AND pst.index_id IN (0,1)/*Remove Nonclustered index counts*/
GROUP BY f.data_space_id,
f.NAME,
t.schema_id,
t.name,
p.partition_number,
ps.name,
pf.name,
pf.boundary_value_on_right,
left_prv.value,
right_prv.value,
ISNULL(STR(CAST(left_prv.value AS BIGINT)), '-INF')
+ CASE WHEN pf.boundary_value_on_right = 0 THEN ' < '
ELSE ' <= '
END + 'X' + CASE WHEN pf.boundary_value_on_right = 0 THEN ' <= '
ELSE ' < '
END + ISNULL(STR(CAST(right_prv.value AS BIGINT)), 'INF') ,
row_count,
p.OBJECT_ID,
pst.index_id;
然后你可以像这样查询表值函数:
SELECT * FROM dbo.tvfPartitionAllocationDetails('dbo','mytablename');
这假定没有超出行或 lob 页面。如果你有这些,并且想要显示它们,它们可以很容易地添加到函数中。