Actual combat: MySQL statistics data and index size for all tables in the specified schema

Source: Internet
Author: User

#统计指定架构的所有表的数据和索引大小情况
#tablesize. Sh
#!/bin/sh
#[email protected]

If ["$#"-GT 2];then
echo "**********************************"
echo "Too many input parameters"
echo "**********************************"
echo "USAGE01: $ schema_name table_name"
echo "EG01: $ wind T1"
echo "USAGE02: $ schema_name"
echo "EG02: $ 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.