SET FOREIGN_KEY_CHECKS = 0 ; SELECT concat( 'ALTER TABLE ' , table_name, ' OPTIMIZE TABLE;' ) FROM information_schema.tables WHERE table_schema = database(); SELECT concat( 'ALTER TABLE ' , table_name, ' REPAIR TABLE;' ) FROM information_schema.tables WHERE table_schema = database(); SET FOREIGN_KEY_CHECKS = 1 ;