24 Oct 2017 12:27 | macOS
Recently I had a recurring problem where I would see mdworker running at high
CPU every 5 minutes or so for no apparent reason. Internet searches reveal loads
of people with the same problem and lots of witchcrafty ways to try to resolve
it that often don't work.
This is how I fixed it. The problem seems to occur because *something* is
writing a bunch of files to the disk and spotlight is then having to index them.
I use Spotlight for a very small number of use cases - launching applications
and searching for documents, that's it. Therefore I don't actually need
spotlight to index the entire disk, just my /Applications/ and iCloud documents
folders is sufficient.
First, disable all indexing:
$ sudo mdutil -i off /
then trash any partial indexes you've already got:
$ sudo rm -rf /.Spotlight-V100/
Now go to System Preferences -> Spotlight and go to the Privacy tab. As a
general rule of thumb you should put your Volumes directory in here, otherwise
spotlight will try to index any external drive you attach which is probably not
what you want. This is especially true if you're using Carbon Copy Cloner or
Superduper to clone your root volume, as then you'll end up launching apps from
the backup disk and all kinds of weird shit will happen.
Make sure finder is set to show hidden files:
$ defaults write com.apple.finder AppleShowAllFiles -boolean true
$ killall Finder
Now open finder at the root of your system volume:
$ open /
Then drag all the folders from it except Applications into the privacy tab of
the spotlight preferences. You'll get some errors if you have symlinks in the
root, don't worry about them just continue. Then turn indexing back on with:
$ sudo mdiutil -i on /
Now watch top and wait for it to index /Applications/. After 20-30 minutes at
most for an SSD-backed system it should have finished and settled down.
At this point you've fixed the load issue but you've only got /Applications/
indexed, maybe you want your cloud documents too or some other folders.
The trick is to add them one by one and watch. This is a bit tricky because you
can only add folders to exclude rather than include. We've currently got "Users"
excluded so your entire home directory won't currently be indexed.
Go to your ~/Library/ directory (in Finder click Go from the top menubar and
click Library). Drag all of the directories *except* "Mobile Documents" into the
Privacy tab of the Spotlight preferences.
As an extra precaution also go into your home directory and drag any folders
in that you don't care about indexing, especially if you know they have frequent
changes.
Once you've done this you can remove "Users" from the Privacy tab of the
Spotlight settings and mds will fire up again and start indexing the stuff that
is no longer excluded, which should be your cloud documents directory and not
much else. On my system with around ~20GB of cloud docs this took under half an
hour to complete.
If you want to index other directories my advice is to remove things from
exclusions one at a time and wait for it to completely settle down before
removing anything else, this will let you determine if something is causing a
problem with the indexing and then investigate why.
Now you should have a very carefully tuned spotlight system that only indexes
the folders you actually care about being able to search and nothing else. Apps
that start blasting data into random places in your home directory should no
longer cause high mdworker cpu usage.