Here is a simple script for optimizing MySQL table. You will need to set the username and password for a user with "admin" like privileges.

#!/usr/bin/perl
use DBI;

$dbi_host="localhost";

# all databases
@dbi_u=("admin_user");
@dbi_p=("qwerty");

#set the max count value
$ending_value = scalar(_at_dbi_u);

#loop throuogh each of the above databases
for($counter=0 ; $counter < $ending_value ; $counter++){
        # connect to data input table
        $dbh_in = DBI->connect("dbi:mysql:host=$dbi_host", $dbi_u[$counter], $dbi_p[$counter], { RaiseError => 0, AutoCommit => 1 }) or die "cannot connect to server" . $DBI::errstr;
        $sth_dbs = $dbh_in -> prepare("SHOW DATABASES");
        $sth_dbs -> execute();
       
        for($loop_db=0; $loop_db<$sth_dbs -> rows; $loop_db++){
                @row = $sth_dbs -> fetchrow_array;
       
                # databases with a '-' in their name are not handled very well
                if (index($row[0], '-') == -1){
                        # gen query
                        $sth = $dbh_in -> prepare("SHOW TABLES FROM ".$row[0]);
                        $sth -> execute();
                        $query = "";
       
                        for($loop_t=0; $loop_t<$sth -> rows; $loop_t++) {
                                @row2 = $sth -> fetchrow_array;
       
                                if ($query eq ""){
                                        $query = "OPTIMIZE TABLE ";
                                }else{
                                        $query .= ", ";
                                }
       
                                $query .= $row[0].".".$row2[0];
                        }
                       
                        # skip databases without tables
                        if ($query ne ""){
                                print "Optimizing ".$row[0]."...\n";
                                $sth = $dbh_in -> prepare($query);
                                $sth -> execute();
                        }
                }
        }
}
 

All it does it loop through each database and optimise the tables. I would suggest a simple weekly or monthly cron job to execute the script. The script though will e-mail the cron user a list of optimised databases - simply add " >/dev/null 2>&1" to the end of the crontab line to send the data to nowhere.

pdavies | MySQL | 18 June, 1:03am
Trackback URI: http://www.dot-commers.co.uk/index.php/trackback/4
Titanpoker repg: Titanpoker / 13 November, 1:37pm
Titanpoker
europa casino etog: europa casino / 3 January, 2:08pm
europa casino
فوركس أونلاين svkh: فوركس أونلاين / 5 January, 10:19am
فوركس أونلاين
casino tropez cmof: casino tropez / 5 January, 11:41am
casino tropez
titan poker vtyk: titan poker / 5 January, 12:50pm
titan poker

Leave a Comment









Comment XML feeds: RSS | Atom

Category Cloud

Link Dump Tag Cloud