简体   繁体   English

SQL Server日志文件-当不使用数据库时,为什么它的大小增加了?

[英]SQL Server Log File - Why it increases in size when the database is not in use?

We have an ERP application that uses a SQL server database. 我们有一个使用SQL Server数据库的ERP应用程序。 Although the application is not in use currently, its log file increases constantly and is currently at 28GB. 尽管当前未使用该应用程序,但其日志文件不断增加,目前为28GB。 I took a full backup couple of weeks back to truncate the log file when it was 85GB and now it is back to 28GB. 我花了几周的时间进行完整备份,以截断日志文件(当时它是85GB),现在又回到了28GB。 I've setup the log file size to be 20GB with 100MB increment and unrestricted growth. 我已将日志文件大小设置为20GB,增量为100MB,并且不受限制地增长。

Why the log file size is increasing when the application is not in use, how can I see the transactions that are causing it to increase and how can I manage it better? 为什么在不使用应用程序时日志文件的大小会增加,如何查看导致日志文件增加的事务以及如何对其进行更好的管理? Does setting up a server trace is useful in this case? 在这种情况下,设置服务器跟踪是否有用?

Check the output of sys.dm_exec_requests and sys.dm_exec_sessions to find out what queries are running and what/who's connected to your database. 检查sys.dm_exec_requestssys.dm_exec_sessions的输出,以查找正在运行的查询以及连接到数据库的对象。

If that doesn't help you, then you could certainly set up a server trace or an extended events session - if you really think no one is connecting and querying, you could verify that using something like this: 如果那对您没有帮助,那么您当然可以设置服务器跟踪或扩展事件会话-如果您真的认为没有人在连接和查询,则可以使用以下方法进行验证:

IF EXISTS (SELECT * FROM sys.server_event_sessions WHERE name = 'EE_Queries')
DROP EVENT SESSION EE_Queries ON SERVER;
GO

CREATE EVENT SESSION EE_Queries ON SERVER
ADD EVENT sqlserver.sql_statement_completed
(ACTION (sqlserver.sql_text,sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.nt_username, sqlserver.username)
WHERE sqlserver.database_id = /* fill in your DB_ID() here */)
ADD TARGET package0.asynchronous_file_target
(SET FILENAME = N'C:\logs\EE_Queries.xel', METADATAFILE = N'C:\logs\EE_Queries.xem')
WITH (max_dispatch_latency = 1 seconds);
GO


ALTER EVENT SESSION EE_Queries ON SERVER STATE = START;

which would log all queries going on against your database - just replace the comment portion with the DB_ID() of the database you want to track. 它将记录对数据库进行的所有查询-只需将注释部分替换为要跟踪的数据库的DB_ID() That will start an extended events session that tracks all queries, including the text of the query, the app name, the host name, and the username (NT and/or SQL user name) of the user that's running the query. 这将启动扩展事件会话,该会话跟踪所有查询,包括查询的文本,应用程序名称,主机名以及运行查询的用户的用户名(NT和/或SQL用户名)。 That should clue you in pretty quickly. 那应该很快就提示您。 Just be careful with that events session in a production environment on a busy database - it will likely degrade performance and may fill up the disk quickly if you have lots of queries . 请注意在繁忙数据库中的生产环境中的事件会话-如果您有很多查询,它可能会降低性能并可能很快填满磁盘 Output of that extended event locally: 扩展事件在本地的输出:

在此处输入图片说明

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

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