所有索引和主键都是在主文件(.mdf)中创建的。有没有办法将所有索引和主键移动到辅助文件组(.ndf)?
问问题
3115 次
2 回答
1
使用以下查询:
DECLARE @SchemaName NVARCHAR(300),
@ObjectName NVARCHAR(300),
@IndexName NVARCHAR(300),
@Columns NVARCHAR(MAX),
@IncludeColumns NVARCHAR(MAX),
@Command NVARCHAR(MAX)
DECLARE IndexCursor CURSOR FOR
SELECT SCHEMA_NAME(o.schema_id),OBJECT_NAME(o.object_id), i.name,
STUFF((SELECT ',['+c.name+']'
FROM sys.index_columns ic
INNER JOIN sys.columns c ON c.column_id = ic.column_id AND c.object_id = ic.object_id
WHERE ic.object_id = i.object_id
AND ic.index_id = i.index_id
AND ic.is_included_column=0
ORDER BY ic.key_ordinal
FOR XML PATH('')
),1,1,'') IndexColumn,
STUFF((SELECT ',['+c.name+']'
FROM sys.index_columns ic
INNER JOIN sys.columns c ON c.column_id = ic.column_id AND c.object_id = ic.object_id
WHERE ic.object_id = i.object_id
AND ic.index_id = i.index_id
AND ic.is_included_column=1
ORDER BY ic.key_ordinal
FOR XML PATH('')
),1,1,'') IncludeColumn,
i.filter_definition
FROM sys.indexes i
INNER JOIN sys.objects o ON o.object_id = i.object_id
INNER JOIN sys.data_spaces ds ON ds.data_space_id = i.data_space_id
WHERE ds.name = 'PRIMARY'
AND OBJECTPROPERTY(i.object_id,'IsUserTable')=1
OPEN IndexCursor
FETCH NEXT FROM IndexCursor INTO @SchemaName, @ObjectName, @IndexName, @Columns, @IncludeColumns
WHILE @@FETCH_STATUS=0 BEGIN
SET @Command = 'ALTER INDEX ' + @IndexName + ' ON [' + @SchemaName + '].['+@ObjectName+']('+@Columns+') '+ ISNULL('Include ('+@IncludeColumns+')','') + ' ON [YourFileGroupName]'
EXEC(@Command)
FETCH NEXT FROM IndexCursor INTO @SchemaName, @ObjectName, @IndexName, @Columns, @IncludeColumns
END
CLOSE IndexCursor
DEALLOCATE IndexCursor
于 2014-07-07T06:17:59.337 回答
0
您不能使用 Management Studio 移动使用 UNIQUE 或 PRIMARY KEY 约束创建的索引。要移动这些索引,请在 Transact-SQL 中使用带有 (DROP_EXISTING=ON) 选项的 CREATE INDEX 语句。
如果表具有聚集索引,则将聚集索引移动到新文件组会将表移动到该文件组。
对于分步说明,您可以使用MS 文档。
于 2014-07-07T06:00:58.377 回答