SQL Server:更改索引的文件组(这也是一个PK)
我们正在对一组数据库进行清理,第一步是将数据库中的所有索引放入正确的文件组中。
目前,这些索引混合在 DATA 文件组和 INDEXES 文件组之间;它们都需要移动到 INDEXES 文件组。
我想这可以在脚本中轻松完成,但是如何最好地处理主键上的索引?
以下命令
DROP INDEX table.indexname
会产生错误:
不允许显式 DROP INDEX 在索引“Answer.PK_Answer”上。这是 用于 PRIMARY KEY 约束 执法。
那么最好的方法是什么?我是否需要删除主键,然后删除索引,然后重新创建主键,最后在正确的文件组上重新创建索引?这种方法有什么缺点吗?
We're doing a cleanup on a group of databases, and the first step is to get all indexes in the database into the correct filegroups.
Currently, those indexes are mixed between the DATA filegroup and the INDEXES filegroup; they all need to move to the INDEXES filegroup.
This can be done easily enough in script I guess, however how do you best handle an index on a Primary Key?
The following command
DROP INDEX table.indexname
produces the error:
An explicit DROP INDEX is not allowed
on index 'Answer.PK_Answer'. It is
being used for PRIMARY KEY constraint
enforcement.
So what is the best way? Do I need to drop the Primary Key, then drop the Index, then re-create the primary key and finally re-create the index on the correct filegroup? Are there any drawbacks to this method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试以下语句,该语句删除并重新创建索引文件组上的索引
CREATE CLUSTERED INDEX PK_Answer
ON 表名(答案)
与(DROP_EXISTING = ON);
You can try the below statement which drops and recreates the index on the index filegroup
CREATE CLUSTERED INDEX PK_Answer
ON tablename(Answer)
WITH (DROP_EXISTING = ON);
由于他有主键:
CREATE UNIQUE CLUSTERED INDEX PK_Answer ON tablename(Answer) WITH (DROP_EXISTING = ON);
Since he has a primary key:
CREATE UNIQUE CLUSTERED INDEX PK_Answer ON tablename(Answer) WITH (DROP_EXISTING = ON);
如果其他人需要此信息(我需要),如果您希望将重新创建的主键移动到另一个位置,请在末尾添加文件组。之前的答案都没有规定这部分:
In case anyone else needed this information (I did), add the FILEGROUP at the end if you wish to MOVE the recreated PRIMARY KEY to another location. Neither of the previous answers stipulated this portion: