Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Moving Large Table to Different File Group

By Raj Gujar, (first published: 2008/10/16)

We had a database that was growing very large in size and the database was located on the E: drive that had a 200 GB Capacity. The drive was almost 90% full and the disk was running out of disk space. We had other drives on the server where there was available disk space, so our immediate solutions was to move a few large tables from that drive to different drive.

Here is how we did it. First, we identified the large tables by using sp_spaceused to identify the space used by every table.

Next we decided to create a new filegroup with T-SQL (you can also use SSMS to create a new file group).

ALTER DATABASE SALES ADD FILEGROUP [SECONDERYDATA]

We then had to create a file to point the new Filegroup to a new drive:

ALTER DATABASE SALES
ADD FILE
( NAME = XFILENAME,
FILENAME = 'new path\SALES.ndf',
SIZE = 1MB,
MAXSIZE = 10MB,
FILEGROWTH = 1MB)
TO FILEGROUP [SECONDERYDATA]
GO

Now the database knows that there is another filegroup that it could use for data. Remember the Server \ Database will not start to create new files in the new file group, you will have to explicitly specify it.

Now let's see how you can move an existing table that has a Cluster Index to a different filegroup. First, let's drop the Primary Key constraint with an Move to Option ( We are assuming that there is a cluster index on the PK).

ALTER TABLE [INVOICE]
DROP CONSTRAINT [INVOICE_PK] WITH (MOVE TO SECONDERYDATA)

After the move, we now recreate the PK Constraint:

ALTER TABLE [INVOICE]
ADD CONSTRAINT [INVOICE_PK] PRIMARY KEY CLUSTERED 
( [column name] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [SECONDERYDATA]

Remember when you recreate the PK constraint on the Seconderydata filegroup, all the data in that table will automatically be moved to the Seconderydata filegroup. This will only happen in the case of a table that has a primary key constraint and has a clustered index.

The transfer time may depend on the size of the table, so please do not do this during business hours.SQL Server will generally lock the entire table.

Now all you data for that table will be moved to the new file group. Please remember to shrink the database to make the space available to the Operating System.

Total article views: 28657 | Views in the last 30 days: 166
 
Related Articles
FORUM

Restoring database - Filegroups

Restoring database - Filegroups

FORUM

Filegroup vs performance

filegroup

FORUM

Backup FileGroup and Restore on Secondary Database

Backup FileGroup and Restore on Secondary Database with same filegroups

FORUM

Filegroups

transferring a table to a new filegroup with foreign key constraints

FORUM

creating filegroup

creating filegroup

Tags
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones