说明:
通过MySQL的information_schema数据库,可查询数据库中每个表占用的空间、表记录的行数;该库中有一个TABLES表,这个表主要字段分别是:
TABLE_SCHEMA:数据库名
TABLE_NAME:表名
ENGINE:所使用的存储引擎
TABLES_ROWS:记录数
DATA_LENGTH:数据大小
INDEX_LENGTH:索引大小
其他字段请参考MySQL的手册,查看一个表占用空间的大小,那就相当于是数据大小 + 索引大小 。
查看所有库的大小
mysql> use information_schema;
mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data from TABLES;
查看指定库的大小
mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB')
as data from TABLES where table_schema='mysql';
查看指定库的指定表的大小
mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB')
as data from TABLES where table_schema='mysql' and table_name='user';
查看指定库的索引大小
mysql> SELECT CONCAT(ROUND(SUM(index_length)/(1024*1024), 2), ' MB')
AS 'Total Index Size' FROM TABLES WHERE table_schema = 'mysql';
查看指定库的指定表的索引大小
mysql> SELECT CONCAT(ROUND(SUM(index_length)/(1024*1024), 2), ' MB')
AS 'Total Index Size' FROM TABLES WHERE table_schema = 'mysql' and table_name='user';
查询指定数据库中每个表的总行数,数据大小,索引大小和总大小
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 'mysql';
查询数据库指定表的数据部分大小,索引部分大小和总占用磁盘大小
mysql> SELECT a.table_schema ,a.table_name ,
concat(round(sum(DATA_LENGTH / 1024 / 1024) +
sum(INDEX_LENGTH / 1024 / 1024) ,2) ,'MB') total_size ,
concat(round(sum(DATA_LENGTH / 1024 / 1024) , 2) ,'MB') AS data_size ,
concat(round(sum(INDEX_LENGTH / 1024 / 1024) , 2) ,'MB') AS index_size
FROM information_schema. TABLES a WHERE
a.table_schema = '数据库' AND a.table_name = '表名';
--end--