Here is a simple script for optimizing MySQL table. You will need to set the username and password for a user with "admin" like privileges.
All it does it loop through each database and optimise the tables. I would suggest a simple weekly or monthly cron job to execute the script. The script though will e-mail the cron user a list of optimised databases - simply add
>/dev/null 2>&1 to the end of the crontab line to send the data to nowhere.