仅供参考,这就是我的做法。我想将“桶”放入一个持久表中,并且每两周只“重新计算”一次。否则,我担心如果我每天计算这些桶,一张桌子可能会从一个桶跳到下一个桶。但是,我想经常重新计算模式和 DDL 修改。这是那个片段。
-------------------------------------------------------------------------------------
--Get the total table size (by rows)
-------------------------------------------------------------------------------------
if object_id('tempdb..#Space') is not null
drop table #Space
SELECT
TableName = t.NAME,
Schem = s.name,
Pages = sum(a.total_pages),
Grp = row_number() over (order by sum(a.total_pages) desc)
INTO #Space
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN
sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN
sys.schemas s ON t.schema_id = s.schema_id
WHERE
t.NAME NOT LIKE 'dt%'
AND t.is_ms_shipped = 0
AND i.OBJECT_ID > 255
GROUP BY
t.Name, s.name
-------------------------------------------------------------------------------------
--split the tables into 7 buckets by:
--updating the Grp to the Grp with the lowest cumulative sum of all members by
--ordering by the current cumulative sum of all members
-------------------------------------------------------------------------------------
declare @ct int = 8
while @ct <= (select max(Grp) from #Space)
begin
update S
set Grp = (select top 1 Grp from #Space where Grp < 8 order by sum(Pages) over (partition by Grp) asc)
from #Space S
where S.Grp = @ct
set @ct = @ct + 1
end
insert into AdminTools..TableSpace (TableName
,Schem
,Pages
,Grp
,GrpPages
,LoadDate)
select
TableName
,Schem
,Pages
,Grp
,GrpPages = sum(Pages) over (partition by Grp)
,LoadDate = getdate()
from #Space
end