I have a table partitioned by a date column, and each partition stores one month of data.
Furthermore, each partition is associated with one filegroup, and each filegroup has exactly one db file (NDF).
My setup is pretty simple:
CREATE PARTITION FUNCTION MyPF (DATE)
AS RANGE RIGHT FOR VALUES (
'2019-09-01',
'2019-10-01',
'2019-11-01',
'2019-12-01');
CREATE PARTITION SCHEME MyPS PARTITION MyPF
TO (
FG_2019_08,
FG_2019_09,
FG_2019_10,
FG_2019_11);
I need to truncate a random month on a regular basis (sometimes even from the middle, without touching other months).
Instead of doing complicated switch out, I figured out from BOL, that TRUNCATE TABLE has WITH option in my SQL Server 2017, so I can just say:
TRUNCATE TABLE MyTable WITH (PARTITIONS(3));
TRUNCATE TABLE MyTable WITH (PARTITIONS(1));
which will remove all rows from partition from the files associated with FG_2019_08 and FG_2019_10.
That works great, and now I have an empty file, but SQL Server doesn't allow to drop: It says it still in use. My understanding, that I still need to adjust the partition function and partition schema , in order to remove the empty NDF files.
I checked out BOL, but I cannot wrap my head around how to alter (merge?) the boundary values, to reflect the changes above. They should become something like this, right?:
ALTER PARTITION FUNCTION MyPF (DATE)
AS RANGE RIGHT FOR VALUES (
'2019-10-01',
'2019-12-01');
ALTER PARTITION SCHEME MyPS PARTITION MyPF
TO (
FG_2019_09,
FG_2019_11);
Could anybody tell how could I alter these two object, if I need to alter at all?