问题
My customers have very small databases of sizes 50 t0 100 MBs. We are using SQL Server 2014.
The backup operations are scheduled properly.
Can I recommend them to have the maintenance jobs for Index Maintenance, Update Statistics and DBCC CheckDB? How deep it will affect if these jobs are not taken care?
回答1:
too vague to answer precisely, it depends a lot on the kind of activity done on the DB, the recovery model used. In the simple recovery mode, which use just db file without transaction log if there are frequent delete and insertions on tables you usually need to rebuild indexes on regular bases.
I use to set a daily (night run) task that invoke sqlcmd to execute the following SQL:
--*********************************
--*** REPLACE THIS WITH DBNAME ***
USE db_mytestcopy_of_live_production
--*********************************
GO
DECLARE @Queryresult NVARCHAR(MAX)
SET @Queryresult=''
--*********************************
--DEFRAGGING THE MOST 6 FRAGMENTED INDEXES EXCLUDING ONES WITH A FRAG UP TO 35 %
--TO TAILOR IT TO YOUR NEEDS CHANGE THE NUMBERS AFTER "SELECT TOP" AND AFTER "ss.avg_fragmentation_in_percent >"
--WITHIN THE SQL HERE BELOW
--*********************************
SELECT TOP 6
@Queryresult=@Queryresult + 'ALTER INDEX ' + QUOTENAME(i.name) + ' ON '
+ QUOTENAME('dbo') + '.'
+ QUOTENAME(OBJECT_NAME(i.OBJECT_ID)) + ' REBUILD;'
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 'SAMPLED') ss
INNER JOIN sys.indexes i ON i.OBJECT_ID = ss.OBJECT_ID AND i.index_id = ss.index_id
INNER JOIN sys.objects o ON ss.object_id = o.object_id
WHERE ss.avg_fragmentation_in_percent > 35
AND ss.record_count > 0
AND o.is_ms_shipped = 0 --Excludes any objects created as a part of SQL Server installation
AND ss.index_id > 0 --Excludes heap indexes
ORDER BY ss.avg_fragmentation_in_percent DESC
--*********************************
--Uncomment to see the generated command
--*********************************
--PRINT @QueryResult
EXEC sp_executesql @QueryResult
来源:https://stackoverflow.com/questions/38914538/maintenance-jobs-in-sql-small-databases