#!/usr/bin/perl # Author: Peter R. Wood, http://prwdot.org/ use strict; use DBI; my $default_database = ''; my $database = $ARGV[0] || $default_database; my $db_host = ''; my $db_user = ''; my $db_pass = ''; my $logfile = ""; logit( "-" x 32 ); logit("Starting run."); # Connect to the database my $dbh = DBI->connect( "DBI:mysql:database=$database;host=$db_host", $db_user, $db_pass, { RaiseError => 1 } ); my $table_fetch = "show tables"; my $table_sth = $dbh->prepare($table_fetch); $table_sth->execute; while ( my ($table) = $table_sth->fetchrow ) { my $command = "analyze table $table"; my $sth = $dbh->prepare($command); $sth->execute(); my ( $table_name, $operation, $type, $text ) = $sth->fetchrow; logit("$operation $table_name $type: $text"); $command = "optimize table $table"; $sth = $dbh->prepare($command); $sth->execute(); ( $table_name, $operation, $type, $text ) = $sth->fetchrow; logit("$operation $table_name $type: $text"); $sth->finish; } $table_sth->finish; $dbh->disconnect; logit("Finished."); sub logit { my ($message) = @_; my $timestamp = scalar( localtime( time() ) ); open( LOG, ">>$logfile" ); print LOG "[$timestamp] $message\n"; }