1、手残方法
自己写脚本,不会利用元数据表和只会简单sql的解决办法:
# cat loop_db_tables.sh
#!/bin/bashdbnames=`mysql -uroot -ppassword -e "show databases;"`
for dbname in $dbnames
do# 由于是借用了shell,所以需要处理输出文本"Database",以及不需要查询的元数据库和测试库。if [ $dbname == "Database" -o $dbname == "information_schema" -o $dbname == "mysql" -o $dbname == "sys" -o $dbname == "test" -o $dbname == "apollo" ]thenecho $dbname;elsetbls=`mysql -uroot -ppassword -S /export/servers/data/my3306/run/mysqld.sock -e "use $dbname; show tables"`for table in $tblsdo# 同理 ,这里的"Tables_in_apolloconfigdb"也是shell输出文本if [ $table != "Tables_in_apolloconfigdb" ];thentbl_status=`mysql -uroot -ppassword -S /export/servers/data/my3306/run/mysqld.sock -e "use $dbname; show table status like '$table';"`#根据 show table status like "table_name"的结果过滤data_length=`echo $tbl_status | awk '{print $25}'`# 数据长度转换成KB,默认是Bytedata_length_kb=$((data_length/1024))index_length=`echo $tbl_status | awk '{print $27}'`# 打印结果echo "=====DB Name: "$dbname " -----Table Name: "$table " ======Data Length: "$data_length "--+-+-+-+--"$index_lengthecho $dbname $table $data_length_kbfidonefi
done
哈哈哈,看完自己写的脚本,想哭的心都有,脚本搞完,还要用文本处理器处理一下,才能用。
会python的可以直接写进excel表里面,比shell省事儿。
2、干爽方法
直接上sql:
SELECT COALESCE ( bb.表名, '汇总' ) AS "表名", bb.记录数, bb.数据容量(MB), bb.索引容量(MB) FROM (SELECT aa.表名, sum( AA.记录数 ) AS "记录数", sum( AA.数据容量(MB) ) AS "数据容量(MB)", sum( AA.索引容量(MB) ) AS "索引容量(MB)" FROM (SELECT table_schema "数据库", table_name AS "表名", table_rows AS "记录数", TRUNCATE ( data_length / 1024 , 2 ) / 1024 AS "数据容量(MB)", TRUNCATE ( index_length / 1024 , 2 ) / 1024 AS "索引容量(MB)" FROM information_schema.TABLES WHERE table_schema = 'Your_db_name' ORDER BY data_length DESC, index_length DESC ) AA GROUP BY aa.表名 WITH ROLLUP ) bb ORDER BY bb.数据容量(MB) DESC, bb.索引容量(MB) DESC;
把里面的Your_db_name 改成自己的数据库名称即可。
sql的方法是别的大神写的,我反正是写不出来。
参考链接: 如何查找出MYSQL数据库数据、索引的占用空间?