?
Solved

move all the non-clustered indexes to another filegroup

Posted on 2004-09-29
7
Medium Priority
?
795 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 
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 750 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

762 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