• 用SQL命令查看Mysql数据库大小 统计数据库空间占用


    -- 1. 进入information_schema 数据库(存放了其他的数据库的信息)
    use information_schema;
    
    -- 2. 查询所有数据的大小:
    select concat(round(sum(data_length/1024/1024),2),'MB') as data 
    from information_schema.tables
    ;
    
    -- 3. 查看实例下所有数据库的空间占用情况
    select 
         table_schema
        ,concat(round(sum(data_length/1024/1024),2),'MB') as data 
    from information_schema.tables 
    where table_schema like 'db_name_%' 
    group by table_schema
    ;
    
    -- 4.查看指定数据库的大小:
    select concat(round(sum(data_length/1024/1024),2),'MB') as data 
    from information_schema.tables 
    where table_schema='home'
    ;
    
    -- 5. 查看指定数据库下的所有表的空间占用情况
    select table_name,round(sum(data_length/1024/1024),2) as size 
    from information_schema.tables 
    where table_schema='DB_NAME'
    group by table_name
    order by size
    ;
    
    -- 6. 查看指定数据库的某个表的大小
    select concat(round(sum(data_length/1024/1024),2),'MB') as data 
    from information_schema.tables 
    where table_schema='home' and table_name='members'
    ;
    select     table_schema    ,round(sum(data_length/1024/1024),2) as data_length     ,round(sum(DATA_FREE/1024/1024),2) as data_free    ,round(sum(INDEX_LENGTH/1024/1024),2) as INDEX_LENGTHfrom information_schema.tables where table_schema='DB_NAME'group by table_schemaorder by data_length;
    select
         TABLE_SCHEMA
        ,sum(DATA_LENGTH)/1024/1024/1024                            as size_DATA_LENGTH_g 
        ,sum(INDEX_LENGTH)/1024/1024/1024                           as size_INDEX_LENGTH_g 
        ,sum(DATA_FREE)/1024/1024/1024                              as size_DATA_FREE_g 
        ,sum((DATA_LENGTH+INDEX_LENGTH+DATA_FREE))/1024/1024/1024   as size_g 
    from information_schema.tables 
    where table_type = 'BASE TABLE'
    group by TABLE_SCHEMA
    order by size_DATA_FREE_g
    ;
    
    select
         TABLE_NAME
        ,sum(DATA_LENGTH)/1024/1024/1024                            as size_DATA_LENGTH_g 
        ,sum(INDEX_LENGTH)/1024/1024/1024                           as size_INDEX_LENGTH_g 
        ,sum(DATA_FREE)/1024/1024/1024                              as size_DATA_FREE_g 
        ,sum((DATA_LENGTH+INDEX_LENGTH+DATA_FREE))/1024/1024/1024   as size_g 
    from information_schema.tables 
    where table_type = 'BASE TABLE'
        and table_schema = 'db_name'
        -- and TABLE_NAME = 'table_name'
    group by TABLE_NAME
    order by size_g desc
    limit 20
    ;
  • 相关阅读:
    sequelize 批量添加和更新数据 bulkCreate
    js 线程和进程的关系
    mysql 索引 笔记1
    mysql 主键
    loj2292 「THUSC 2016」成绩单
    loj2291 「THUSC 2016」补退选
    cf984c Finite or not?
    cf984e Elevator
    loj2540 「PKUWC 2018」随机算法
    luoguT30204 偷上网
  • 原文地址:https://www.cnblogs.com/exmyth/p/15855478.html
Copyright © 2020-2023  润新知