Fix Slow, Bloated MSDB: Purge Old History And Add Missing Indexes

After tempdb, msdb is often the most abused system database, growing unchecked until it tanks your backup reporting and job monitoring.

I’ve watched MSDB performance degrade across multiple SQL Server instances. It’s not optimized out of the box and doesn’t get much care, so as it balloons to 100GB+, metadata queries crawl, showing up as top offenders in Activity Monitor.

Another indicator of MSDB performance problems: missing indexes in MSDB showing at the top of the missing indexes DMV results.

In the past, I’d just find and add missing indexes. But MSDB tuning gets often gets overlooked even by the best of us. 


I have my own reasons to keep MSDB lean and fast:

  • I regularly query MSDB to get job status, backup history, and missing backups (manual + automated)
  • Our enterprise backup tool frequently queries the backup history tables, about every 5 minutes. These queries have become resource-intensive and slow, significantly hindering performance on several SQL Servers. Yes, imagine that, your backup tool spends more time querying backup meta data than time it takes to do transaction log backups.
  • Bloated MSDBs (50-100GB). While this may concern some, it doesn’t bother me per se..

Solution: 1) Index MSDB tables, 2) Purge old history, 3) Configure job history retention.

The missing index DMVs make the first step easy. 

Here’s what they recommend on one of my production servers:

Most out-of-the-box MSDBs would usually show 3-5 missing index recommendations. 


Manage MSDB Size Through Purging

I’ve written a SQL script that complements the indexing strategy by regularly purging old history records from MSDB. It uses SQL Server’s built-in system procedures for the heavy lifting, all you specify is @DaysRetention, and older records are automatically removed from key history tables. View/download it from GitHub:

Purge_MSDB_History_Cleanup.sql


Configure job history retention

You can configure automated purging of job history tables through the SSMS: SQL Server Agent ? Right-click ? Properties.

AVvXsEi84J5nH0uQ4QT3Fl7xixwj00HaumbL 0ezRNbc5oopxSoHEmFJuD eX97XDxU i9wAruuqhlsUGT6hqyJ1xDl6 oTWCC8F7TbKzov7SxnUYsobbfxgGjuuhbFQoUitbOrwLxZCWgtMJCkJMexTQIEY2kx9VGa8vAzXTuHt3DLF7lLi4JqdBctJa2cBhfiF=w554 h473

Go to the History page and tweak the values per your needs:

AVvXsEj5xhR06gdQk19LsLeVRh5Z9GIY0PP668B6RcZ25nw580XVz9F4FmGXi7tneE5Fk35rD88W4SUZGpeaSNXdGJroJhB1UzKBhX5lojmWHXs60RTzW Bepgwuz93YtXqQAq02q PEGizgdmabeMWkh7pBxu4Uv4XyUYha 0h7TCCELxkkVZau3Cg1KuLuiOGu=w569 h276

Click OK button at the right-bottom corner to save the changes.

That’s it. Add indexes + regular history purging + job retention settings. These three steps transformed my 100GB monsters into snappy, responsive MSDB database..

Questions, comments or tweaks? Drop them in the comments!

Share.
Leave A Reply