[SQL Server] トランザクションログを圧縮する方法 / Transaction Log 削除

inno
2014-04-25 00:13 7646 28
トランザクションログを圧縮する方法 / Transaction Log 削除

データベースを使うとトランザクションログがどんどん増えて行く。
トランザクションログを空にしないといつかはディスクの容量が限界に達してサービスが停止することもある。

トランザクションログを空にするにはトランザクションログをバックアップすれば空になる。
だが、既に大きくなっていまったトランザクションログファイルのサイズは元に戻らない。

大量のデータを処理してトランザクションログのサイズが1GBになっているとしよう。
トランザクションログをバックアップしてログファイルの内容を空にしたとしよう。
だが、トランザクションログファイルのサイズは1GBになっているとことだ。
その空になったファイルを圧縮して小さくする必要がある。

では、データベースのトランザクションログがどのぐらい使用されているか確認をしたい!!!

DBCC SQLPERF(LOGSPACE)

上記のSQLを実行するとデータベースごとにトランザクションログの使用量が確認できる。
まず、どうやって実行すればいいのか下記のSQLを見よう。

BACKUP LOG [データベース名] WITH TRUNCATE_ONLY 
DBCC SHRINKFILE ('[論理ファイル名]', [圧縮サイズ(単位:MB)])

1行目がログをバックアップする。
2行目は空になったトランザクションログファイルを圧縮する。


論理ファイル名を分かりません!!!という方は下記のSQLを参考。

EXEC sp_helpfile

論理ファイル名を確認したいデータベースから上記のSQLを実行すると
下記のような結果がでる。

実行結果


ちなみに、私は「test」というデータベースを作成してテストしている。
では、「test」データベースからログファイルの物理ファイル名「test_log」ファイルのサイズを1MBに変更しよう。

BACKUP LOG test TO DISK = N'NUL'
DBCC SHRINKFILE ('test_log', 1)

これを実行すると実行結果が下記のとおりだ。


「size」が1024KBになっている。
もし、上記のSQLを実行した時に下記のエラーが発生した場合、

メッセージ 4214、レベル 16、状態 1、行 1
現在、データベースのバックアップが存在しないので、BACKUP LOG を実行できません。
メッセージ 3013、レベル 16、状態 1、行 1
BACKUP LOG が異常終了しています。

これはまだ該当データベースのフールパックアップをしていないからだ。

BACKUP DATABASE test TO DISK = N'NUL'

このSQLを実行した後に再度上記のSQLを実行すれば正常に処理される。
これでトランザクションログファイルの圧縮作業は終わり。

コメント

Johnc775 2016年 10月 27日 (01:31)
Hiya, I am really glad I have found this information. Nowadays bloggers publish just about gossips and web and this is really annoying. A good blog with interesting content, that is what I need. Thanks for keeping this website, I'll be visiting it. Do you do newsletters? Can not find it. bccbbeabcdfd
Johnf709 2017年 03月 02日 (06:39)
I have observed that in the world these days, video games include the latest popularity with kids of all ages. Periodically it may be extremely hard to drag the kids away from the games. If you want the best of both worlds, there are plenty of educational games for kids. Great post. bbfdekedggge
Pharme765 2017年 03月 02日 (20:27)
Hello!<a href="http://www.cia9online.com/#1.html">cialis online pharmacy</a>
Pharmk194 2017年 03月 02日 (20:27)
Hello! [url=http://www.cia9online.com/#2.html]cialis online pharmacy[/url]
Pharmd388 2017年 03月 02日 (20:27)
Hello! cialis online pharmacy http://www.cia9online.com/#4.html
Pharmf683 2017年 03月 02日 (20:27)
Hello!
Pharmc34 2017年 03月 04日 (02:32)
Hello!<a href="http://www.cia9online.com/#1.html">cialis</a>
Pharmc20 2017年 03月 04日 (02:32)
Hello! [url=http://www.cia9online.com/#2.html]cialis[/url]
Pharmb44 2017年 03月 04日 (02:33)
Hello! cialis http://www.cia9online.com/#4.html
Pharmd819 2017年 03月 04日 (02:33)
Hello!
Pharme822 2017年 03月 05日 (08:55)
Hello!<a href="http://www.cia9online.com/#1.html">online pharmacy cialis</a>
Pharmf246 2017年 03月 05日 (08:55)
Hello! [url=http://www.cia9online.com/#2.html]online pharmacy cialis[/url]
Pharmd271 2017年 03月 05日 (08:55)
Hello! online pharmacy cialis http://www.cia9online.com/#4.html
Pharmf933 2017年 03月 05日 (08:56)
Hello!
Pharme309 2017年 03月 06日 (15:00)
Hello!<a href="http://www.cia9online.com/#1.html">cialis online pharmacy</a>
Pharmd792 2017年 03月 06日 (15:01)
Hello! [url=http://www.cia9online.com/#2.html]cialis online pharmacy[/url]
Pharmf732 2017年 03月 06日 (15:03)
Hello! cialis online pharmacy http://www.cia9online.com/#4.html
Pharmd911 2017年 03月 06日 (15:06)
Hello!
Pharmd684 2017年 03月 07日 (21:12)
Hello!<a href="http://www.cia9online.com/#1.html">cialis</a>
Pharmd198 2017年 03月 07日 (21:12)
Hello! [url=http://www.cia9online.com/#2.html]cialis[/url]
Pharmg550 2017年 03月 07日 (21:13)
Hello! cialis http://www.cia9online.com/#4.html
Pharme987 2017年 03月 09日 (03:19)
Hello!<a href="http://www.cia9online.com/#1.html">cialis online</a>
Pharmd216 2017年 03月 09日 (03:19)
Hello! [url=http://www.cia9online.com/#2.html]cialis online[/url]
Pharmb249 2017年 03月 09日 (03:19)
Hello! cialis online http://www.cia9online.com/#4.html
Pharmk73 2017年 03月 10日 (09:20)
Hello!<a href="http://www.cia9online.com/#1.html">cialis</a>
Pharma177 2017年 03月 10日 (09:20)
Hello! [url=http://www.cia9online.com/#2.html]cialis[/url]
Pharmf917 2017年 03月 10日 (09:20)
Hello! cialis http://www.cia9online.com/#4.html
Pharmd115 2017年 03月 10日 (09:20)
Hello!