Mysql查询库、表存储量Size)
1、要查询表所占的容量,就是把表的数据和索引加起来就可以了。
SELECT SUMDATA_LENGTH) + SUMINDEX_LENGTH) FROM information_schema.tables WHERE table_schema='table_name';
2、查询所有的数据大小
SELECT CONCATROUNDSUMDATA_LENGTH/1024/1024), 2), 'M') FROM tables;
3、查询某个表的数据
SELECT CONCATROUNDSUMDATA_LENGTH/1024/1024),2),'M') FROM tables WHERE table_schema='database_name' AND table_name='table_name';
—– 注:获取结果是字节单位,转换为M单位。
mysql中information_schema数据库,存储数据库元数据,包括数据库信息、数据库中表的信息等。
schemata表:这个表里面主要是存储在mysql中的所有的数据库的信息
tables表:这个表里存储了所有数据库中的表的信息,包括每个表有多少个列等信息。
columns表:这个表存储了所有表中的表字段信息。
statistics表:存储了表中索引的信息。
user_privileges表:存储了用户的权限信息。
schema_privileges表:存储了数据库权限。
table_privileges表:存储了表的权限。
column_privileges表:存储了列的权限信息。
character_sets表:存储了mysql可以用的字符集的信息。
collations表:提供各个字符集的对照信息。
collation_character_set_applicability表:相当于collations表和character_sets表的前两个字段的一个对比,记录了字符集之间的对照信息。
table_constraints表:这个表主要是用于记录表的描述存在约束的表和约束类型。
key_column_usage表:记录具有约束的列。
routines表:记录了存储过程和函数的信息,不包含自定义的过程或函数信息。
views表:记录了视图信息,需要有show view权限。
triggers表:存储了触发器的信息,需要有super权限。
1.查看所有数据库容量大小
select table_schema as '数据库', sumtable_rows) as '记录数', sumtruncatedata_length/1024/1024, 2)) as '数据容量MB)', sumtruncateindex_length/1024/1024, 2)) as '索引容量MB)' from information_schema.tables group by table_schema order by sumdata_length) desc, sumindex_length) desc;
2.查看所有数据库各表容量大小
select table_schema as '数据库', table_name as '表名', table_rows as '记录数', truncatedata_length/1024/1024, 2) as '数据容量MB)', truncateindex_length/1024/1024, 2) as '索引容量MB)' from information_schema.tables order by data_length desc, index_length desc;
3.查看指定数据库容量大小
select table_schema as '数据库', sumtable_rows) as '记录数', sumtruncatedata_length/1024/1024, 2)) as '数据容量MB)', sumtruncateindex_length/1024/1024, 2)) as '索引容量MB)' from information_schema.tables where table_schema='mysql';
4.查看指定数据库各表容量大小
select table_schema as '数据库', table_name as '表名', table_rows as '记录数', truncatedata_length/1024/1024, 2) as '数据容量MB)', truncateindex_length/1024/1024, 2) as '索引容量MB)' from information_schema.tables where table_schema='mysql' order by data_length desc, index_length desc;