MySQL OPTIMIZE all tables?

前端 未结 14 2105
迷失自我
迷失自我 2020-12-04 04:57

MySQL has an OPTIMIZE TABLE command which can be used to reclaim unused space in a MySQL install. Is there a way (built-in command or common stored procedure) to run this o

14条回答
  •  爱一瞬间的悲伤
    2020-12-04 05:13

    This bash script will accept the root password as option and optimize it one by one, with status output:

    #!/bin/bash
    
    if [ -z "$1" ] ; then
      echo
      echo "ERROR: root password Parameter missing."
      exit
    fi
    MYSQL_USER=root
    MYSQL_PASS=$1
    MYSQL_CONN="-u${MYSQL_USER} -p${MYSQL_PASS}"
    TBLLIST=""
    COMMA=""
    SQL="SELECT CONCAT(table_schema,'.',table_name) FROM information_schema.tables WHERE"
    SQL="${SQL} table_schema NOT IN ('information_schema','mysql','performance_schema')"
    for DBTB in `mysql ${MYSQL_CONN} -ANe"${SQL}"`
    do
        echo OPTIMIZE TABLE "${DBTB};"
        SQL="OPTIMIZE TABLE ${DBTB};"
        mysql ${MYSQL_CONN} -ANe"${SQL}"
    done
    

提交回复
热议问题