tags:

views:

52

answers:

2

I've seen many different schedules for optimizing tables in MySQL: everything from immediately after a delete, to once a day, to never. I have often setup a cron job to run every night to optimize all the tables.

What factors do you use to determine how often a table should be optimized?

+2  A: 

Also this quote from MySQL themselves is good.

In most setups, you need not run OPTIMIZE TABLE at all. Even if you do a lot of updates to variable-length rows, it is not likely that you need to do this more than once a week or month and only on certain tables.

Based on this article on Table Optimization.

This has the effect of defragmenting the table and reducing the size of the table on disk down to 105MB. It also has a very positive affect on query performance, reducing the select query response time from 0.63 to 0.39 seconds. N.B. the mysql query cache was turned off to demonstrate.

Here's another quote from "MySQL administrators guide"

OPTIMIZE TABLE does a table repair and a key analysis, and also sorts the index tree so that key lookups are faster.

Just because this quote can never be told to often.

Premature optimization is the root of all evil

What you should do is measure before, do an optimization and then measure after. Then do the same thing the day after, if the increase in performance is minimal, then you should do the same after and see if you get a big boost in performance then.

Ólafur Waage
Sorry, but this isn't optimization in that way. This isn't changing a query to make it faster, but instead essentially defragging the database.
Darryl Hein
"In most setups"--what deteremines when it's part of "most setups"?
Darryl Hein
I know. An unoptimized database table has effect on all queries.
Ólafur Waage
@Ólafur your comment doesn't match what you are saying in your answer?
Darryl Hein
Sorry, english isn't my primary language. What i am saying is that an Unoptimized table runs all queries slower on it.
Ólafur Waage
Okay, I agree with you, but the first part of your answer says don't optimize and optimize later, not sooner. I'm also looking for general practices, not trial and error processes, which I am doing and have done.
Darryl Hein
It's referring to that trying to optimize without measuring before and after isn't optimizing at all. But just a change in the system.
Ólafur Waage
Nice edit :) Makes it clearer
Ólafur Waage
A: 

Other factors will most likely contribute more to query times than having run the optimize table.

If performance is an issue (and only if), then you should profile the application using the database start by optimizing the actual bottlenecks.

Ben S