我在 SQL Server 中有一个生产数据库,希望在功能完成后进行最后的修改。在发货之前,我想确保我在 SQL Server 数据库中进行了一些清理,并截断和收缩日志文件?
我可以每晚运行一次作业来截断日志和缩小文件吗?
这就是我到目前为止所拥有的:
ALTER proc [dbo].[UTIL_ShrinkDB_TruncateLog]
as
-- exec sp_helpfile
BACKUP LOG PMIS WITH TRUNCATE_ONLY
DBCC SHRINKFILE (PMIS, 1)
DBCC SHRINKFILE (PMIS, 1)
编辑: 我的恢复模型很简单
由于您正在对日志文件执行备份,因此不必截断它。请注意,这不会导致日志文件缩小,而只会导致其覆盖自身。所以你需要经常执行它们来维护一个小的日志文件。
这里有一篇关于日志维护的好文章。
无论如何,有人(像我一样)偶然发现了这个条目,并正在寻找恢复模型已满的截断日志,这是我经过几个小时的调查后发现的。通过以下步骤改善日志大小和灾难恢复。
SELECT name, recovery_model_desc FROM sys.databases WHERE name = 'AdventureWorks'
-- AdventureWorks SIMPLE
ALTER DATABASE AdventureWorks SET RECOVERY FULL
DECLARE @FileName varchar(1000)
SELECT @FileName = (SELECT 'D:\SQL Server\Backup\' + CAST(FORMAT(GETDATE(), 'yyyy-MM-dd HH-mm-ss') AS VARCHAR) + ' AdventureWorks.bak')
BACKUP DATABASE AdventureWorks TO DISK=@FileName
奖励:检查备份历史记录
-- inspect backup history (type D=Data, L=Log)
SELECT TOP 10 bs.database_name, bs.backup_start_date, CAST((bs.backup_finish_date-bs.backup_start_date) as time(0)) duration, bs.server_name, bs.user_name, bs.type,
bm.physical_device_name, CAST(bs.compressed_backup_size / 1024 / 1024 / 1024 AS decimal(10,3)) compressed_backup_size_GB
FROM msdb.dbo.backupset AS bs
INNER JOIN msdb.dbo.backupmediafamily AS bm on bs.media_set_id = bm.media_set_id
ORDER BY bs.backup_start_date DESC
跟踪日志大小和使用情况几周。 创建表 TMP_dbLogSize:
-- create table to track log usage and size
SELECT GETDATE() AS date, total_log_size_in_bytes / 1024 / 1024 total_log_MB, used_log_space_in_bytes / 1024 / 1024 used_log_MB, log_space_in_bytes_since_last_backup / 1024 / 1024 space_since_last_bak_MB, used_log_space_in_percent INTO TMP_dbLogSize FROM sys.dm_db_log_space_usage
通过计划的作业收集表中的信息
-- track log usage and size
INSERT INTO TMP_dbLogSize SELECT GETDATE() AS date, total_log_size_in_bytes / 1024 / 1024 total_log, used_log_space_in_bytes / 1024 / 1024 used_log, log_space_in_bytes_since_last_backup / 1024 / 1024 space_since_last_bak, used_log_space_in_percent FROM sys.dm_db_log_space_usage
-- clean table for tracking log usage after some time
delete from TMP_dbLogSize where DATEDIFF(DAY, date, GETDATE()) > 60
此步骤将释放日志文件中的空间。
-- backup log (overwrite last log backup)
BACKUP LOG AdventureWorks TO DISK = N'D:\SQL Server\Backup\AdventureWorks_log.trn' WITH FORMAT, INIT, SKIP
检查 TMP_dbLogSize 中典型的最大日志使用量,然后将其缩小到该值。
SELECT name, size/128 FileSizeInMB, FILE_ID FROM sys.database_files
--AdventureWorks_data 32921 1
--AdventureWorks_log 285 2
use AdventureWorks
-- dbcc shrinkfile(FILE_ID from sys.database_files, target size in MB)
dbcc shrinkfile(AdventureWorks_log, 150)
定期重建索引,使性能保持良好,可以在备份后执行。
-- DB maintenance, rebuild all indexes
DECLARE @TableName VARCHAR(255)
DECLARE @sql NVARCHAR(500)
DECLARE @fillfactor INT
SET @fillfactor = 80
DECLARE TableCursor CURSOR FOR
SELECT QUOTENAME(OBJECT_SCHEMA_NAME([object_id]))+'.' + QUOTENAME(name) AS TableName FROM sys.tables
OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = 'ALTER INDEX ALL ON ' + @TableName + ' REBUILD WITH (FILLFACTOR = ' + CONVERT(VARCHAR(3),@fillfactor) + ')'
EXEC (@sql)
FETCH NEXT FROM TableCursor INTO @TableName
END
CLOSE TableCursor
DEALLOCATE TableCursor
GO