简体   繁体   中英

What is the reason that the LOG file size of the SQL Server database does not decrease?

I used the following query to view the database log file.

declare @templTable as table 
(   DatabaseName  nvarchar(50),
    LogSizeMB nvarchar(50), 
    LogSpaceUsedPersent nvarchar(50),
    Statusee bit
)
INSERT INTO @templTable
EXEC('DBCC SQLPERF(LOGSPACE)')
SELECT * FROM @templTable ORDER BY convert(float , LogSizeMB) desc

DatabaseName    LogSizeMB   LogSpaceUsedPersent 
===============================================
MainDB           6579.93    65.8095             

I also used the following code to view the amount of space used by the main database file.

with CteDbSizes
as
(
    select database_id, type, size * 8.0 / 1024 size , f.physical_name
    from sys.master_files f
)
select 
    dbFileSizes.[name] AS DatabaseName,
    (select sum(size) from CteDbSizes where type = 1 and CteDbSizes.database_id = dbFileSizes.database_id) LogFileSizeMB,
    (select sum(size) from CteDbSizes where type = 0 and CteDbSizes.database_id = dbFileSizes.database_id) DataFileSizeMB
    --, (select physical_name from CteDbSizes where type = 0 and CteDbSizes.database_id = dbFileSizes.database_id) as PathPfFile
from sys.databases dbFileSizes ORDER BY DataFileSizeMB DESC

DatabaseName    LogFileSizeMB    DataFileSizeMB
===============================================
MainDB           6579.937500     7668.250000

But whatever I did, the amount of database log space is not less than 6 GB. Do you think there is a reason that the database log has not been changed for more than a month? Is there a solution to reduce this amount or not? I also used different methods and queries to reduce the size of the log file. I got good answers on other databases. Like folow:

use [master];
GO
USE [master]
GO
ALTER DATABASE [MainDB] SET RECOVERY SIMPLE WITH NO_WAIT
GO    
USE [MainDB]
GO
DBCC SHRINKDATABASE(N'MainDB')
GO
DBCC SHRINKFILE (N'MainDB_log' , EMPTYFILE)
GO
ALTER DATABASE [MainDB] SET RECOVERY FULL WITH NO_WAIT
GO

But in this particular database, the database log is still not less than 6 GB. Please help. Thanks.

As I informed in the main post, after a while, the size of our database file log reached about 25 gigs and we could no longer even compress the database files. After some searching, I came to the conclusion that I should back up the log file and then compress the log file. For this purpose, I defined a job that prepares a file from a log backup file almost every 30 minutes, and the size of these files usually does not exceed 150 MB. Then, after each backup of the log file, I run the log compression command once. With this method, the size of the log file is greatly reduced and now we have about 500 MB of log file. Of course, due to the large number of transactions on the database, the mentioned job must always be active. If the job is not active, I will increase the log volume again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM