简体   繁体   English

SQL Server数据库的LOG文件大小没有减少是什么原因?

[英]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.但无论我做什么,数据库日志空间的数量都不小于 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.但是在这个特定的数据库中,数据库日志仍然不少于6GB。 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.正如我在主要帖子中所告知的那样,一段时间后,我们的数据库文件日志的大小达到了大约 25 个 gig,我们甚至无法再压缩数据库文件。 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.为此,我定义了一个几乎每 30 分钟从一个日志备份文件准备一个文件的作业,这些文件的大小通常不超过 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.使用这种方法,日志文件的大小大大减少,现在我们有大约 500 MB 的日志文件。 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.如果作业不活跃,我会再次增加日志量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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