zoukankan      html  css  js  c++  java
  • 查询数据库和表的大小

    如果想知道MySQL数据库中每个表占用的空间、表记录的行数的话,可以打开MySQL的 information_schema 数据库。在该库中有一个 TABLES 表,这个表主要字段分别是:

    TABLE_SCHEMA : 数据库名

    TABLE_NAME:表名

    ENGINE:所使用的存储引擎

    TABLES_ROWS:记录数

    DATA_LENGTH:数据大小

    INDEX_LENGTH:索引大小

    这几个字段对我们来说最有用。 一个表占用空间的大小,相当于是 数据大小 + 索引大小,

    首先查询所有数据库占用磁盘空间大小的SQL语句如下:

    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; 

    然后是查询单个数据库里面各个表所占磁盘空间大小包括其索引的大小的SQL语句如下:

    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 = 'TestDB'  
    group by TABLE_NAME  
    order by data_length desc;

     注意将SQL语句中的TestDB换成你自己的数据库名称即可!

    要想知道每个数据库的大小的话,步骤如下:

    1、进入information_schema 数据库(存放了其他的数据库的信息)

      use information_schema;
    2、查询所有数据的大小:

      select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables;

    3、查看指定数据库的大小: 比如查看数据库home的大小

      select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='home';

    4、查看指定数据库的某个表的大小 比如查看数据库home中 members 表的大小

      select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='home' and table_name='members';

    参考:

    http://blog.csdn.net/wangpanyang/article/details/46005209

    http://www.jb51.net/article/39130.htm

    http://www.t086.com/article/5167

  • 相关阅读:
    idea14导入eclipse项目并部署运行完整步骤
    Java之Socket
    Docker之宿主机ssh至docker容器
    ElasticSearch的安装、使用、踩坑
    Linux下grep、tail、wc、awk文件处理命令
    Spring中@Async注解实现“方法”的异步调用
    Thrift——栗子
    Linux中的守护进程——supervise
    【composer】 PHP composer 镜像地址更换
    【Mac】解决macos安装升级时报错安装所选更新时发生错误的问题
  • 原文地址:https://www.cnblogs.com/domestique/p/8097597.html
Copyright © 2011-2022 走看看