程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> 實戰:mysql統計指定架構的所有表的數據和索引大小情況

實戰:mysql統計指定架構的所有表的數據和索引大小情況

編輯:MySQL綜合教程

#統計指定架構的所有表的數據和索引大小情況
#tablesize.sh
#!/bin/sh
#[email protected]

if [ "$#" -gt 2 ];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

source /usr/local/mysql/scripts/mysql_env.ini
logfiledate=tmp.`date +%Y%m%d%H%M%S`.txt


SCHEMA_NAME=$1
TABLE_NAME=$2

if [ "$#" -eq 2 ];then
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;"
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;"
fi


SCHEMA_JUDEGE="select * from information_schema.schemata where schema_name='${SCHEMA_NAME}';"
mysql -h${MYSQL_HOST} -u${MYSQL_USER} -p${MYSQL_PASS} -e"${SCHEMA_JUDEGE}" >${logfiledate}

if [ -e "${logfiledate}" -a ! -s "${logfiledate}" ];then
echo "you input ${SCHEMA_NAME} not exits,pleae check your databases"
rm -rf ${logfiledate}
else
echo "the result is :"
mysql -h${MYSQL_HOST} -u${MYSQL_USER} -p${MYSQL_PASS} -e"${SQL_CMD}"

rm -rf ${logfiledate}
fi

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved