SQL Server 数据库清除日志的方法
方法一:?
1、打開查詢分析器,輸入命令?
BACKUP LOG database_name WITH NO_LOG?
2、再打開企業(yè)管理器--右鍵要壓縮的數(shù)據(jù)庫--所有任務(wù)--收縮數(shù)據(jù)庫--收縮文件--選擇日志文件--在收縮方式里選擇收縮至xxm,這里會給出一個(gè)允許收縮到的最小m數(shù),直接輸入這個(gè)數(shù),確定就可以了。?
方法二:?
設(shè)置檢查點(diǎn),自動截?cái)嗳罩?span id="ozvdkddzhkzd" class="Apple-converted-space">?
一般情況下,SQL數(shù)據(jù)庫的收縮并不能很大程度上減小數(shù)據(jù)庫大小,其主要作用是收縮日志大小,應(yīng)當(dāng)定期進(jìn)行此操作以免數(shù)據(jù)庫日志過大?
1、設(shè)置數(shù)據(jù)庫模式為簡單模式:打開SQL企業(yè)管理器,在控制臺根目錄中依次點(diǎn)開Microsoft SQL Server-->SQL Server組-->雙擊打開你的服務(wù)器-->雙擊打開數(shù)據(jù)庫目錄-->選擇你的數(shù)據(jù)庫名稱(如用戶數(shù)據(jù)庫cwbase1)-->然后點(diǎn)擊右鍵選擇屬性-->選擇選項(xiàng)-->在故障還原的模式中選擇“簡單”,然后按確定保存?
2、在當(dāng)前數(shù)據(jù)庫上點(diǎn)右鍵,看所有任務(wù)中的收縮數(shù)據(jù)庫,一般里面的默認(rèn)設(shè)置不用調(diào)整,直接點(diǎn)確定?
3、收縮數(shù)據(jù)庫完成后,建議將您的數(shù)據(jù)庫屬性重新設(shè)置為標(biāo)準(zhǔn)模式,操作方法同第一點(diǎn),因?yàn)槿罩驹谝恍┊惓G闆r下往往是恢復(fù)數(shù)據(jù)庫的重要依據(jù)?
方法三:通過SQL收縮日志?
把代碼復(fù)制到查詢分析器里,然后修改其中的3個(gè)參數(shù)(數(shù)據(jù)庫名,日志文件名,和目標(biāo)日志文件的大小),運(yùn)行即可?
SET NOCOUNT ON?
DECLARE @LogicalFileName sysname,?
@MaxMinutes INT,?
@NewSize INT?
USE tablename -- 要操作的數(shù)據(jù)庫名?
SELECT @LogicalFileName = 'tablename_log', -- 日志文件名?
@MaxMinutes = 10, -- Limit on time allowed to wrap log.?
@NewSize = 1 -- 你想設(shè)定的日志文件的大小(M)?
-- Setup / initialize?
DECLARE @OriginalSize int?
SELECT @OriginalSize = size?
FROM sysfiles?
WHERE name = @LogicalFileName?
SELECT 'Original Size of ' + db_name() + ' LOG is ' +?
CONVERT(VARCHAR(30),@OriginalSize) + ' 8K pages or ' +?
CONVERT(VARCHAR(30),(@OriginalSize*8/1024)) + 'MB'?
FROM sysfiles?
WHERE name = @LogicalFileName?
CREATE TABLE DummyTrans?
(DummyColumn char (8000) not null)?
DECLARE @Counter INT,?
@StartTime DATETIME,?
@TruncLog VARCHAR(255)?
SELECT @StartTime = GETDATE(),?
@TruncLog = 'BACKUP LOG ' + db_name() + ' WITH TRUNCATE_ONLY'?
DBCC SHRINKFILE (@LogicalFileName, @NewSize)?
EXEC (@TruncLog)?
-- Wrap the log if necessary.?
WHILE @MaxMinutes > DATEDIFF (mi, @StartTime, GETDATE()) -- time has not expired?
AND @OriginalSize = (SELECT size FROM sysfiles WHERE name = @LogicalFileName)?
AND (@OriginalSize * 8 /1024) > @NewSize?
BEGIN -- Outer loop.?
SELECT @Counter = 0?
WHILE ((@Counter < @OriginalSize / 16) AND (@Counter < 50000))?
BEGIN -- update?
INSERT DummyTrans VALUES ('Fill Log')?
DELETE DummyTrans?
SELECT @Counter = @Counter + 1?
END?
EXEC (@TruncLog)?
END?
SELECT 'Final Size of ' + db_name() + ' LOG is ' +?
CONVERT(VARCHAR(30),size) + ' 8K pages or ' +?
CONVERT(VARCHAR(30),(size*8/1024)) + 'MB'?
FROM sysfiles?
WHERE name = @LogicalFileName?
DROP TABLE DummyTrans?
SET NOCOUNT OFF?
方法四:刪除日志文件。?
此方法有一定的風(fēng)險(xiǎn)性,因?yàn)閟ql server的日志文件不是即時(shí)寫入數(shù)據(jù)庫主文件的,如處理不當(dāng),會造成數(shù)據(jù)的損失。1、操作前請斷開所有數(shù)據(jù)庫連接。?
2、分離數(shù)據(jù)庫?
分離數(shù)據(jù)庫:企業(yè)管理器->服務(wù)器->數(shù)據(jù)庫->cwbase1->右鍵->分離數(shù)據(jù)庫?
分離后,cwbase1數(shù)據(jù)庫被刪除,但保留了數(shù)據(jù)文件和日志文件?
3、刪除log物理文件?
刪除LOG物理文件,然后附加數(shù)據(jù)庫: 企業(yè)管理器->服務(wù)器->數(shù)據(jù)庫->右鍵->附加數(shù)據(jù)庫?
此法生成新的log,大小只有500多k。?
注意:建議使用第一種方法。操作前請確保所有操作員都已經(jīng)推出系統(tǒng),斷開數(shù)據(jù)庫的連接。?
以上操作前,請務(wù)必做好數(shù)據(jù)備份!
1.sql server 2005 清除日志語句?
dump transaction 數(shù)據(jù)庫名稱 with no_log?
backup log 數(shù)據(jù)庫名稱 with no_log?
dbcc shrinkdatabase(數(shù)據(jù)庫名稱)?
2.sql server 2008 清除日志語句?
sp_dboption 數(shù)據(jù)庫名稱, "trunc. log on chkpt.", true?
checkpoint?
sp_dboption 數(shù)據(jù)庫名稱, "autoshrink", true?
清除SQLSERVER數(shù)據(jù)庫日志文件的方法:?
1、先將這個(gè)數(shù)據(jù)庫卸載:?
EXEC sp_detach_db 'database_name', 'true'?
然后將該數(shù)據(jù)庫所對應(yīng)的Log文件刪掉;?
最后,再將這個(gè)數(shù)據(jù)庫注冊到系統(tǒng)里面:?
EXEC sp_attach_db @dbname = N'database_name',?
@filename1 = N'e:\mssql7\data\database_name_data.mdf'?
2、數(shù)據(jù)庫上點(diǎn)右鍵-所有任務(wù)-收縮數(shù)據(jù)庫-選擇收縮文件為LOG 。?
3、清除SQLSERVER數(shù)據(jù)庫日志的方法:?
*******下面是轉(zhuǎn)發(fā)的郵件*****?
The shrinking of log files is not immediate in SQL Server 7.0. The?
shrinking of log files does not occur until the active portion of the?
log moves. As updates are performed on the database, the shrink?
operation occurs at checkpoints or transaction log backups. Each log?
file is marked with the target_percent for the shrink operation. Each?
subsequent log backup or log truncation attempts to shrink the file to?
bring its size as close to the target_percent as possible. Because a log?
file can be shrunk only to a virtual log file boundary, it may not be?
possible to shrink a log file to a size smaller than the size of a?
virtual log file even if it is not being used. Please refer to SQL Book?
Online for the details.?
RESOLUTION?
Below script will help to shrink the log file immediately, pls keep it?
running for 3~4 minutes and then stop it manually.?
\* Run "select fileid, name,filename from ..sysfiles" to get?
the fileid which you want to shrink *\?
use?
go?
dbcc shrinkfile(fileid,notruncate)?
dbcc shrinkfile(fileid,truncateonly)?
create table t1 (char1 char(4000))?
go?
declare @i int?
select @i = 0?
while (1 = 1)?
begin?
while (@i < 100)?
begin?
insert into t1 values ('a') select @i = @i +1?
end?
truncate table t1?
backup log with truncate_only?
end?
go?
*****轉(zhuǎn)發(fā)內(nèi)容結(jié)束*****?
SQLServer數(shù)據(jù)庫日志清理 清除sqlserver2005日志?
有時(shí)候當(dāng)系統(tǒng)運(yùn)行時(shí)間比較長的時(shí)候,我們把備份的數(shù)據(jù)庫還原的時(shí)候發(fā)現(xiàn),數(shù)據(jù)庫中數(shù)據(jù)文件和日志文件變的好大,特別是日志文件。現(xiàn)在給大家介紹如何清理SQLServer數(shù)據(jù)庫日志;有兩種方法如下:
方法一:手動清除sqlserver2005日志?
1.右鍵在清除日志的數(shù)據(jù)庫,如“TestDB”,點(diǎn)擊[新建查詢(Q)]?
2.輸入以下SQL語句,其中“TestDB”是數(shù)據(jù)庫名稱?
DUMP TRANSACTION TestDB WITH NO_LOG?
3.執(zhí)行該SQL,成功后繼續(xù)以下操作?
4.右鍵該數(shù)據(jù)庫節(jié)點(diǎn),點(diǎn)擊[任務(wù)(T)] -> [收縮(S)] -> [文件(F)]?
5.在彈出的“收縮文件”對話框中,將“文件類型(T)”選為“日志”,將“收縮操作”選中“在釋放未使用的空間前重新組織頁(O)”?
6.在“將文件收縮到(K)”文本框中輸入后面提示的最小大小的數(shù)值,點(diǎn)擊[確定]即可。?
方法二:用工具軟件SqlServer日志清除專家3.0,可對Sql Server 6.5到Sql Server 2005的各種版本的數(shù)據(jù)庫日志的清除;其使用方法非常簡單;SqlServer 日志清除專家綠色版 V3.5下載地址:?
下載地址?http://www.jb51.net/softs/21840.html?
方法一操作起來相對麻煩一些,可是可以定制日志的大小,清理日志后其相應(yīng)的數(shù)據(jù)庫數(shù)據(jù)文件在也會變小,數(shù)據(jù)也不會丟失;方法二操作比較方便,可以把數(shù)據(jù)庫中的日志文件清理到1M大小;
原文鏈接:http://www.jb51.net/article/30811.htm
轉(zhuǎn)載于:https://www.cnblogs.com/xuhongfei/archive/2013/04/03/2997444.html
總結(jié)
以上是生活随笔為你收集整理的SQL Server 数据库清除日志的方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 找一个婚礼直播的帖子,求有爱的jms帮帮
- 下一篇: 贪心算法之最优装载