sql-serversql-server-2008filegroup

How can I move a table to another filegroup in MS SQL Server?


I have SQL Server 2008 Ent and OLTP database with two big tables. How I can move these tables to another filegroup without service interrupting? Now, about 100-130 records inserted and 30-50 records updated each second in these tables. Each table have about 100M records and six fields (including one field geography).

I am looking for a solution in Google, but all solutions contain

create second table, insert rows from first table, drop first table, etc

Can I use partitioning functions for solving this problem?


Solution

  • If you want to just move the table to a new filegroup, you need to recreate the clustered index on the table (after all: the clustered index is the table data) on the new filegroup you want.

    You can do this with e.g.:

    CREATE CLUSTERED INDEX CIX_YourTable
       ON dbo.YourTable(YourClusteringKeyFields)
       WITH DROP_EXISTING
       ON [filegroup_name]
    

    or if your clustered index is unique:

    CREATE UNIQUE CLUSTERED INDEX CIX_YourTable
       ON dbo.YourTable(YourClusteringKeyFields)
       WITH DROP_EXISTING
       ON [filegroup_name]
    

    This creates a new clustered index and drop the existing one, and it creates the new clustered index in the file group you specified - et voila, your table data has been moved to the new filegroup.

    See the MSDN docs on CREATE INDEX for details on all available options you might want to specify.

    This of course doesn't yet deal with partioning, but that's a whole other story all to itself...