Mysql表空间管理的命令

在日常工作中经常会出现数据库表空间不够的情况,需要对大量临时表进行清理,记录一般执行的如下操作。
介绍一下information_schema是Mysql用于元数据的内部数据库,记录了数据库名,表名,列名和访问权限等信息,如下如所示。


information_schema库中内容
查看数据库、表占用空间情况
  • 切换到information_schema库命令:
use information_schema;
  • 查看所有数据库的大小,以MB为单位,根据换算关系自定义:
select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables;
  • 查看指定数据库的大小,即在上一条命令的基础上加上数据库名称作为筛选条件:
select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='数据库名';
  • 查看指定数据库、指定表的占用空间大小:
select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='数据库名' and table_name='表名';
批量删除数据库下面的表
  • 首先通过select命令生成要删除的表sql语句,加上table_name的筛选条件删除指定表:
select concat('drop table ',table_name,';') from information_schema.`TABLES` WHERE table_schema='数据库名' and table_name='表名';

执行结果如下:


select语句执行结果
  • 批量执行sql语句,方便批量删除数据表(删库需谨慎!)
drop table templ6_0_8512;
drop table templ7_0_5850;
drop table templ7_0_8512;
drop table templ8_0_5850;
drop table templ8_0_8512;
drop table templ9_0_5850;
drop table templ9_0_8512;
drop table template0_5850;
drop table template0_8512;
drop table templh1_5850;
drop table templh1_8512;

执行结果:


删除表操作
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容