Solved

move all the non-clustered indexes to another filegroup

Posted on 2004-09-29
7
794 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

718 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