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

前端 未结 6 865
梦谈多话
梦谈多话 2020-12-04 13:08

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. Thats been built up over the six months we\'ve been

相关标签:
6条回答
  • 2020-12-04 13:52

    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.

    detaching the db & deleting the log file is dangerous! don't do this unless you'd like data loss

    0 讨论(0)
  • 2020-12-04 13:54

    Welcome to the fickle world of SQL Server log management.

    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? 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
    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!

    0 讨论(0)
  • 2020-12-04 14:02
    • 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
    • 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)
    0 讨论(0)
  • 2020-12-04 14:03

    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

    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
    
    0 讨论(0)
  • 2020-12-04 14:04

    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)

    (**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).

    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. (don`t worry the ldf file will be created after the attachment process.)

    6. After that, a new log file create with a size of 504 KB!!!.

    In step 2, if you faced an error that database is used by another user, you can:

    1.run this command on master database "sp_who2" and see what process using your database.

    2.read the process number, for example it is 52 and type "kill 52", now your database is free and ready to detach.

    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. Immediately click Ok button in the detach dialog box (that showed the detach error previously).
    0 讨论(0)
  • 2020-12-04 14:07
    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.

    2. Use dbcc shrinkfile or Management Studio to shrink the log files.

    Step #2 will do nothing until the backup mode is set.

    0 讨论(0)
提交回复
热议问题