46.怎样查看一张表的在磁盘上的大小?
1.元数据信息查询
Mysql元数据信息包括很多内容:表的大小、表的创建时间、那个用户创建的该表、该表中的数据页的信息以及索引页的一些信息等等,今天这里主要将的是关于information_schema下面的tables表,该表信息量很大且很有用,以下就是根据该表进行统计的一些查询信息。
1.1 显示所有的库和表的信息
SELECT TABLE_SCHEMA ,TABLE_NAME from information_schema.tables; SELECT table_schema,GROUP_CONCAT(TABLE_name) from information_schema.tables group BY TABLE_schema;
1.2 查询一下innodb引擎的表
SELECT TABLE_SCHEMA ,TABLE_NAME from information_schema.tables where engine='innodb';
1.3 统计某个库下的某个表占用空间大小
公式:某个表占用空间大小=平均行长度*行数+索引长度 举例1:统计shcool库下的student表占用空间的大下 SELECT TABLE_name,(AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH)/1024 from information_schema.tables WHERE TABLE_schema='shcool' and TABLE_name='student'; 举例2: 统计shcool库下所有表的总大小(求这个库所占用的空间大下) SELECT table_schema,SUM((AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH)/1024) from information_schema.tables where TABLE_schema='shcool';
1.4 统计每个库的数据量大下,并按照数据量大下排序
SELECT TABLE_schema, SUM((AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH)/1024) as total_KB from information_schema.tables GROUP BY TABLE_schema order by total_KB DESC;
1.5.配合concat()函数拼接语句或命令 *****
1.5.1 模仿以下语句,进行数据库的分库分表备份
mysqldump -uroot -p123 world city >/bak/world_city.sql SELECT concat( "mysqldump -uroot -p123"," ",table_schema," ",TABLE_name," >/bak/",TABLE_schema,"_",table_name,".sql") from information_schema.tables
1.5.2 模仿以下语句,进行批量生成对world库下所有表进行操作
ALTER TABLE world.city DISCARD TABLESPACE; select concat("ALTER TABLE", TABLE_schema,".",table_name," DISCARD TABLESPACE") from information_schema.tables WHERE TABLE_schema='shcool';
2.怎样查看一张表在磁盘上的大小
2.1 这里推荐查看informance_schema.INNODB_SYS_TABLESPACES这个表的大小
比如说我这里想要查看在tb1表占用磁盘空间大小
root@localhost 13:37: [information_schema]> select * from innodb_sys_tablespaces where name='liulin/tb1' \G; *************************** 1. row *************************** SPACE: 23 NAME: liulin/tb1 FLAG: 33 FILE_FORMAT: Barracuda ROW_FORMAT: Dynamic PAGE_SIZE: 16384 ZIP_PAGE_SIZE: 0 SPACE_TYPE: Single FS_BLOCK_SIZE: 4096 FILE_SIZE: 98304. # 这个表示文件未压缩时最大的大小 ALLOCATED_SIZE: 98304 # 这个表示文件实际的大小 1 row in set (0.00 sec)
1)FILE_SIZE:the apparent size of the file, which represents the maximum size of the file, uncompressed. This column pertains to the InnoDB transparent page compression feature
2)allocated_size:The actual size of the file, which is the amount of space allocated on disk. This column pertains to the InnoDB transparent page compression feature
参看文章:https://www.percona.com/blog/2016/01/26/finding_mysql_table_size_on_disk/