Вопрос

I have multiple indexed views that I want to move from the primary filegroup into another filegroup, with minimal impact on performance. How can I accomplish this?

Это было полезно?

Решение

This should generate the script to do what you want, including both the clustered and any non-clustered indexes. I've added the ONLINE = ON option to minimize impact to existing workload but, in theory anyway, your indexed views shouldn't be huge and so shouldn't have any drastic impact. Of course this option is only available in Enterprise Edition, and the script handles that. If you can't do it online you'll certainly want to test the impact with a simulated workload (and you'll want to test this operation in a test environment first in any case). And you are best off scheduling this work for a relative downtime or, ideally, a maintenance window.

DECLARE @opt NVARCHAR(13),  @sql NVARCHAR(MAX), @fg  NVARCHAR(128);

SELECT @fg  = N'OtherFileGroupName', -- please fix this
  @sql = N'', 
  @opt = CASE WHEN CONVERT(NVARCHAR(32), SERVERPROPERTY('Edition')) 
    LIKE N'Enterprise%' THEN N', ONLINE = ON' ELSE N'' END;

SELECT @sql = @sql + N'
  CREATE ' + CASE WHEN i.index_id = 1 
    THEN 'UNIQUE CLUSTERED' 
    ELSE '' END
  + ' INDEX ' + QUOTENAME(i.name) 
  + ' ON ' + QUOTENAME(SCHEMA_NAME(v.[schema_id]))
  + '.' + QUOTENAME(v.name) 
  + '(' + STUFF((SELECT ',' + QUOTENAME(c.name)
    FROM sys.columns AS c
    INNER JOIN sys.index_columns AS ic 
    ON c.[object_id] = ic.[object_id]
    AND c.column_id = ic.column_id
    WHERE c.[object_id] = v.[object_id]
    AND ic.index_id = i.index_id
    FOR XML PATH, TYPE).value('.[1]', 'nvarchar(max)'),1,1,'')
  + ') 
  WITH (DROP_EXISTING = ON' + @opt + ')
  ON ' + QUOTENAME(@fg) + ';'
FROM sys.views AS v
INNER JOIN sys.indexes AS i
ON v.[object_id] = i.[object_id]
ORDER BY i.index_id;

SELECT @sql;
-- EXEC sp_executesql @sql;

This should yield a script like:

CREATE UNIQUE CLUSTERED INDEX [x] 
  ON [dbo].[splunge_view]([id])
  WITH (DROP_EXISTING = ON, ONLINE = ON)
  ON [OtherFileGroupName];

CREATE  INDEX [y] 
  ON [dbo].[splunge_view]([c],[id]) 
  WITH (DROP_EXISTING = ON, ONLINE = ON)
  ON [OtherFileGroupName];

...

When you're happy with the SELECT output (works best in results to text mode, but still is limited to 8K there and only if you change the setting in tools / options), uncomment the EXEC. But again, in a test environment first!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top