【发布时间】:2016-12-29 09:17:38
【问题描述】:
如何使用 MSSQL 2005 在不同驱动器上创建文件组,然后将表移入其中?
例如:我的数据库位于 H:\Product.mdf 和 H:\Product.ldf
我想在 F:\FileGroup\ 上创建一个新文件组,然后将带有聚集索引的表移动到这个新文件组。
【问题讨论】:
标签: sql sql-server
如何使用 MSSQL 2005 在不同驱动器上创建文件组,然后将表移入其中?
例如:我的数据库位于 H:\Product.mdf 和 H:\Product.ldf
我想在 F:\FileGroup\ 上创建一个新文件组,然后将带有聚集索引的表移动到这个新文件组。
【问题讨论】:
标签: sql sql-server
这不是一项简单的任务,根据您的表的大小,可能需要大量的停机时间。
首先,你必须定义新的文件组:
ALTER DATABASE MyDatabase
add filegroup NewGroup
然后,为该文件组创建一个适当的文件,例如:
ALTER DATABASE MyDatabase
add file
(
name = NewFile
,filename = 'C:\temp\NewFile.ndf'
,size = 100MB
,maxsize = unlimited
,filegrowth = 100MB
)
to filegroup NewGroup
要将表移动到文件组,您必须在文件组上为该表创建聚集索引。如果您有一个集群约束(例如唯一键或主键),则必须先将其删除。这是移动此类表格的一种方法:
-- Set up sample table
CREATE TABLE MyTable
(
Data varchar(100) not null
constraint PK_MyTable
primary key clustered
)
-- Can't "move" primary key constraint to a new file group
ALTER TABLE MyTable
drop constraint PK_MyTable
-- This will move the data in the table to the new file group
CREATE clustered index Move_MyTable
on MyTable (Data)
on NewGroup
-- Still in the new file group, just no index
DROP INDEX MyTable.Move_MyTable
-- Recreate the primary key, keeping it on the new file group
ALTER TABLE MyTable
add constraint PK_MyTable
primary key clustered (Data)
on NewGroup
这有点麻烦,所以请务必先在数据库副本上测试所有内容!
【讨论】: