简体   繁体   English

MS SQL。 如何缩小日志文件的时间点?

[英]MS SQL. How can I find time-point when log file was shrink?

Someone is shrinking the log files of databases in production. 有人正在缩减生产中数据库的日志文件。 Now and then. 时不时。 It's not good to shrink often. 经常收缩是不好的。

But how do I find information of when and what sql user did the log file shrinking? 但是,如何找到日志文件收缩的时间和SQL用户的信息?

Everyting on MS SQL server is logged so somewhere is the information I seek. MS SQL Server上的所有记录都会记录下来,因此我要查找的信息在某处。 Some kind of system view or table? 某种系统视图或表?

If you right click on your database, go to reports and the select "Disk Usage" (from memory, I'm not sat at a PC to check) it will show the events. 如果右键单击数据库,请转到报告,然后选择“磁盘使用情况”(从内存中,我不是坐在PC上检查),它将显示事件。

However, the information is from the default trace so will only show recent events. 但是,该信息来自默认跟踪,因此将仅显示最近事件。 I believe the default trace is only 5 20MB files. 我相信默认跟踪只有5个20MB文件。

If it is more infrequent than this, you will have to set up a trace or use profiler (trace is lighter on resources than profiler) and look for Log File Auto shrink events 如果不常用,则必须设置跟踪或使用事件探查器(在资源上跟踪比事件探查器轻),并查找日志文件自动收缩事件

Go to Server > Management > Extended Events. 转到服务器>管理>扩展事件。 Then create new session and use Event library : 然后创建新会话并使用事件库: 在此处输入图片说明

Then you can view live data: 然后,您可以查看实时数据:

在此处输入图片说明

Or analyze past data: 或分析过去的数据:

在此处输入图片说明

Hope this helps. 希望这可以帮助。 If youre interested in reading extended logs file directly using t-sql, I can help too ;) 如果您有兴趣直接使用t-sql读取扩展日志文件,我也可以提供帮助;)

when and what SQL user did the log file shrinking??? 何时以及什么SQL用户将日志文件缩小了???

You should be able to determine this from the "last modified" time on the "ldf" file (unless it has autogrown in the interim - which seems unlikely as it is still only 1MB) 您应该能够从"ldf"文件上的"last modified"时间确定这一点(除非该文件在过渡期间已自动增长-这似乎不太可能,因为它仍然只有1MB)

Does the database have logged activity happening? 数据库是否发生了记录的活动? Is the autoshrink database option turned on? 自动收缩数据库选项是否打开?

You wouldn't see a SQL agent job if that was the case a background task performs these shrink operations. 如果后台任务执行这些收缩操作,则不会看到SQL代理作业。 This setting is extremely unrecommended 极不推荐此设置

Log file was shrunk. 日志文件已缩小。 Who did it? 谁干的?

The default trace can be looked at to determine by whom and when the shrink file was executed. 可以查看默认跟踪以确定收缩文件的执行者和执行时间。

To search the default trace, first determine if the default trace is enabled. 要搜索默认跟踪,请首先确定是否启用了默认跟踪。 This can be done by running sp_configure as follows: 可以通过如下运行sp_configure来完成:

SQL: SQL:

EXEC master.dbo.sp_configure 'show advanced options', 1; 
GO 
EXEC master.dbo.sp_configure 'default trace enabled';
GO

Once the default trace is validated and running, determine the location the trace files are being written to by running the function, fn_trace_getinfo 验证并运行了默认跟踪后,通过运行函数fn_trace_getinfo确定写入跟踪文件的位置

SQL: SQL:

SELECT * 
FROM fn_trace_getinfo(default);
GO

To read the trace file, use fn_trace_gettable. 要读取跟踪文件,请使用fn_trace_gettable。 This will pull all the data from the trace file and show it in a table format in SSMS for ease of reviewing 这将从跟踪文件中提取所有数据,并以表格格式在SSMS中显示它们,以便于检查

SQL: SQL:

SELECT 
    TextData,
    HostName,
    ApplicationName,
    LoginName, 
    StartTime  
FROM 
[fn_trace_gettable]('C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Log\log_75.trc', DEFAULT) 
WHERE TextData LIKE '%SHRINKFILE%'; ----- Location of default trace will be different ,so kindly check that accordingly

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

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