mysql查看数据库大小表大小,库或表是否存在
来源:原创
时间:2018-10-09
作者:脚本小站
分类:DB
所有库大小:
USE information_schema; select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data from TABLES;
查看各个数据库大小:
USE information_schema; SELECT TABLE_SCHEMA, concat(round(SUM(DATA_LENGTH/1024/1024/1024),2),'GB') FROM TABLES GROUP BY TABLE_SCHEMA;
某数据库大小:
USE information_schema; select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as 数据库名称 from information_schema.TABLES where table_schema='数据库名称'
表大小:
USE information_schema; select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data from TABLES where table_schema='数据库名称' and table_name='表名称';
库是否存在:
USE information_schema; SELECT information_schema.SCHEMATA.SCHEMA_NAME FROM information_schema.SCHEMATA where SCHEMA_NAME='databaseName';
表是否存在:
USE information_schema; SELECT table_name FROM information_schema.TABLES WHERE table_name ='tablename';
查看所有库的大小:
USE 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)', sum(truncate(DATA_FREE/1024/1024, 2)) as '碎片占用(MB)' from information_schema.tables group by table_schema order by sum(data_length) desc, sum(index_length) desc;
查看指定库的容量大小:
USE 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)', sum(truncate(DATA_FREE/1024/1024, 2)) as '碎片占用(MB)' from information_schema.tables where table_schema='要指定库名称' order by data_length desc, index_length desc;
指定库的所有表的容量大小:
USE information_schema; 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)', truncate(DATA_FREE/1024/1024, 2) as '碎片占用(MB)' from information_schema.tables where table_schema='指定库的名称' order by data_length desc, index_length desc;
指定库的指定表的容量大小:
USE information_schema; 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)', truncate(DATA_FREE/1024/1024, 2) as '碎片占用(MB)'from information_schema.tableswhere table_schema='指定的库'and table_name='指定的表'order by data_length desc, index_length desc;
指定库的容量排名前十的表:
USE information_schema; SELECT TABLE_SCHEMA as '数据库', table_name as '表名', table_rows as '记录数', ENGINE as '存储引擎', truncate(data_length/1024/1024, 2) as '数据容量(MB)', truncate(index_length/1024/1024, 2) as '索引容量(MB)', truncate(DATA_FREE/1024/1024, 2) as '碎片占用(MB)'from tables where table_schema='指定的库' order by table_rows desc limit 10;