This script came from here. However, if you copy and paste it from that website you have to correct all the quotes and single ticks in the whole script. Hopefully my above paste makes it better.

For background on what this does, this script cleans up the sqllite "Envelope Index" database that Mail.app uses to store it's list of emails and attachments. If you clean this up, Mail.app starts and runs a TON faster. Check out more here.