1、进去指定schema 数据库(存放了其他的数据库的信息)
use information_schema
2、查询所有数据的大小
mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data from TABLES ;
+-------------+
| data |
+-------------+
| 588585.94MB |
+-------------+
1 row in set (5 min 24.35 sec)
3、查看指定数据库的大小
比如说 数据库xx
select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data from TABLES where table_schema='xx';
4、查看指定数据库的表的大小
比如说 数据库xx中test表
select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data from TABLES where table_schema='xx' and table_name='xx_test';
5、查看指定库的索引大小
mysql> SELECT CONCAT(ROUND(SUM(index_length)/(1024*1024), 2), ' MB') AS 'Total Index Size' FROM TABLES WHERE table_schema = 'xx';
mysql> SELECT CONCAT(table_schema,'.',table_name) AS 'Table Name', CONCAT(ROUND(table_rows/1000000,4),'M') AS 'Number of Rows', CONCAT(ROUND(data_length/(1024*1024*1024),4),'G') AS 'Data Size', CONCAT(ROUND(index_length/(1024*1024*1024),4),'G') AS 'Index Size', CONCAT(ROUND((data_length+index_length)/(1024*1024*1024),4),'G') AS'Total'FROM information_schema.TABLES WHERE table_schema LIKE 'test';
+---------------+----------------+-----------+------------+---------+
| Table Name | Number of Rows | Data Size | Index Size | Total |
+---------------+----------------+-----------+------------+---------+
| test.a_br | 0.4625M | 0.0259G | 0.0171G | 0.0431G |
| test.a_skuclr | 0.7099M | 0.0660G | 0.0259G | 0.0919G |
| test.a_yuser | 1.0736M | 0.0497G | 0.0213G | 0.0710G |
| test.test | 0.0000M | 0.0000G | 0.0000G | 0.0000G |
+---------------+----------------+-----------+------------+---------+
4 rows in set (0.13 sec)
mysql> SELECT concat(round((sum(DATA_LENGTH)+sum(INDEX_LENGTH))/1024/1024/1024,2),'G') FROM information_schema.TABLES where TABLE_SCHEMA like 'traffic%';
+--------------------------------------------------------------------------+
| concat(round((sum(DATA_LENGTH)+sum(INDEX_LENGTH))/1024/1024/1024,2),'G') |
+--------------------------------------------------------------------------+
| 574.79G |
+--------------------------------------------------------------------------+
1 row in set (4 min 39.07 sec)