简体   繁体   English

如何减小 sql server 日志文件的大小?

[英]How do I decrease the size of my sql server log file?

So I have been neglecting to do any backups of my fogbugz database, and now the fogbugz ldf file is over 2 and half gigs.所以我一直忽略对我的fogbugz数据库做任何备份,现在fogbugz ldf文件超过2个半演出。 Thats been built up over the six months we've been using fogbugz.这是我们在使用fogbugz 的六个月中建立起来的。

I backed up the database, then I backed up, and truncated the transaction log, yet the transaction log is still 2 and a half gigs.我备份了数据库,然后我备份,并截断了事务日志,但事务日志仍然是 2 个半演出。 I did a shrink on the log file and its still 2 and a half gigs.我对日志文件做了一个缩小,它仍然是 2 个半的演出。 Nothing I do seems to shrink the file in size.我所做的一切似乎都没有缩小文件的大小。

Is there anyway to fix the problem?有没有办法解决这个问题? Or is the only way back at this point to detach the database, delete the log file and then reattach with a new one?或者是此时分离数据库,删除日志文件然后重新附加一个新的唯一方法?

  • Perform a full backup of your database.执行数据库的完整备份。 Don't skip this.不要跳过这个。 Really.真的。
  • Change the backup method of your database to "Simple"将数据库的备份方法更改为“简单”
  • Open a query window and enter "checkpoint" and execute打开一个查询窗口,输入“checkpoint”并执行
  • Perform another backup of the database执行数据库的另一个备份
  • Change the backup method of your database back to "Full" (or whatever it was, if it wasn't already Simple)将数据库的备份方法改回“完整”(或其他任何方法,如果它还不是简单的话)
  • Perform a final full backup of the database.执行数据库的最终完整备份。
  • Run below queries one by one一一运行以下查询
    1. USE Database_Name
    2. select name,recovery_model_desc from sys.databases
    3. ALTER DATABASE Database_Name SET RECOVERY simple
    4. DBCC SHRINKFILE (Database_Name_log , 1)

Welcome to the fickle world of SQL Server log management.欢迎来到变化无常的 SQL Server 日志管理世界。

SOMETHING is wrong, though I don't think anyone will be able to tell you more than that without some additional information.有些事情是错误的,但我认为没有一些额外的信息,没有人能告诉你更多。 For example, has this database ever been used for Transactional SQL Server replication?例如,此数据库是否曾用于事务性 SQL Server 复制? This can cause issues like this if a transaction hasn't been replicated to a subscriber.如果事务尚未复制到订阅者,这可能会导致类似的问题。

In the interim, this should at least allow you to kill the log file:在此期间,这至少应该允许您终止日志文件:

  1. Perform a full backup of your database.执行数据库的完整备份。 Don't skip this.不要跳过这个。 Really.真的。
  2. Change the backup method of your database to "Simple"将数据库的备份方法更改为“简单”
  3. Open a query window and enter "checkpoint" and execute打开一个查询窗口,输入“checkpoint”并执行
  4. Perform another backup of the database执行数据库的另一个备份
  5. Change the backup method of your database back to "Full" (or whatever it was, if it wasn't already Simple)将数据库的备份方法改回“完整”(或其他任何方法,如果它还不是简单的话)
  6. Perform a final full backup of the database.执行数据库的最终完整备份。

You should now be able to shrink the files (if performing the backup didn't do that for you).您现在应该能够缩小文件(如果执行备份不适合您)。

Good luck!祝你好运!

  1. Ensure the database's backup mode is set to Simple (see here for an overview of the different modes).确保数据库的备份模式设置为简单(有关不同模式的概述,请参见此处)。 This will avoid SQL Server waiting for a transaction log backup before reusing space.这将避免 SQL Server 在重用空间之前等待事务日志备份。

  2. Use dbcc shrinkfile or Management Studio to shrink the log files.使用dbcc shrinkfileManagement Studio来收缩日志文件。

Step #2 will do nothing until the backup mode is set.在设置备份模式之前,步骤 #2 将不执行任何操作。

This is one of the best suggestion in which is done using query.这是使用查询完成的最佳建议之一。 Good for those who has a lot of databases just like me.适合像我一样拥有大量数据库的人。 Can run it using a script.可以使用脚本运行它。

https://medium.com/@bharatdwarkani/shrinking-sql-server-db-log-file-size-sql-server-db-maintenance-7ddb0c331668 https://medium.com/@bharatdwarkani/shrinking-sql-server-db-log-file-size-sql-server-db-maintenance-7ddb0c331668

USE DatabaseName;
GO
-- Truncate the log by changing the database recovery model to SIMPLE.
ALTER DATABASE DatabaseName
SET RECOVERY SIMPLE;
GO
-- Shrink the truncated log file to 1 MB.
DBCC SHRINKFILE (DatabaseName_Log, 1);
GO
-- Reset the database recovery model.
ALTER DATABASE DatabaseName
SET RECOVERY FULL;
GO

You have to shrink & backup the log a several times to get the log file to reduce in size, this is because the the log file pages cannot be re-organized as data files pages can be, only truncated.您必须多次收缩和备份日志才能减小日志文件的大小,这是因为日志文件页面不能像数据文件页面那样重新组织,只能被截断。 For a more detailed explanation check this out.有关更详细的解释, 请查看此内容。

WARNING : Detaching the db & deleting the log file is dangerous!警告:分离数据库并删除日志文件是危险的! don't do this unless you'd like data loss除非您希望数据丢失,否则不要这样做

I had the same problem, my database log file size was about 39 gigabyte, and after shrinking (both database and files) it reduced to 37 gigabyte that was not enough, so I did this solution: (I did not need the ldf file (log file) anymore)我遇到了同样的问题,我的数据库日志文件大小约为 39 GB,在缩小(数据库和文件)后,它减少到 37 GB 不够用,所以我做了这个解决方案:(我不需要 ldf 文件(日志文件)了)

(**Important) : Get a full backup of your database before the process. (**重要):在此过程之前获取数据库的完整备份。

  1. Run "checkpoint" on that database.在该数据库上运行“检查点”。

  2. Detach that database (right click on the database and chose tasks >> Detach...) {if you see an error, do the steps in the end of this text}分离该数据库(右键单击该数据库并选择任务 >> 分离...){如果您看到错误,请执行本文末尾的步骤}

  3. Move MyDatabase.ldf to another folder, you can find it in your hard disk in the same folder as your database (Just in case you need it in the future for some reason such as what user did some task).将 MyDatabase.ldf 移动到另一个文件夹,您可以在与数据库相同的文件夹中的硬盘中找到它(以防万一您将来由于某些原因需要它,例如用户执行了某些任务)。

  4. Attach the database (right click on Databases and chose Attach...)附加数据库(右键单击数据库并选择附加...)

  5. On attach dialog remove the .ldf file (which shows 'file not found' comment) and click Ok.在附加对话框中删除 .ldf 文件(显示“找不到文件”注释)并单击“确定”。 (don`t worry the ldf file will be created after the attachment process.) (不要担心在附件过程后会创建 ldf 文件。)

  6. After that, a new log file create with a size of 504 KB!!!.之后,会创建一个大小为 504 KB 的新日志文件!!!。

In step 2, if you faced an error that database is used by another user, you can:在步骤 2 中,如果您遇到数据库被其他用户使用的错误,您可以:

1.run this command on master database "sp_who2" and see what process using your database. 1.在主数据库“sp_who2”上运行这个命令,看看是什么进程在使用你的数据库。

2.read the process number, for example it is 52 and type "kill 52", now your database is free and ready to detach. 2.读取进程号,例如它是52并输入“kill 52”,现在你的数据库是空闲的,可以分离了。

If the number of processes using your database is too much:如果使用数据库的进程数量过多:

1.Open services (type services in windows start) find SQL Server ... process and reset it (right click and chose reset). 1.打开服务(在windows start中输入services)找到SQL Server ...进程并重置它(右键单击并选择重置)。

  1. Immediately click Ok button in the detach dialog box (that showed the detach error previously).立即单击分离对话框中的确定按钮(之前显示分离错误)。

暂无
暂无

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

相关问题 如何在SQL中增加或减少字段价格? - How do I increase or decrease a field price in SQL? 如何从SQL Server中的.sql文件创建表 - How do I create tables from my .sql file in SQL Server 如何减少SQL日志文件的大小 - How to reduce SQL log file size 如何使用ucanaccess访问Java接口的远程服务器/如何找到远程SQL Server文件路径? - How do I use ucanaccess to access a remote server for a java interface / how do I find my remote SQL Server file path? SQL Server:如何在批量删除表和行后减小数据库大小 - SQL Server : how to decrease database size after bulk delete tables and rows SQL Server数据库事务日志文件的大小急剧增加 - SQL Server Database Transaction Log File size increased dramatically 如何将文件表与 SQL Server 中的现有表链接 - How do I link the File Table with my existing table in SQL server 如何将 sql 数据文件导入 SQL Server? - How do I import a sql data file into SQL Server? 如何将我的数据库上的所有sql server管理工作室激活作为可执行的tsql日志? - How can i get all my sql server managment studio activite on my database as a executable tsql log? 如果我的 SQL 服务器没有多个数据库的选项,我如何添加成员加入/登录并将配置文件添加到我的网站的方法 - How do I add a way for members to join/Log in and have profiles to my website if my SQL server doesn't have the option for more than the one database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM