使用Backup创建测试环境之后,发现testdb的Log File过大,达到400GB,由于测试环境实际上不需要这么大的Log Space,占用400GB的Disk Space实在浪费Disk Resource,于是使用DBCC Shrink收缩Log File:
dbcc shrinkfile(testdb_log_5,10240,notruncate) dbcc shrinkfile(testdb_log_5,10240,truncateonly)
命名执行完成之后,发现还有300多GB,实际Log File占用的空间的百分比十分低,0.000428%
DBCC SQLPERF(LOGSPACE)
由于test db的还原模式是Simple,并且没有active user,最大的可能性是db的Trasaction log被标记为Replication,使用以下函数统计,发现有大量的log未被LogReader读取。
select count(0) from sys.fn_dblog(null,null) f where f.Description =‘REPLICATE‘
在Publisher database中,使用 sp_repltrans 查看没有被LogReader标记为Distributed的Transaction。
sp_repltrans returns a result set of all the transactions in the publication database transaction log that are marked for replication but have not been marked as distributed.
exec sys.sp_repltrans
Unable to execute procedure. The database is not published. Execute the procedure in a database that is published for replication.
由于testdb是使用backup还原的测试数据库,没有在master中注册为Publisher database,必须设置 database 为publish,表示 Database can be used for other types of publications.
exec sys.sp_replicationdboption @dbname = N‘testdb‘, @optname = N‘publish‘, @value = N‘true‘
注册成功之后,使用 sp_repldone,将所有的Transaction Log 标记为Distributed。
sp_repldone updates the record that identifies the last distributed transaction of the server.
EXEC sys.sp_repldone @xactid = NULL, @xact_segno = NULL, @numtrans = 0, @time = 0, @reset = 1
When xactid is NULL, xact_seqno is NULL, and reset is 1, all replicated transactions in the log are marked as distributed. This is useful when there are replicated transactions in the transaction log that are no longer valid and you want to truncate the log,
最后,使用DBCC ShrinkFile命令,Transaction Log File收缩完成。
参考doc:
sp_replicationdboption (Transact-SQL)