查询数据库占用空间

  • 查询数据库物理文件存放位置

show variables like 'datadir';

  • 查询所有数据库占用空间
select TABLE_SCHEMA, concat(truncate(sum(data_length)/1024/1024,2),' MB') as data_size,
concat(truncate(sum(index_length)/1024/1024,2),'MB') as index_size
from information_schema.tables
group by TABLE_SCHEMA
order by data_length desc;
  • 查询指定数据库所有表占用空间
select TABLE_NAME, concat(truncate(data_length/1024/1024,2),' MB') as data_size,
concat(truncate(index_length/1024/1024,2),' MB') as index_size
from information_schema.tables where TABLE_SCHEMA = 'onecloud_web'
group by TABLE_NAME
order by data_length desc;

删除数据后释放内存

如果foo是InnoDB,执行如下命令

mysql>optimeze table foo;

会返回如图信息,最后的一条Table does not support optimize, doing recreate + analyze instead,即代表optimize无法优化表。

这个时候使用如下命令优化表

mysql>alter table foo ENGINE = 'InnoDB';

mysql>analyze table foo;