查看MySQL表占用空间

如果想知道MySQL数据库中每个表占用的空间、表记录的行数的话,可以打开MySQL的 information_schema 数据库。在该库中有一个 TABLES 表,这个表主要字段分别是: TABLE_SCHEMA : 数据库名 TABLE_NAME:表名 ENGINE:所使用的存储引擎 TABLES_ROWS:记录数 DATA_LENGTH:数据大小 INDEX_LENGTH:索引大小
1、进去指定schema 数据库:
      > use information_schema;
2、查询所有数据的大小 :
      > select concat(round(sum(DATA_LENGTH/1024/1024), 2), 'MB') as data from TABLES;
3、查看指定数据库实例的大小:
     > select concat(round(sum(DATA_LENGTH/1024/1024), 2), 'MB') as data from TABLES where table_schema='db_name';
4、查看指定数据库的表的大小: 
     > select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data from TABLES where table_schema='db_name' and table_name='tb_name';
 
示例:
//先进去MySQL自带管理库:information_schema //自己的数据库:abig //自己的表:t_teacher
//得到的结果是以字节为单位,除1024为K,除1048576为M。
mysql> select concat(round(sum(data_length/1024/1024),2),'MB') as data_length_MB, -> concat(round(sum(index_length/1024/1024),2),'MB') as index_length_MB -> from tables where -> table_schema='abig' -> and table_name = 't_teacher';

猜你喜欢

转载自sunwonder.iteye.com/blog/2395811