Moving all non-clustered indexes to another filegroup in SQL Server

被刻印的时光 ゝ 提交于 2019-12-03 04:23:23

问题


In SQL Server 2008, I want to move ALL non-clustered indexes in a DB to a secondary filegroup. What's the easiest way to do this?


回答1:


Run this updated script to create a stored procedure called MoveIndexToFileGroup. This procedure moves all the non-clustered indexes on a table to a specified file group. It even supports the INCLUDE columns that some other scripts do not. In addition, it will not rebuild or move an index that is already on the desired file group. Once you've created the procedure, call it like this:

EXEC MoveIndexToFileGroup @DBName = '<your database name>',
                          @SchemaName = '<schema name that defaults to dbo>',
                          @ObjectNameList = '<a table or list of tables>',
                          @IndexName = '<an index or NULL for all of them>',
                          @FileGroupName = '<the target file group>';

To create a script that will run this for each table in your database, switch your query output to text, and run this:

SELECT 'EXEC MoveIndexToFileGroup '''
    +TABLE_CATALOG+''','''
    +TABLE_SCHEMA+''','''
    +TABLE_NAME+''',NULL,''the target file group'';'
    +char(13)+char(10)
    +'GO'+char(13)+char(10)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
ORDER BY TABLE_SCHEMA, TABLE_NAME;

Please refer to the original blog for more details. I did not write this procedure, but updated it according to the blog's responses and confirmed it works on both SQL Server 2005 and 2008.

Updates

  1. @psteffek modified the script to work on SQL Server 2012. I merged his changes.
  2. The procedure fails when your table has the IGNORE_DUP_KEY option on. No fix for this yet.
  3. @srutzky pointed out the procedure does not guarantee to preserve the order of an index and made suggestions on how to fix it. I updated the procedure accordingly.
  4. ojiNY noted the procedure left out index filters (for compatibility with SQL 2005). Per his suggestion, I added them back in.



回答2:


Script them, change the ON clause, drop them, re-run the new script. There is no alternative really.

Luckily, there are scripts on the Interwebs such as this one that will deal with scripting for you.




回答3:


Update: This thing will take long time to do step 2 manually if you are using MS SQL Server manager 2008R2 or earlier. I used sql server manager 2014, so it works well (because the way it export the drop and create index is easy to modify) I tried to run script in SQL server 2014 and got some issue, I'm too lazy to detect the problems, SO I come up with another solution that not depend on the version of SQL server you are running.

  1. Export your index (with drop and create)

2.Update your script, remove all things related to drop create tables, keep the thing belong to indexs. and Replace your original index with the new index (in my case, I replace ON [PRIMARY] by ON [SECONDARY] [

]5
  1. Run script! And wait until it done.

(You may want to save the script to run in some others environment).



来源:https://stackoverflow.com/questions/4237238/moving-all-non-clustered-indexes-to-another-filegroup-in-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!