Practice: mysql collects statistics on the data and index size of all tables in the specified architecture.

Source: Internet
Author: User

# Count the data and index size of all tables in the specified Architecture
# Tablesize. sh
#! /Bin/sh
# Ocpyang@126.com

If ["$ #"-gt 2]; then
Echo "**********************************"
Echo "too wrote 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 when policyymm1_d1_h1_m1_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}"-! -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.