AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 63755
Accepted
U01SFA3
U01SFA3
Asked: 2014-04-24 08:37:50 +0800 CST2014-04-24 08:37:50 +0800 CST 2014-04-24 08:37:50 +0800 CST

庞大的 MSDB 数据库

  • 772

我有一个非常大的 MSDB 数据库,并且一直在努力清除它并设置维护任务——这是我的前任几年前应该做的事情!

到目前为止,我已经设法截断了sysmaintplan_logdetail和sysmaintplan_log文件。

现在我希望查看一些备份数据库——备份文件表超过 5Gb。

我尝试使用 SP sp_delete_backuphistory但这填满了我的 MSDB 事务日志并几乎杀死了服务器......

有没有其他方法可以清除它并缩小文件而不造成任何损坏?

有关信息,我的 MSDB 数据文件仍然是 20Gb,使用的是 13Gb。登录500Mb,用了6Mb。

任何帮助将不胜感激 - 我不是 DBA!

disk-space truncate
  • 1 1 个回答
  • 3896 Views

1 个回答

  • Voted
  1. Best Answer
    Kin Shah
    2014-04-24T09:06:46+08:002014-04-24T09:06:46+08:00

    我尝试使用 SP sp_delete_backuphistory 但这填满了我的 MSDB 事务日志并几乎杀死了服务器......

    有没有其他方法可以清除它并缩小文件而不造成任何损坏?

    分批进行。

    如果你的 msdb 很大,那么在运行脚本后,为了释放未使用的空间,我建议你缩小你的 msdb(是的,缩小它,然后一旦空间被释放,你可以使用 OLA 的脚本进行重组/重建和更新统计信息-这种收缩将是一次性的事情- 这样您就可以释放未使用的空间)。

    确保根据服务器的繁忙程度安排以下脚本频繁运行(就作业频率而言,日志传送是否正在运行以及数据库邮件的使用+服务器上发生的备份和恢复为他们都登录到MSDB)

    use msdb
    /* Author : Kin
       Purpose: For dba.stackexchange.com - Trim down msdb
    */
    
    -- Declaration
    DECLARE @DeleteDate datetime 
    DECLARE @DaysToRetain int
    DECLARE @Batch int
    
    set @DaysToRetain = 30
    set @Batch = 5000
    set @DeleteDate = convert(datetime,convert(varchar,getdate()-@DaysToRetain,101),101) 
    
    
    -- ----------------
    --  Index creation
    -- ----------------
    --  backupset
    -- ----------------
    Print 'Index Creation..'
    
    if not exists (select * from msdb..sysindexes where name = 'IX_backupset_backup_set_uuid')
    begin   
        Create NONCLUSTERED index IX_backupset_backup_set_uuid on backupset(backup_set_uuid)
    end 
    
    if not exists (select * from msdb..sysindexes where name = 'IX_backupset_media_set_id')
    begin   
        Create NONCLUSTERED index IX_backupset_media_set_id on backupset(media_set_id)
    end
    
    if not exists (select * from msdb..sysindexes where name = 'IX_backupset_backup_finish_date')begin  
        Create NONCLUSTERED index IX_backupset_backup_finish_date on backupset(backup_finish_date)
    end 
    
    if not exists (select * from msdb..sysindexes where name = 'IX_backupset_backup_start_date')    
    begin
        Create NONCLUSTERED index IX_backupset_backup_start_date on backupset(backup_start_date)
    end
    
    
    -- ------------
    --  backupfile
    -- ------------
    if not exists (select * from msdb..sysindexes where name = 'IX_backupfile_backup_set_id')    
    begin
        Create NONCLUSTERED index IX_backupfile_backup_set_id on backupfile(backup_set_id)
    end
    
    -- -------------------
    --  backupmediafamily
    -- -------------------
    if not exists (select * from msdb..sysindexes where name = 'IX_backupmediafamily_media_set_id')    
    begin
        Create NONCLUSTERED index IX_backupmediafamily_media_set_id on backupmediafamily(media_set_id)
    end
    
    -- -------------------
    --  backupfilegroup
    -- -------------------
    if not exists (select * from msdb..sysindexes where name = 'IX_backupfilegroup_backup_set_id')    
    begin
        Create NONCLUSTERED index IX_backupfilegroup_backup_set_id on backupfilegroup(backup_set_id)
    end
    
    -- ----------------
    --  restorehistory
    -- ----------------
    if not exists (select * from msdb..sysindexes where name = 'IX_restorehistory_restore_history_id')    
    begin
        Create NONCLUSTERED index IX_restorehistory_restore_history_id on restorehistory(restore_history_id)
    end
    
    if not exists (select * from msdb..sysindexes where name = 'IX_restorehistory_backup_set_id')    
    begin
        Create NONCLUSTERED index IX_restorehistory_backup_set_id on restorehistory(backup_set_id)
    end
    
    -- -------------
    --  restorefile
    -- -------------
    if not exists (select * from msdb..sysindexes where name = 'IX_restorefile_restore_history_id')    
    begin
        Create NONCLUSTERED index IX_restorefile_restore_history_id on restorefile(restore_history_id)
    end
    
    -- ------------------
    --  restorefilegroup
    -- ------------------
    if not exists (select * from msdb..sysindexes where name = 'IX_restorefilegroup_restore_history_id')    
    begin
        Create NONCLUSTERED index IX_restorefilegroup_restore_history_id on restorefilegroup(restore_history_id)
    end
    
    Print 'End of Index Creation..'
    
    
    -- ------------------------------
    --  Maintenance  before deletion
    -- ------------------------------
    --  Reindex 
    -- ------------------------------
    
    -- ----------------
    --  backupset
    -- ----------------
    Print 'Maintenance Reindex..'
    
    ALTER INDEX [IX_backupset_backup_set_uuid] ON [msdb].[dbo].[backupset] REORGANIZE WITH ( LOB_COMPACTION = OFF)
    WAITFOR DELAY '00:00:05'
    print 'IX_backupset_backup_set_uuid..'
    
    ALTER INDEX [IX_backupset_media_set_id] ON [msdb].[dbo].[backupset] REORGANIZE WITH ( LOB_COMPACTION = OFF)
    WAITFOR DELAY '00:00:05'
    print 'IX_backupset_media_set_id..'
    
    
    ALTER INDEX [IX_backupset_backup_finish_date] ON [msdb].[dbo].[backupset] REORGANIZE WITH ( LOB_COMPACTION = OFF)
    WAITFOR DELAY '00:00:05'
    print 'IX_backupset_backup_finish_date..'
    
    ALTER INDEX [IX_backupset_backup_start_date] ON [msdb].[dbo].[backupset] REORGANIZE WITH ( LOB_COMPACTION = OFF)
    WAITFOR DELAY '00:00:05'
    print 'IX_backupset_backup_start_date..'
    
    
    -- ------------
    --  backupfile
    -- ------------
    ALTER INDEX [IX_backupfile_backup_set_id] ON [msdb].[dbo].[backupfile] REORGANIZE WITH ( LOB_COMPACTION = OFF)
    WAITFOR DELAY '00:00:05'
    print 'IX_backupfile_backup_set_id..'
    
    -- -------------------
    --  backupmediafamily
    -- -------------------
    ALTER INDEX [IX_backupmediafamily_media_set_id] ON [msdb].[dbo].[backupmediafamily] REORGANIZE WITH ( LOB_COMPACTION = OFF)
    WAITFOR DELAY '00:00:05'
    print 'IX_backupmediafamily_media_set_id..'
    
    -- ------------------
    --  backupfilegroup
    -- ------------------
    ALTER INDEX [IX_backupfilegroup_backup_set_id] ON [msdb].[dbo].[backupfilegroup] REORGANIZE WITH ( LOB_COMPACTION = OFF)
    WAITFOR DELAY '00:00:05'
    print '[IX_backupfilegroup_backup_set_id]..'
    
    -- ----------------
    --  restorehistory
    -- ----------------
    ALTER INDEX [IX_restorehistory_restore_history_id] ON [msdb].[dbo].[restorehistory] REORGANIZE WITH ( LOB_COMPACTION = OFF)
    WAITFOR DELAY '00:00:05'
    print 'IX_restorehistory_restore_history_id..'
    
    
    ALTER INDEX [IX_restorehistory_backup_set_id] ON [msdb].[dbo].[restorehistory] REORGANIZE WITH ( LOB_COMPACTION = OFF)
    WAITFOR DELAY '00:00:05'
    print 'IX_restorehistory_backup_set_id..'
    
    -- -------------
    --  restorefile
    -- -------------
    ALTER INDEX [IX_restorefile_restore_history_id] ON [msdb].[dbo].[restorefile] REORGANIZE WITH ( LOB_COMPACTION = OFF)
    WAITFOR DELAY '00:00:05'
    print 'IX_restorefile_restore_history_id..'
    
    
    -- ------------------
    --  restorefilegroup
    -- ------------------
    ALTER INDEX [IX_restorefilegroup_restore_history_id] ON [msdb].[dbo].[restorefilegroup] REORGANIZE WITH ( LOB_COMPACTION = OFF)
    WAITFOR DELAY '00:00:05'
    print 'IX_restorefilegroup_restore_history_id..'
    
    Print 'End of Maintenance Reindex..'
    
    
    
    --delete records    
    
    print 'DEL restorefile..' 
    if exists (select * from msdb.dbo.sysobjects where name = 'restorefile')    
    begin 
        
        DELETE top (@Batch) FROM msdb..restorefile
        FROM msdb..restorefile rf with (nolock)
            INNER JOIN msdb..restorehistory rh with (nolock) ON rf.restore_history_id = rh.restore_history_id
            INNER JOIN msdb..backupset bs with (nolock) ON rh.backup_set_id = bs.backup_set_id
        WHERE bs.backup_finish_date < @DeleteDate 
        
        
        while @@rowcount <> 0    
        begin    
        
            DELETE top (@Batch) FROM msdb..restorefile
            FROM msdb..restorefile rf with (nolock)
                INNER JOIN msdb..restorehistory rh with (nolock) ON rf.restore_history_id = rh.restore_history_id
                INNER JOIN msdb..backupset bs with (nolock) ON rh.backup_set_id = bs.backup_set_id
            WHERE bs.backup_finish_date < @DeleteDate 
            
        end    
    end    
    
    
    
    print 'DEL restorefilegroup..'  
    if exists (select * from msdb.dbo.sysobjects where name = 'restorefilegroup')    
    begin 
        
        DELETE top (@Batch) FROM msdb..restorefilegroup
        FROM msdb..restorefilegroup rfg with (nolock)
            INNER JOIN msdb..restorehistory rh with (nolock) ON rfg.restore_history_id = rh.restore_history_id
            INNER JOIN msdb..backupset bs with (nolock) ON rh.backup_set_id = bs.backup_set_id
        WHERE bs.backup_finish_date  < @DeleteDate 
    
        
        while @@rowcount <> 0    
        begin    
        
            DELETE top (@Batch) FROM msdb..restorefilegroup
            FROM msdb..restorefilegroup rfg with (nolock)
                INNER JOIN msdb..restorehistory rh with (nolock) ON rfg.restore_history_id = rh.restore_history_id
                INNER JOIN msdb..backupset bs with (nolock) ON rh.backup_set_id = bs.backup_set_id
            WHERE bs.backup_finish_date  < @DeleteDate  
        End
    End 
        
        
        
    print 'Temp table ..'   
    SELECT media_set_id, backup_finish_date
        INTO #Temp 
    FROM msdb..backupset with (nolock)
    WHERE backup_finish_date  < @DeleteDate 
    
    
        
    
    
    print 'DEL backupfile..' 
    if exists (select * from msdb.dbo.sysobjects where name = 'backupfile')    
    begin 
        
        DELETE top (@Batch) FROM msdb..backupfile
        FROM msdb..backupfile bf with (nolock)
            INNER JOIN msdb..backupset bs with (nolock) ON bf.backup_set_id = bs.backup_set_id
            INNER JOIN #Temp t ON bs.media_set_id = t.media_set_id
        WHERE bs.backup_finish_date  < @DeleteDate 
        
        
        while @@rowcount <> 0    
        begin    
        
            DELETE top (@Batch) FROM msdb..backupfile
            FROM msdb..backupfile bf with (nolock)
                INNER JOIN msdb..backupset bs with (nolock) ON bf.backup_set_id = bs.backup_set_id
                INNER JOIN #Temp t ON bs.media_set_id = t.media_set_id
            WHERE bs.backup_finish_date  < @DeleteDate      
        End
    End 
    
    
    
    print 'DEL backupmediafamily..' 
    if exists (select * from msdb.dbo.sysobjects where name = 'backupmediafamily')    
    begin 
        
        DELETE top (@Batch) FROM msdb..backupmediafamily
        FROM msdb..backupmediafamily bmf with (nolock)
            INNER JOIN msdb..backupmediaset bms with (nolock) ON bmf.media_set_id = bms.media_set_id
            INNER JOIN #Temp t ON bms.media_set_id = t.media_set_id
        
        
        while @@rowcount <> 0    
        begin    
            
            DELETE top (@Batch) FROM msdb..backupmediafamily
            FROM msdb..backupmediafamily bmf with (nolock)
                INNER JOIN msdb..backupmediaset bms with (nolock) ON bmf.media_set_id = bms.media_set_id
                INNER JOIN #Temp t ON bms.media_set_id = t.media_set_id
        End
    End 
    
    
    
    print 'DEL backupfilegroup..' 
    if exists (select * from msdb.dbo.sysobjects where name = 'backupfilegroup')    
    begin 
        
        Delete top (@Batch) FROM msdb..backupfilegroup
            FROM msdb..backupfilegroup bfg with (nolock)
            INNER JOIN msdb..backupset bs with (nolock) ON bfg.backup_set_id = bs.backup_set_id
            INNER JOIN #Temp t ON bs.media_set_id = t.media_set_id
        
        
        while @@rowcount <> 0    
        begin    
        
            Delete top (@Batch) FROM msdb..backupfilegroup
            FROM msdb..backupfilegroup bfg with (nolock)
                INNER JOIN msdb..backupset bs with (nolock) ON bfg.backup_set_id = bs.backup_set_id
                INNER JOIN #Temp t ON bs.media_set_id = t.media_set_id
    
        End
    End 
            
        
    
    print 'DEL restorehistory..' 
    if exists (select * from msdb.dbo.sysobjects where name = 'restorehistory')    
    begin       
        DELETE top (@Batch) FROM msdb..restorehistory
        FROM msdb..restorehistory rh with (nolock)
            INNER JOIN msdb..backupset bs with (nolock) ON rh.backup_set_id = bs.backup_set_id
        WHERE bs.backup_finish_date  < @DeleteDate 
        
        
        while @@rowcount <> 0    
        begin    
        
            DELETE top (@Batch) FROM msdb..restorehistory
            FROM msdb..restorehistory rh with (nolock)
                INNER JOIN msdb..backupset bs with (nolock) ON rh.backup_set_id = bs.backup_set_id
            WHERE bs.backup_finish_date  < @DeleteDate 
        
        End
    End 
        
        
    print 'DEL backupset..' 
    if exists (select * from msdb.dbo.sysobjects where name = 'backupset')    
    begin   
    
        DELETE top (@Batch) FROM msdb..backupset
        FROM msdb..backupset bs with (nolock)
            INNER JOIN #Temp t ON bs.media_set_id = t.media_set_id
            
        while @@rowcount <> 0    
        begin    
        
            DELETE top (@Batch) FROM msdb..backupset
            FROM msdb..backupset bs with (nolock)
                INNER JOIN #Temp t ON bs.media_set_id = t.media_set_id
        
        End
    End 
        
    
    print 'DEL backupmediaset..' 
    if exists (select * from msdb.dbo.sysobjects where name = 'backupmediaset')    
    begin   
    
        DELETE top (@Batch) FROM msdb..backupmediaset
        FROM msdb..backupmediaset bms with (nolock)
            INNER JOIN #Temp t ON bms.media_set_id = t.media_set_id
            
        while @@rowcount <> 0    
        begin    
        
            DELETE top (@Batch) FROM msdb..backupmediaset
            FROM msdb..backupmediaset bms with (nolock)
                INNER JOIN #Temp t ON bms.media_set_id = t.media_set_id
        
        End
    End 
        
    
    DROP TABLE #Temp
    
    
    
    print 'Update Statistic on msdb'
    -- Updates the statistics for all tables in the database. 
    EXEC sp_updatestats
    go
    
    • 4

相关问题

  • 哪些因素会阻止和延迟 TRUNCATE 命令?

  • 了解块大小

  • PostgreSQL:查询全局表空间的位置?

  • 为什么一个表的数据空间可能会占用原始数据大小的 4 倍?

  • PostgreSQL:TRUNCATE 后未释放磁盘空间

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve