There was a tip on Hawk Wings long ago which showed how to speed up Apple’s Mail.app by vacuuming the SQLite3 index. I hadn’t tried it since Snow Leopard, but I was having some serious issues with the time it took to open a Mail folder on my Air, so I thought I’d give it a shot.

The same trick still works, you just have to modify the path. The new command (in Mountain Lion, and probably Lion) is:

sqlite3 ~/Library/Mail/V2/MailData/Envelope\ Index vacuum;

Give it a shot and see if performance improves. If it does, you can automate this with a bash function or AppleScript. Here’s a handy AppleScript from Hawk Wings modified to work with the new path:

speedmail.applescriptraw
"
(*
Speed up Mail.app by vacuuming the Envelope Index
Code from: http://www.hawkwings.net/2007/03/03/scripts-to-automate-the-mailapp-envelope-speed-trick/
Originally by "pmbuko" with modifications by Romulo
Updated by Brett Terpstra 2012
Updated by Mathias Törnblom 2015 to support V3 in El Capitan and still keep backwards compability
Updated by @lbutlr for V5 and Container folder in High Sierra and use du
*)

tell application "Mail" to quit
set os_version to do shell script "sw_vers -productVersion"
set mail_version to "V2"
considering numeric strings
	if "10.10"  os_version then set mail_version to "~/Library/Mail/V3/Maildata/Envelope\\ Index"
	if "10.12" < os_version then set mail_version to "~/Library/Mail/V4/Maildata/Envelope\\ Index"
	if "10.13"  os_version then set mail_version to "~/Library/Containers/com.apple.mail/Data/Library/Mail/V5/Maildata/Envelope\\ Index"
end considering

set sizeBefore to do shell script "du -h  " & mail_version & "|  awk {'print $1'}"
do shell script "/usr/bin/sqlite3 " & mail_version & " vacuum"
set sizeAfter to do shell script "du -h " & mail_version & "| awk {'print $1'}"
display dialog ("Mail index before: " & sizeBefore & return & "Mail index after: " & sizeAfter & return & return & "Enjoy the new speed!")
tell application "Mail" to activate

Open AppleScript Editor, paste that in and save it as a “scpt” file in “~/Library/Scripts/” (“~” is your home folder, create the “Scripts” folder if it’s not there1). Run it using the script menu (if you have it enabled in AppleScript Editor preferences) or directly from AppleScript Editor. You could also have it run automatically in the middle of the night using a scheduler like Lingon and osascript to run the AppleScript file.

You usually won’t see a huge difference in the database size, but you’ll definitely notice the improvement in Mail’s performance.

  1. If you can’t find your ~/Library folder while you’re in the Save dialog, use G to open the “Go to folder” dialog and enter “~/Library” and hit enter. It will take you to the (hidden) Library folder where you can look for the Scripts folder, creating it with N if it doesn’t exist.