实战:mysql统计指定架构的全部表的数据和索引大小情况-v2

时间:2022-09-08 17:10:15

PS:第一个版本号里未做输入的schema_name和table_name推断,改动了一下!再次share!

#统计指定架构的全部表的数据和索引大小情况

#tablesize.sh

#!/bin/sh

#ocpyang@126.com

if [ "$#" -gt 2 -o "$#" -lt 1 ];then

echo "**********************************"

echo "too many input parameters"

echo "**********************************"

echo "USAGE01: $0 schema_name table_name"

echo "eg01: $0 wind t1"

echo "USAGE02: $0 schema_name "

echo "eg02: $0 wind "

exit 1;

fi

#set mysql evn

MYSQL_USER=system  #mysql的username

MYSQL_PASS='password'  #mysql的登录用户密码

MYSQL_HOST=192.168.2.188

judegedate_01="judegedate01.`date +%Y%m%d%H%M%S`.txt"

judegedate_02="judegedate02.`date +%Y%m%d%H%M%S`.txt"

SCHEMA_NAME=$1

TABLE_NAME=$2

#judege

SCHEMA_JUDEGE="select schema_name from information_schema.schemata where schema_name='${SCHEMA_NAME}';"

mysql -h${MYSQL_HOST} -u${MYSQL_USER} -p${MYSQL_PASS} -e"${SCHEMA_JUDEGE}" >${judegedate_01}

TABLE_JUDEGE="select table_name from information_schema.tables where table_name='${TABLE_NAME}';"

mysql -h${MYSQL_HOST} -u${MYSQL_USER} -p${MYSQL_PASS} -e"${TABLE_JUDEGE}" >${judegedate_02}

if [ "$#" -eq 2 ];then

 if [ ! -s "${judegedate_01}" ];then

 echo "****************************************************************************"

 echo "you input schema_name ${SCHEMA_NAME} not exits,pleae check your databases"

 echo "*****************************************************************************"

 rm -rf ${judegedate_01}

 rm -rf ${judegedate_02}

 exit 0

 fi

 if [ ! -s "${judegedate_02}" ];then

 echo "*****************************************************************************"

 echo "you input table_name ${TABLE_NAME} not exits,pleae check your databases"

 echo "*****************************************************************************"

 rm -rf ${judegedate_01}

 rm -rf ${judegedate_02}

 exit 0

 fi

SQL_CMD="select table_schema, table_name,table_rows,

 round(sum(data_length+index_length)/1024/1024) as total_MB,

 round(sum(data_length)/1024/1024) as data_MB,

 round(sum(index_length)/1024/1024) as index_MB

 from information_schema.tables  where table_type='BASE TABLE' and table_schema='${SCHEMA_NAME}'

 and table_name='${TABLE_NAME}'

 group by table_schema, table_name,table_rows;"

 echo "the result is :"

 mysql -h${MYSQL_HOST} -u${MYSQL_USER} -p${MYSQL_PASS} -e"${SQL_CMD}"

 rm -rf ${judegedate_01}

 rm -rf ${judegedate_02}

else 

 if [ ! -s "${judegedate_01}" ];then

 echo "*****************************************************************************"

 echo "you input schema_name ${SCHEMA_NAME} not exits,pleae check your databases"

 echo "*****************************************************************************"

 rm -rf ${judegedate_01}

 rm -rf ${judegedate_02}

 exit 0

 else

 SQL_CMD="select table_schema, table_name,table_rows,

 round(sum(data_length+index_length)/1024/1024) as total_MB,

 round(sum(data_length)/1024/1024) as data_MB,

 round(sum(index_length)/1024/1024) as index_MB

 from information_schema.tables  where table_type='BASE TABLE' and table_schema='${SCHEMA_NAME}'

 group by table_schema, table_name,table_rows;"

 echo "the result is :"

 mysql -h${MYSQL_HOST} -u${MYSQL_USER} -p${MYSQL_PASS} -e"${SQL_CMD}"

 rm -rf ${judegedate_01}

 rm -rf ${judegedate_02}

 fi

fi