1
我的團隊正在研究.NET Web應用程序,其數據存儲通過SQL Server 2005 Express進行管理。是否可以自動執行數據庫中的任務,以便在部署/安裝後按間隔自動重建特定索引?如果是這樣,這是如何實現的?我認爲這將在數據庫的tasks
下,但我沒有看到它。在SQL Server Express 2005中自動重建索引
我的團隊正在研究.NET Web應用程序,其數據存儲通過SQL Server 2005 Express進行管理。是否可以自動執行數據庫中的任務,以便在部署/安裝後按間隔自動重建特定索引?如果是這樣,這是如何實現的?我認爲這將在數據庫的tasks
下,但我沒有看到它。在SQL Server Express 2005中自動重建索引
當我服務我的數據庫時,此腳本幫助我刪除索引的碎片。也許,它對你有用:
SET NOCOUNT ON;
DECLARE
@SQL NVARCHAR(MAX)
, @IndexName SYSNAME
, @Output VARCHAR(200)
, @ServerVersion VARCHAR(100)
SELECT @ServerVersion = CAST(SERVERPROPERTY('Edition') AS VARCHAR(100))
DECLARE cur CURSOR LOCAL READ_ONLY FORWARD_ONLY FOR
SELECT
'ALTER INDEX [' + ix.name + N'] ON [' + SCHEMA_NAME(t.[schema_id]) + '].[' + t.name + '] ' +
CASE
WHEN ps.avg_fragmentation_in_percent > 50 THEN
CASE WHEN @ServerVersion LIKE 'Enterprise%' OR @ServerVersion LIKE 'Developer%' THEN
'REBUILD WITH (SORT_IN_TEMPDB = ON, ONLINE = ON ' + CASE WHEN ix.fill_factor > 0 THEN ', FILLFACTOR = ' + CAST(ix.fill_factor AS VARCHAR(3)) ELSE '' END + ') '
ELSE
'REBUILD WITH (SORT_IN_TEMPDB = ON' + CASE WHEN ix.fill_factor > 0 THEN ', FILLFACTOR = ' + CAST(ix.fill_factor AS VARCHAR(3)) ELSE '' END + ') '
END
ELSE 'REORGANIZE '
END +
CASE
WHEN ps.partition_number > 1 THEN N' PARTITION = ' + CAST(ps.partition_number AS NVARCHAR(MAX))
ELSE N''
END + ';', ix.name
FROM sys.indexes ix
JOIN sys.objects t ON t.[object_id] = ix.[object_id]
JOIN (
SELECT
[object_id]
, index_id
, avg_fragmentation_in_percent
, partition_number
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, N'LIMITED')
WHERE page_count > 100
AND avg_fragmentation_in_percent > 10
) ps ON t.[object_id] = ps.[object_id] AND ix.index_id = ps.index_id
OPEN cur
FETCH NEXT FROM cur INTO @SQL, @IndexName
WHILE @@FETCH_STATUS = 0 BEGIN
SELECT @Output = CONVERT(NVARCHAR(15), GETDATE(), 114) + ': ' + @IndexName
RAISERROR(@Output, 0, 1) WITH NOWAIT
EXEC (@SQL)
FETCH NEXT FROM cur INTO @SQL, @IndexName
END
CLOSE cur
DEALLOCATE cur
一般在SQL Server中,你可能會使用一個維護計劃作爲SQL Server代理作業調度它。 Express並不附帶SQL代理,因此您需要使用Windows計劃程序。 –
啊,謝謝你的信息。 Windows調度程序是否可以在啓動/安裝時進行打包和自動配置? – Christian