「卓象程序员」MySQL查看数据库表容量大小的方法示例
时间: 2019-10-10 16:54:48 来源: 卓象IT实训基地【原创】
information_schema简介
在MySQL中,把 information_schema 看作是一个数据库,确切说是信息数据库。其中保存着关于MySQL服务器所维护的所有其他数据库的信息。如数据库名,数据库的表,表栏的数据类型与访问权 限等。
SCHEMATA表:提供了当前mysql实例中所有数据库的信息。
TABLES表:提供了关于数据库中的表的信息(包括视图)。详细表述了某个表属于哪个schema,表类型,表引擎,创建时间等信息。
COLUMNS表:提供了表中的列信息。详细表述了某张表的所有列以及每个列的信息。
STATISTICS表:提供了关于表索引的信息。
下面是使用information_schema表查看数据库信息的案例:
查看所有数据库容量大小
select
table_schema as '数据库',
sum(table_rows) as '记录数',
sum(truncate(data_length/1024/1024, 2)) as '数据容量(MB)',
sum(truncate(index_length/1024/1024, 2)) as '索引容量(MB)'
from information_schema.tables
group by table_schema
order by sum(data_length) desc, sum(index_length) desc
运行结果:
查看所有数据库各表容量大小
select
table_schema as '数据库',
table_name as '表名',
table_rows as '记录数',
truncate(data_length/1024/1024, 2) as '数据容量(MB)',
truncate(index_length/1024/1024, 2) as '索引容量(MB)'
from information_schema.tables
order by data_length desc, index_length desc
运行结果:
查看(指定库)mysql库容量大小
select
table_schema as '数据库',
sum(table_rows) as '记录数',
sum(truncate(data_length/1024/1024, 2)) as '数据容量(MB)',
sum(truncate(index_length/1024/1024, 2)) as '索引容量(MB)'
from information_schema.tables
where table_schema='mysql';
查看(指定库)mysql库各表容量大小
select
table_schema as '数据库',
table_name as '表名',
table_rows as '记录数',
truncate(data_length/1024/1024, 2) as '数据容量(MB)',
truncate(index_length/1024/1024, 2) as '索引容量(MB)'
from information_schema.tables
where table_schema='mysql'
order by data_length desc, index_length desc;
每日分享技术干货~
报名卓象免费训练营课程
学习最新热门IT技术
找一份自己满意的高薪工作
15天免费学习!!
卓象程序员
★ 零基础,随时体验
★ 免费试听,满意后再报名
★ 小班授课,学习氛围浓厚
★ 口碑相传,80%学员来自口碑推荐
★ 教学严管,闯关式学习保障学习效果
★ 公司地点,哈尔滨市东直路123号7楼