Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I want to move all indexes to a separate hard drive. To that effect, I created a new filegroup, then added a new file to it (e.g. alter database foo add file(...) to FileGroup).

Now I want to move all the indexes to this new filegroup. Turns out, I can't use ALTER INDEX to move an index between filegroups. And I can't move PK or Unique indexes either, lest I want to move the table itself (which I don't), so those are out. So, at least, I'd like to move the rest of the indexes.

I am struggling to find a method to move all eligible indexes from one filegroup to another. Is there a reasonable automated way to do this?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Found the answer. See comments on how to make it work for SQL Server 2005.

share|improve this answer

Create a new filegroup and file, rebuild the index on your filegroup.

In this case my index is actually on the filegroup PRIMARY, and i moved it to the new filegroup [TEST]. Drop and recreate the index, for recreating it is important that you give sql server the information where to store the new index. In my case [TEST].

USE [ShrinkIsBed]
GO

IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[FirstTable]') AND name = N'NX_TEST')
DROP INDEX [NX_TEST] ON [dbo].[FirstTable] WITH ( ONLINE = OFF )
GO

USE [ShrinkIsBed]
GO

CREATE NONCLUSTERED INDEX [NX_TEST] ON [dbo].[FirstTable] 
(
    [FirstName] ASC,
    [LastName] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) **ON [TEST]**
GO
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.