To answer this question, we must first understand
- If the table does not have an index, its data is called a heap.
- If a table has a clustered index, that index effectively represents your table data. Therefore, if you move a clustered index, you will also move your data.
The first step is to find out more information about the table that we want to move. We do this by executing this T-SQL:
sp_help N'<<your table name>>'
The output column will be "Data_located_on_filegroup". This is a convenient way to find out which filegroup your table data is included in. But more important is the output, which shows you information about table indexes. (If you want to see information about table indexes, just run sp_helpindex N'<<your table name>>' ). Your table may have 1) no indexes (so this is a bunch), 2) one index, or 3) several indexes. If index_description starts with "clustered, unique, ...", this is the index you want to move. If the index is also the primary key, thatโs fine, you can still move it.
To move the index, look at the index_name and index_keys shown in the results of the above help query, then use them to populate <<blanks>> in the following query:
CREATE UNIQUE CLUSTERED INDEX [<<name of clustered index>>] ON [<<table name>>]([<<column name the index is on - from index_keys above>>]) WITH DROP_EXISTING, ONLINE ON <<name of file group you want to move the index to>>
DROP EXISTING, ONLINE values โโabove. DROP EXISTING ensures that the index is not duplicated, and ONLINE saves the table on the network while you move it.
If the index you are moving is not a clustered index, replace UNIQUE CLUSTERED above with NONCLUSTERED
To move the heap table, add a clustered index to it, then run the above statement to move it to another file group, and then release the index.
Now go back and run sp_help in your table and check the results to see where the table and index data are now.
If your table has several indexes, then after running the above statement to move the cluster index, sp_helpindex will show that your cluster index is in the new file group, but any remaining indexes will still be in the original file group. The table will continue to function normally, but you should have a good reason why you need indexes located in different filegroups. If you want the table and all its indexes to be in the same filegroup, repeat the above instructions for each index, replacing CREATE [NONCLUSTERED, or other] ... DROP EXISTING... as needed, depending on the type of index you are moving.