zoukankan      html  css  js  c++  java
  • Mysql查询库、表存储量(Size)

     Mysql查询库、表存储量(Size) 

    1、要查询表所占的容量,就是把表的数据和索引加起来就可以了。

    SELECT SUM(DATA_LENGTH) + SUM(INDEX_LENGTH) FROM information_schema.tables WHERE table_schema='table_name';

    2、查询所有的数据大小

    SELECT CONCAT(ROUND(SUM(DATA_LENGTH/1024/1024), 2), 'M') FROM tables;

    3、查询某个表的数据 

    SELECT CONCAT(ROUND(SUM(DATA_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 '数据库',
    sum(table_rows) as '记录数',
    sum(truncate(data_length/1024/1024, 2)) as '数据容量(MB)',
    sum(truncate(index_length/1024/1024, 2)) as '索引容量(MB)'
    from information_schema.tables
    group by table_schema
    order by sum(data_length) desc, sum(index_length) desc;

    2.查看所有数据库各表容量大小

    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)'
    from information_schema.tables
    order by data_length desc, index_length desc;

     3.查看指定数据库容量大小

    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)'
    from information_schema.tables
    where table_schema='mysql';

    4.查看指定数据库各表容量大小

    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)'
    from information_schema.tables
    where table_schema='mysql'
    order by data_length desc, index_length desc;
  • 相关阅读:
    POJ 1141 括号匹配 DP
    881. Boats to Save People
    870. Advantage Shuffle
    874. Walking Robot Simulation
    文件操作
    861. Score After Flipping Matrix
    860. Lemonade Change
    842. Split Array into Fibonacci Sequence
    765. Couples Holding Hands
    763. Partition Labels
  • 原文地址:https://www.cnblogs.com/fieldtianye/p/10789365.html
Copyright © 2011-2022 走看看