Заметки сисадмина » Сжатие логов журнала транзакции MS SQL

Заметки сисадмина о интересных вещах из мира IT, инструкции и рецензии. Настраиваем Компьютеры/Сервера/1С/SIP-телефонию в Москве

Сжатие логов журнала транзакции MS SQL

2019-01-18 · Posted in SQL

Автоматический шринк журнала транзакций и перевод базы в простой режим восстановления.

На SQL сервере развернуто порядка 100 тестовых баз. Разворачивают в основном рабочие базы с режимом восстановления “Полный” и периодически забывая переводить в “Простую”. В связи с чем на сервере стали занимать много мест журналы транзакций. Вначале шринк журнала проходил руками, но потом это надоело и я решил это немного автоматизировать. Написал скрипт, который, пробегаясь по всем базам, кроме системных, переводит каждую в “Простой” режим восстановления и делает шринк журнала. Скрипт повесил на задание, для автоматического выполнения по расписанию.

Второй вариант:

The code below, get a list of non system databases, set the database to readonly and then shrink the file. I have kept this code in a few SQL Server boxes using SQL Agent Job, where space is always an issue. On Sat/Sun night every week, it start running and shrink all the databases within few hours (depending upon the size of databases):

In addition, you can find the option “Auto Shrink” in your MS SQL database properties. When you enable this option, the SQL Server will be periodically checking the unused space and reducing the size of the database and log files. Microsoft does not recommend using this option for typical databases, and if you decided to use Auto Shrink, your database should be running in the Full Recovery mode.

You can enable this option in the database parameters in the Automatic section. Just change the Auto Shrink parameter value to True. After you enabled autoshrink, MS SQL will perform automatic compression only if the unused space occupies more than 25% of the total volume size.

This method is applicable to all supported versions of SQL Server: 2005, 2008, 2012, 2014, and 2016, 2017.

This is just one of the ways to quickly reduce the size of the logs. Not the greatest, but very simple and effective.

Leave a Reply