Solved

move all the non-clustered indexes to another filegroup

Posted on 2004-09-29
7
788 Views
Last Modified: 2012-06-21
What is the easiest way to move the entire database's non-clustered indexes into another filegroup?
Thanks!
0
Comment
Question by:loveviolet
  • 5
7 Comments
 
LVL 15

Expert Comment

by:jdlambert1
ID: 12184841
Script the current indexes, edit the script to point to the new file group, drop the indexes, then run the edited script.
0
 
LVL 15

Expert Comment

by:jdlambert1
ID: 12184931
To script the indexes, open Enterprise Manager and drill down to the tables, and select all the tables you want to script the indexes of (Ctrl-A for all of them). Right-click on any selected table name and choose All Tasks/Generate SQL Script... to start the wizard. Click on the Formatting tab and clear the CREATE and DROP checkboxes (this is to script the table, not the indexes. Next, click the Options tab and set the "Script indexes" checkbox. Click OK and tell it where to save the script.

Open the script in Query Analyzer and hit Ctrl-H to open the replace function. Replace "ON [PRIMARY]" with "ON [INDEX_GROUP]" or whatever the name of your new file group is.
0
 
LVL 15

Expert Comment

by:jdlambert1
ID: 12185058
There are several ways to deal with dropping the indexes. If you have zillions of tables, I'd go to SQLServerCentral.com and grab one of their Index-dropping stored procedures. If you have a manageable number, I'd make a copy of the create-index script, and modify it to make each command a drop (by copying and altering the same script, you ensure you're only dropping what you will be receating):

DROP INDEX TableName.IndexName
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 1

Author Comment

by:loveviolet
ID: 12185127
Thanks for your answer.  Actually I need to move all the unique constraints as well, but I want to eliminate the clustered ones...  The SQL server's built in script will not differentiate clustered/nonclustered constraints.
0
 
LVL 15

Accepted Solution

by:
jdlambert1 earned 250 total points
ID: 12185487
You can script the unique constraints by checking the box for "Script PRIMARY keys, FOREIGN keys, defaults, and check constaints", but this may require you to cut the parts of the generated script that you don't want. The same with clustered. At least it's easier to cut what you don't need than write what you do need from scratch.

For a little more sophisticated scripting, there is a "Smart Index Manipulation system" stored procedure code available at SQLServerCentral.com. It requires registration, but it's free. Here's a description:

Script Rating 4/5       Total number of votes [13]  By: psnipes
We are constantly having to drop indexes from tables while repopulating the data in the table. We then have to rebuild the indexes as they were before dropping them. This requires writing a custom drop and recreate index
script for each unique situation. If we try to write scripts ahead of time as soon as the indexing scheme changes on the table the scripts must be changed as well. This sp is part of a collection of five stored procedures I call the Smart Index Manipulation System. It allows you to drop indexes dynamically and then recreate them later just as they were before the drop. You can also keep a running history of your indexes as they change over time.
0
 
LVL 15

Expert Comment

by:jdlambert1
ID: 12185702
Oh, another way to generate one script at a time is with Query Analyzer. F8 opens the Object Browser, and if you drill down to one level below tables, you can find some of the indexes and constraints (separate nodes) that you can right-click-drag & drop into the code window and it will let you choose Create or Drop scripts, which it will write for you. You can left-click-drag if you only want the index or constraint name.
0
 
LVL 34

Expert Comment

by:arbert
ID: 12186013
Also, think just using the CREATE INDEX script with the DROP_EXISTING clause.  It will be a little bit more efficient than dropping the current and recreating it.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now