HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Would a cron job to automatically optimize tables on a monthly basis be ideal?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
idealtablescronbasisautomaticallywouldmonthlyoptimizejob

Problem

I'm wondering if running the OPTIMIZE command via cronjob would be an intelligent move? Sometimes I forget to run optimize for months at a time.

My database is frequently using select, insert, update. It has about 45,000,000 rows.

Select uses the dateChecked column and update uses the URL column.

Solution

The compete and correct answer depends on which Mysql version you have and on which engine is the table.

Fast answer: no.

MyISAM

Short answer
Only if you can have some downtime and want to take some risks (see below).

Long answer
During the optimize, the table becomes I inaccessible, and if you need to use that table (selects, inserts, etc), then it's not a good idea to perform an scheduled downtime.

InnoDB

Short answer
If you have mysql 5.6.17 or above, than you may do it without downtime, but I wouldn't recommend it.

Long answer
Since mysql 5.6.17 that InnoDB can perform an optimize table with Online DDL, which means that selects, and even inserts, deletes and updates can be performed on the table while the optimize is running, if that is your case.
With that you can schedule an optimize on the table knowing that your system continues to run.

But it may not be safe to do it like that, as things may go wrong, and you're not with your eyes on the database to fix it ASAP. For example, you may not have free disk space for the optimize to run.

For 5.6.16 or lower, applies what I've said about myISAM.

Conclusion

Optimize table may lock your database users from accessing the database, if your mysql version doesn't support online ddl. And even if it supports, performing an operation like that automatically needs to be done with extreme care.

Maybe create a cron that sends you an email to remember to do the optimize?

Context

StackExchange Database Administrators Q#129351, answer score: 4

Revisions (0)

No revisions yet.