Dashboard > HOW-TOS > Home > optimize mysql tables
HOW-TOS Log In   View a printable version of the current page.
optimize mysql tables
Added by Matt Rae, last edited by Matt Rae on Aug 18, 2006  (view change)
Labels: 
(None)

#################################################
#!/bin/sh

# this shell script finds tables and runs 'optimize table'
# @date 6/14/2006
# @author Son Nguyen
# @modified by Matt Rae 7/7/2006

printUsage() {
    echo "Usage: $0"
    echo " --optimize"
    return
}


doAllTables() {
    DBNAMES=`mysql -e "SHOW DATABASES\G;" | grep 'Database' | sed -n 's/Database: //p'`
    for DBNAME in $DBNAMES
    do
        # get the table names
        TABLENAMES=`mysql -D $DBNAME -e "SHOW TABLES\G;"|grep 'Tables_in_'|sed -n 's/.*Tables_in_.*: \([_0-9A-Za-z]*\).*/\1/p'`

        # loop through the tables and optimize them
        for TABLENAME in $TABLENAMES
        do
            mysql -D $DBNAME -e "OPTIMIZE TABLE $TABLENAME;"
        done
    done
}

if [ $# -eq 0 ] ; then
    printUsage
    exit 1
fi

case $1 in
    --optimize) doAllTables;;
    --help) printUsage; exit 1;;
    *) printUsage; exit 1;;
esac

0 comments
Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.7 Build:#524 Jul 28, 2006) - Bug/feature request - Contact Administrators