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?
在SQL Server 2008中,我想将DB中的所有非聚集索引移动到辅助文件组。最简单的方法是什么?
3 个解决方案
#1
20
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:
运行此更新的脚本以创建名为MoveIndexToFileGroup的存储过程。此过程将表上的所有非聚集索引移动到指定的文件组。它甚至支持其他一些脚本没有的INCLUDE列。此外,它不会重建或移动已在所需文件组上的索引。创建过程后,请按以下方式调用:
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.
有关更多详细信息,请参阅原始博客。我没有写这个程序,但根据博客的回复更新了它,并确认它适用于SQL Server 2005和2008。
Updates
- @psteffek modified the script to work on SQL Server 2012. I merged his changes.
- @psteffek修改了脚本以在SQL Server 2012上工作。我合并了他的更改。
- The procedure fails when your table has the
IGNORE_DUP_KEY
option on. No fix for this yet. - 当您的表启用了IGNORE_DUP_KEY选项时,该过程将失败。还没有解决这个问题。
- @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.
- @srutzky指出,该程序并不保证保留索引的顺序,并就如何修复它提出了建议。我相应地更新了程序。
- ojiNY noted the procedure left out index filters (for compatibility with SQL 2005). Per his suggestion, I added them back in.
- ojiNY注意到该过程遗漏了索引过滤器(为了与SQL 2005兼容)。根据他的建议,我把它们加入了。
#2
5
Script them, change the ON clause, drop them, re-run the new script. There is no alternative really.
编写脚本,更改ON子句,删除它们,重新运行新脚本。真的没有别的选择。
Luckily, there are scripts on the Interwebs such as this one that will deal with scripting for you.
幸运的是,Interwebs上有一些脚本,例如这个脚本将为你处理脚本。
#3
1
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.
更新:如果您使用的是MS SQL Server Manager 2008R2或更早版本,则需要很长时间才能手动执行第2步。我使用sql server manager 2014,所以它运行良好(因为它导出drop和创建索引的方式很容易修改)我试图在SQL Server 2014中运行脚本并遇到一些问题,我懒得发现问题,所以我想出了另一种不依赖于你正在运行的SQL服务器版本的解决方案。
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
2.更新您的脚本,删除与drop create tables相关的所有内容,保留该东西属于索引。并用新索引替换原始索引(在我的情况下,我将ON [PRIMARY]替换为ON [SECONDARY] [] 5
- Run script! And wait until it done.
- 运行脚本!等到它完成。
(You may want to save the script to run in some others environment)
.
(您可能希望将脚本保存为在其他环境中运行)。
#1
20
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:
运行此更新的脚本以创建名为MoveIndexToFileGroup的存储过程。此过程将表上的所有非聚集索引移动到指定的文件组。它甚至支持其他一些脚本没有的INCLUDE列。此外,它不会重建或移动已在所需文件组上的索引。创建过程后,请按以下方式调用:
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.
有关更多详细信息,请参阅原始博客。我没有写这个程序,但根据博客的回复更新了它,并确认它适用于SQL Server 2005和2008。
Updates
- @psteffek modified the script to work on SQL Server 2012. I merged his changes.
- @psteffek修改了脚本以在SQL Server 2012上工作。我合并了他的更改。
- The procedure fails when your table has the
IGNORE_DUP_KEY
option on. No fix for this yet. - 当您的表启用了IGNORE_DUP_KEY选项时,该过程将失败。还没有解决这个问题。
- @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.
- @srutzky指出,该程序并不保证保留索引的顺序,并就如何修复它提出了建议。我相应地更新了程序。
- ojiNY noted the procedure left out index filters (for compatibility with SQL 2005). Per his suggestion, I added them back in.
- ojiNY注意到该过程遗漏了索引过滤器(为了与SQL 2005兼容)。根据他的建议,我把它们加入了。
#2
5
Script them, change the ON clause, drop them, re-run the new script. There is no alternative really.
编写脚本,更改ON子句,删除它们,重新运行新脚本。真的没有别的选择。
Luckily, there are scripts on the Interwebs such as this one that will deal with scripting for you.
幸运的是,Interwebs上有一些脚本,例如这个脚本将为你处理脚本。
#3
1
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.
更新:如果您使用的是MS SQL Server Manager 2008R2或更早版本,则需要很长时间才能手动执行第2步。我使用sql server manager 2014,所以它运行良好(因为它导出drop和创建索引的方式很容易修改)我试图在SQL Server 2014中运行脚本并遇到一些问题,我懒得发现问题,所以我想出了另一种不依赖于你正在运行的SQL服务器版本的解决方案。
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
2.更新您的脚本,删除与drop create tables相关的所有内容,保留该东西属于索引。并用新索引替换原始索引(在我的情况下,我将ON [PRIMARY]替换为ON [SECONDARY] [] 5
- Run script! And wait until it done.
- 运行脚本!等到它完成。
(You may want to save the script to run in some others environment)
.
(您可能希望将脚本保存为在其他环境中运行)。