简体   繁体   中英

SQL Server Table Partitioning Cannot drop Filegroup after Partition Switch

I have a huge table with around 110 partitions. I wish to archive the oldest partition and drop the FileGroup. Following is the strategy I adopted.

  1. Created an exact empty table tablename_archive and met all partitioning requirements.

  2. Perform Partition switch

     ALTER TABLE tablename SWITCH PARTITION 1 TO tablename_archive PARTITION 1 
  3. After verifying the switch (partition swap) , I dropped the archived table.

  4. Merged the Partition function using the first boundary value as follows

     ALTER PARTITION FUNCTION YMDatePF2 () MERGE RANGE ('2012-01-01 00:00:00.000') 

Although there is no data now on FG, when I try to drop the File or FG it errors out saying.

    The file 'XXXXXXXX' cannot be removed because it is not empty.

    The filegroup 'XXXXXXXX' cannot be removed because it is not empty.

Is there any change I need to make it to Partition scheme too, after merging the function.

Please let me know if you need any more details.

You can never remove the first (or only) partition from a RANGE RIGHT partition function (or conversely, the last (or only) partition of a RANGE LEFT function). The first (or last if RANGE LEFT ) filegroup from the underlying partition schemes can never be removed from the schemes either. Remember you have one more partition, and partition scheme filegroup mapping, than partition boundaries.

If your intent was to archive January 2012 data, you should have switched partition 2 rather than 1 because the first partition contained data less than '2012-01-01 00:00:00.000'. Now that the second partition has been merged, the first partition (and the first filegroup) contains data less than '2012-02-01T00:00:00.000', which includes January 2012 data.

With a RANGE RIGHT sliding window, it is best to plan to keep the first filegroup empty. You could used the PRIMARY filegroup or a dummy one with no files for that purpose. See Table Partitioning Best Practices .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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