Shrinking log files and data files

Shrinking Database Overview:

Shrinking the database is an operation that consumes server performance. If it is not necessary, do not shrink the
go 
  shrink. It is nothing more than recovering the file space that has been allocated to the database. However, when shrinking, the data pages must be moved, which may cause a lot of fragmentation and affect performance.
Go 
  log shrinking is different from data file shrinking. The virtual file state in the log can only shrink when it is reusable.
  And all log virtual files with active logs are active and cannot be shrunk.
go

 

Shrinking log overview:

The log file shrinks, reclaiming disk space.
  According to the design size of the data file, it is generally not necessary to shrink, and shrinking may cause performance problems.

Log
  If the selected log recovery model is complete, if there is no log truncation, the log grows greatly, it is
recommended to
  back up the log.
    backup log db to disk='backup device'
  truncates the log
    backup log db with no_log
  and then shrinks.
    dbcc shrinkfile(2,10)


--===================================DB_Tank
USE [master]
GO

ALTER DATABASE [DB_Tank] SET RECOVERY SIMPLE WITH NO_WAIT
GO

EXEC('
USE [Db_Tank]
DBCC SHRINKFILE (''Db_Tank_Log'' , 2048) --sql server default file unit is M, so here is 2G
')

print '['+CONVERT(varchar(20),GETDATE(),120)+']Restore the game library mode'
GO

USE [master]
GO

ALTER DATABASE [Db_Tank] SET RECOVERY BULK_LOGGED WITH NO_WAIT
GO


--====================================DB_logs
USE [master]
GO

ALTER DATABASE [DB_logs] SET RECOVERY SIMPLE WITH NO_WAIT
GO

EXEC('
USE [Db_logs]
DBCC SHRINKFILE (''Db_Logs_Log'' , 2048)
')

print '['+CONVERT(varchar(20),GETDATE(),120)+']Restore the game library mode'
GO

USE [master]
GO

ALTER DATABASE [Db_Logs] SET RECOVERY BULK_LOGGED WITH NO_WAIT
GO


--========================================System log
/*
C:\Program Files The ERRORLOG.1 error log in \Microsoft SQL Server\MSSQL.1\MSSQL\LOG
is very large, rotate it

sp_cycle_errorlog - manually start the sp that rotates the syslog

*/
/*
C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data
tempdb disk shrink

*/

--======================================tempdb
USE [tempdb]
GO
DBCC SHRINKFILE (N'templog' , 1024)
GO

USE [master]
GO

print '['+CONVERT(varchar(20),GETDATE(),120)+']contract log successfully'
GO

print '['+CONVERT(varchar(20),GETDATE(),120)+'] is shrinking the database'
GO
DBCC SHRINKDATABASE(tempdb)
GO
print '['+CONVERT(varchar(20),GETDATE(),120) +'] shrinking the database successfully'

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325295857&siteId=291194637