4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MySQLのインスタンス内全てのテーブルを最適化するshell

Posted at

概要

インスタンス内の全てのテーブルの最適化を図る

補足事項

innodb のみ

コード

# !/bin/bash

# --------------------------------------------------------
# MySQLのテーブルすべてに、OPTIMIZEを実施する Innodb Only
# --------------------------------------------------------

_DATE=`date +"%Y%m%d"`
_HOSTNAME=`hostname -s`

_START_DATE=`date +"%Y %m %d - %H %M %S"`
# DB Config
_DBHOST=hostname
_DBUSER=userid
_DBPASSWD=passwd
_TMPDIR=/tmp
mkdir -p ${_TMPDIR}/${_HOSTNAME}
_TMP=${_TMPDIR}/${_HOSTNAME}/OPTIMIZE_TABLE_${_HOSTNAME}_${_DATE}.sql
echo "-- ### MySQL OPTIMIZE START ${_START_DATE} ###" > ${_TMP}
mysql -N -h ${_DBHOST} -u ${_DBUSER} -p${_DBPASSWD}  << EOD >> ${_TMP}
select
  'OPTIMIZE TABLE ' ||
  TABLE_SCHEMA || 
  '.' || 
  TABLE_NAME ||
  ' ;'
from 
  INFORMATION_SCHEMA.TABLES
where
  TABLE_SCHEMA != 'mysql' and
  TABLE_SCHEMA != 'information_schema' and
  TABLE_SCHEMA != 'performance_schema' and
  TABLE_TYPE ='BASE TABLE' and
  ENGINE = 'innodb';
EOD

cat ${_TMP} | mysql -N -h ${_DBHOST} -u ${_DBUSER} -p${_DBPASSWD}

_END_DATE=`date +"%Y %m %d - %H %M %S"`

echo "-- ### MySQL OPTIMIZE END ${_END_DATE} ###" >> ${_TMP}
4
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?