實戰:mysql統計指定架構的所有表的資料和索引大小情況-v2

來源:互聯網
上載者:User

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的使用者名稱
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

 

 

 

 

 

 

 

 

 

 

 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.