sp_tblCleanupRetentionWindow
Clone this wiki locally
sp_tblCleanupRetentionWindow
Drops all partitions in all affected tables prior retention window
The stored procedure takes the @pfName
parameter which identifies partition function used by partitoned table(s). Then based on the @retentionWindow
parameter it identifies partition containing the @retentionWindow
value.
The @retentionWindow
identifies partitioning value which must be kept. All values prior that one can be cleared. If the @retentionWindow
value is part of the first (leftmost) partition, the procedure ends and does not perform any cleanup.
If the @retentionWindow
is part of second and higher partitions, it scans for all Partition Schemes using the @pfName
partition fucntion. Then it iterates through all tables using the partitions schemes.
Each partition prior the partition containing the @retentionWindow
value in each table using the @pfName
partition function is then TRUNCATED.
After the truncation the procedure merges all the partition prior the partition containing the @retentionWindow
value to the first (leftmost) partition.
It keeps always the first left most partition empty
Usage
sp_tblCleanupRetentionWindow [parameters]
Parameters
Parameter | Data Type | Default | Description |
---|---|---|---|
@pfName |
nvarchar(128) |
NULL |
Name of the partition function. All associated partition schemes and tables will be cleaned according specified @retentionWindow
|
@retentionWindow |
sql_variant |
NULL |
|
@infoOnly |
bit |
1 |
When 1 prins only information about affected tables and partitions and does not perfrom any cleanup. When 0 does the actual cleanup. |
Samples
Cleanup all tables associated with the pf_partitionByDate
partition function.
Cleanup and merge all partitions prior partition containing value of @retentionWindow = '2021-05-01'
.
pf_partitionByDate
is using date
data type
DECLARE @retentionWindow date = '20201-05-01'
EXEC [sp_tblCleanupRetentionWindow]
@pfName = 'pf_PartitionByDate'
,@retentionWindow = @retentionWindow
,@infoOnly = 0