简体   繁体   English

“ bat文件”运行SQL日志文件收缩任务

[英]“bat file” to run SQL log file shrink task

I have a SQL2012 DB that has a log file growing very rapidly. 我有一个SQL2012 DB,该日志文件的增长非常快。 I have a 3rd party program doing full database backups so I'm not worried about that. 我有一个第三方程序来进行完整的数据库备份,因此我对此并不担心。 I have the command to run in the SQL manager to shrink the log file and that is working but it's a very manual task. 我有命令在SQL管理器中运行以缩小日志文件,并且该命令正在运行,但这是一项非常手动的任务。

I'm trying to create a script that I can schedule to run every 2 weeks but a BAT file won't work it seems as after the sqlcmd -S servername\\instancename -E command is run it just goes to 1> and stops. 我正在尝试创建一个可以安排每2周运行一次的脚本,但是BAT文件将无法运行,就像在运行sqlcmd -S servername\\instancename -E命令之后,它只是转到1>并停止。 I need to run the USE and DBCC commands but not sure how to achieve this. 我需要运行USE和DBCC命令,但不确定如何实现这一点。

I have tried looking online but nowhere can I find a way to make a double click script to run this DBCC SHRINKFILE task. 我尝试过在线查找,但是找不到任何方法可以使双击脚本运行此DBCC SHRINKFILE任务。

So using sqlcmd, I can do this: 因此,使用sqlcmd,我可以这样做:

USE DBNAME
GO
DBCC SHRINKFILE (DBNAME_20070302091322_Log, 1);
GO

Running manually the file shrinks to below 10mb and that is correct. 手动运行,文件会缩小到10mb以下,这是正确的。 I need to just automate this task. 我只需要自动化此任务即可。

I have a SQL2012 DB that has a log file growing very rapidly 我有一个SQL2012数据库,该日志文件的增长非常快

In this case you must verify the database recovery model and log_reuse_wait_desc which you can identity with following command: 在这种情况下,您必须验证可以使用以下命令标识的数据库恢复模型log_reuse_wait_desc

Select name, recovery_model_desc, log_reuse_wait_desc
from sys.databases

If Recovery model set to FULL: 如果恢复模型设置为“完全”:

As mentioned in the comments, you must schedule LOG BACKUP at the backup tool that you are using for Full Backups, even you perform DBCC SHRINKFILE it would't shrink the size due to active portion in the log file, you must perform appropriate action bases on log_reuse_wait_desc status before running DBCC SHRINKFILE command. 如注释中所述,您必须在用于完全备份的备份工具上安排LOG BACKUP ,即使执行DBCC SHRINKFILE ,由于日志文件中的活动部分,它也不会缩小大小,因此必须执行适当的操作基础在运行DBCC SHRINKFILE命令之前,以log_reuse_wait_desc状态运行。 You can verify it looking at message section once DBCC command you executed. 一旦执行DBCC命令,您就可以在消息部分查看它。

For more details on LOG FILE/CHECKPOINTS/ log_reuse_wait .. 有关LOG FILE / CHECKPOINTS / log_reuse_wait更多详细信息。

If Recovery model set to Simple: 如果恢复模型设置为“简单”:

Once you decided to go with Simple recovery model, shrink the file manually, but keep enough room for SQL Engine to perform log operations depending on transaction workload. 一旦决定采用简单恢复模型,就可以手动收缩文件,但要留有足够的空间供SQL Engine根据事务工作量执行日志操作。 Shrinking the log file to lower size means putting unnecessary overload on the server as the SQL Engine try to increase it again when there is no enough space available for the transaction workload/throughput. 将日志文件缩小到较小的大小意味着,如果没有足够的空间可用于事务工作负载/吞吐量,则SQL引擎会尝试再次增加该文件,从而使服务器上出现不必要的过载。

You do not have schedule LOG backup in simple recovery model, as the log file checkpoints would be automatically added and based on checkpoint log would be truncated. 您没有简单恢复模型中的计划LOG backup ,因为将自动添加日志文件检查点,并且基于检查点的日志将被截断。

So looks like with your help i managed to get it working. 因此,在您的帮助下,我设法使其正常运行。 I have a BAT with the -i option where the DBCC command is. 我的DBCC命令带有-i选项的BAT。

here is that bat 这是蝙蝠

sqlcmd -S sqlserver\\instance -E -d databasename -ic:\\test.sql -oc:\\out.txt sqlcmd -S sqlserver \\ instance -E -d数据库名-ic:\\ test.sql -oc:\\ out.txt

here is hte test.sql 这是test.sql

USE databasename GO DBCC SHRINKFILE (DBLOGFILENAME_Log, 1); USE数据库名GO DBCC SHRINKFILE(DBLOGFILENAME_Log,1); GO

tested and its working. 测试和工作。

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

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