方法一:
--讓日志不再增長,但是不能壓縮日志 
exec sp_dboption 'your_dbname', 'trunc. log', 'true' 
方法二:
--超強數據庫文件及日志文件壓縮, 
--壓縮后可能會導致數據庫不能正常訪問,重啟一下數據庫即可 
dump transaction [數據庫名] with no_log 
backup log [數據庫名] with no_log 
dbcc shrinkdatabase([數據庫名]) 
方法三: 
經常在csdn上看到網友發(fā)帖說,壓縮日志文件處理不當,導致數據庫損壞,甚至不能恢復數據,于是就寫了一個通用的數據庫日志文件壓縮的存儲過程來解決此問題: 
/**//*--壓縮數據庫的通用存儲過程 
壓縮日志及數據庫文件大小 
因為要對數據庫進行分離處理 
所以存儲過程不能創(chuàng)建在被壓縮的數據庫中 
--鄒建 2004.3--*/ 
/**//*--調用示例 
exec p_compdb 'test' 
--*/ 
use master --注意,此存儲過程要建在master數據庫中 
go 
if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[p_compdb]') and objectproperty(id, n'isprocedure') = 1) 
drop procedure [dbo].[p_compdb] 
go 
create proc p_compdb 
@dbname sysname, --要壓縮的數據庫名 
@bkdatabase bit=1, --因為分離日志的步驟中,可能會損壞數據庫,所以你可以選擇是否自動數據庫 
@bkfname nvarchar(260)='' --備份的文件名,如果不指定,自動備份到默認備份目錄,備份文件名為:數據庫名+日期時間 
as 
--1.清空日志 
exec('dump transaction ['[email protected]+'] with no_log') 
--2.截斷事務日志: 
exec('backup log ['[email protected]+'] with no_log') 
--3.收縮數據庫文件(如果不壓縮,數據庫的文件不會減小 
exec('dbcc shrinkdatabase(['[email protected]+'])') 
--4.設置自動收縮 
exec('exec sp_dboption '''[email protected]+''',''autoshrink'',''true''') 
--后面的步驟有一定危險,你可以可以選擇是否應該這些步驟 
--5.分離數據庫 
if @bkdatabase=1 
begin 
if isnull(@bkfname,'')='' 
set @[email protected]+'_'+convert(varchar,getdate(),112) 
+replace(convert(varchar,getdate(),108),':','') 
select 提示信息='備份數據庫到sql 默認備份目錄,備份文件名:'[email protected] 
exec('backup database ['[email protected]+'] to disk='''[email protected]+'''') 
end 
--進行分離處理 
create table #t(fname nvarchar(260),type int) 
exec('insert into #t select filename,type=status&0x40 from ['[email protected]+']..sysfiles') 
exec('sp_detach_db '''[email protected]+'''') 
--刪除日志文件 
declare @fname nvarchar(260),@s varchar(8000) 
declare tb cursor local for select fname from #t where type=64 
open tb 
fetch next from tb into @fname 
while @@fetch_status=0 
begin 
set @s='del "'+rtrim(@fname)+'"' 
exec master..xp_cmdshell @s,no_output 
fetch next from tb into @fname 
end 
close tb 
deallocate tb 
--附加數據庫 
set @s='' 
declare tb cursor local for select fname from #t where type=0 
open tb 
fetch next from tb into @fname 
while @@fetch_status=0 
begin 
set @[email protected]+','''+rtrim(@fname)+'''' 
fetch next from tb into @fname 
end 
close tb 
deallocate tb 
exec('sp_attach_single_file_db '''[email protected]+''''[email protected]) 
go 
新聞熱點
疑難解答