views:

1594

answers:

2

Id like to run optimize on all currently framgmented tables. These tables should be those that have information_schema.DATA_FREE > 0.

Is it possible to optimize all tables with this property in one command in sql or will I have to write external code to do this?

A: 

You will have to write external code to do this.

Christian Vest Hansen
Sorry, but the answer below is much more useful, as it has the appropriate code to do this.
El Yobo
Turns out accepted answers cannot be deleted :/
Christian Vest Hansen
+5  A: 

You can do something like this:

SELECT concat("OPTIMIZE TABLE ", table_schema,".",table_name,";") FROM tables WHERE DATA_FREE > 0 INTO OUTFILE '/tmp/optimize.sql'; SOURCE '/tmp/optimize.sql';

Max Bube
Slight changes required to work for me, but great approach.SELECT concat("OPTIMIZE TABLE ", table_schema,".",table_name,";") FROM information_schema.tables WHERE DATA_FREE > 0 INTO OUTFILE '/tmp/optimize.sql'; SOURCE /tmp/optimize.sql;
El Yobo