1.重建数据库所有表索引

DECLARE @TableName NVARCHAR(256);
DECLARE qdjimo CURSOR FOR
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE';
 
OPEN qdjimo;
 
FETCH NEXT FROM qdjimo INTO @TableName;
 
WHILE @@FETCH_STATUS = 0
BEGIN
    DBCC showcontig(@TableName); 
    DBCC DBREINDEX(@TableName);
    DBCC showcontig(@TableName); 
    FETCH NEXT FROM qdjimo INTO @TableName;
END
 
CLOSE qdjimo;
DEALLOCATE qdjimo;

2.清理数据库日志

    USE[master]
    GO
    ALTER DATABASE qdjimo SET RECOVERY SIMPLE WITH NO_WAIT
    GO
    ALTER DATABASE qdjimo SET RECOVERY SIMPLE   --简单模式,只有在这个模式下才行
    GO
    USE qdjimo
    GO
    DBCC SHRINKFILE (N'qdjimo_log' ,1024, TRUNCATEONLY)  --设置压缩后的日志大小为1024M,可以自行指定
    GO
    USE[master]
    GO
    ALTER DATABASE qdjimo SET RECOVERY FULL WITH NO_WAIT
    GO
    ALTER DATABASE qdjimo SET RECOVERY FULL  --还原为完全模式,否则影响数据库备份

    GO

3.备份数据库

declare @name varchar(250) 
set @name='D:\database\数据库\qdjimo'+rtrim(convert(varchar,getdate(),112))+'.bak' 
BACKUP DATABASE[qdjimo] TO  
DISK=@name 
WITH NOFORMAT, NOINIT,  
NAME = N'qdjimo-完整 数据库 备份', 
SKIP, NOREWIND, NOUNLOAD
go

/**删除七天前的备份**/
declare @cmd varchar(60)
set @cmd='del D:\database\数据库\qdjimo'+rtrim(convert(varchar,getdate()-7,112))+'.bak' 
exec xp_cmdshell @cmd
go


select * from pay
最后修改:2024 年 12 月 08 日
如果觉得我的文章对你有用,请随意赞赏