Eager to move a table to another filegroup? We can help you with it.
Here at Bobcares, we have seen several such database-related errors as part of our Server Management Services for web hosts and online service providers.
Today we will take a look at how we can move the table into another filegroup.
Why moving a table to another filegroup is necessary
Sometimes it is very necessary to move the SQL Server tables between two filegroups or create a copy of a table in a different filegroup. The main purpose of having a copy of a table in a different filegroup could be for archiving historical data, disk space issues, using a copy of the table for reporting or for testing purposes.
For instance, a database located on D drive is grown large in size and the drive is running out of space. There are other drives on the server that has enough free disk space. So here, the immediate and easiest solution would be to move the large tables from D drive to a different drive where there is enough space to make some room for the database.
How we move the table to another filegroup
Now let’s see how our Support Engineers assist our customers in moving a table.
1. First, identify the large tables that we will be moving to another filegroup placed on drive E.
2. Now, create a new filegroup on disk E. In case, if you already have a filegroup on this drive then you can use it.
3. Then add a data file to this newly created filegroup.
4. Drop the clustered index from the table that has been identified for the movement. Then recreate it for another filegroup.
5. Finally, create a clustered index on the same table.
Move a table to a different Filegroup using T-SQL codes
You can also move the table from one filegroup to another filegroup by dropping and recreating the clustered indexes. Here are the T-SQL codes that will move the complete table from filegroup1 to filegroup2.
ALTER TABLE TAB1 DROP CONSTRAINT PK_TAB1 WITH (MOVE TO TEST_DATA_2)
GO
ALTER TABLE TAB1 ADD CONSTRAINT PK_TAB1 PRIMARY KEY(TAB1_ID)
GO
where TAB1 is the table name, PK_TAB1 is the clustered index key and TEST_DATA_2 is the target filegroup. However, it isn’t necessary to create another filegroup into a separate disk. You can use this method if you are moving a table from one filegroup to another filegroup irrespective of the filegroup drives.
In case, if your table doesn’t have a clustering index, you can still move the table by creating a clustering index and then follow the same process to move it.
Moving an existing index using Table Designer
1. First, in Object Explorer, click on the plus sign to expand the database that contains the table containing the index that you want to move.
2. Next, click the plus sign to expand the Tables folder.
3. Now right-click on the table containing the index that you want to move and select Design.
4. Click Indexes/Keys on the Table Designer menu.
5. Now select the index that you wish to move.
6. Expand Data Space Specification in the main grid.
7. Then select the Filegroup or Partition Scheme Name and select from the list the filegroup or partition scheme to where you want to move the index.
8. After that, click on the Close button.
9. Finally, on the File menu select Savetable_name.
[Need any further assistance with Database queries? – We are here to help you]
Conclusion
In short, moving a table to another filegroup can be done manually, or by using T-SQL codes. It can also be done using a Table designer. Today, we saw how our Support Engineers do it.
0 Comments