In the 2.9 and later releases, WordPress has added automatic database optimization supports. This feature enables you to repair and optimize your database tables.
You can use this feature even when you’re not logged in to your WordPress. This is because its main intent is to repair the database tables, and sometimes your database may become so corrupt that you can’t login to your WordPress.
Just add following snippet to your wp-config.php file to activate this feature:
define( 'WP_ALLOW_REPAIR', true );
Once activated, you can find the script it uses at: http://www.yoursite.com/wp-admin/maint/repair.php
Once you are done repairing and optimizing your database, make sure to remove this from your wp-config.php for safety purposes.