首页 > 数据库 > mysql教程 > 实战:mysql统计指定架构的所有表的数据和索引大小情况_MySQL

实战:mysql统计指定架构的所有表的数据和索引大小情况_MySQL

WBOY
发布: 2016-06-01 13:08:30
原创
838 人浏览过

#统计指定架构的所有表的数据和索引大小情况
#tablesize.sh
#!/bin/sh
#ocpyang@126.com

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

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板