how to get mysql table size in GB

后端 未结 6 594
终归单人心
终归单人心 2020-12-09 04:43

Just ended up with calculating the size of MySQL table in GB with the following query.

SELECT (data_length+index_length)/power(1024,3) tablesize_gb FR

相关标签:
6条回答
  • 2020-12-09 05:03

    To find table size we can use something like this..

    SELECT count(*) tables,
           concat(round(sum(table_rows)/1000000,2),'M') rows,
           concat(round(sum(data_length)/(1024*1024*1024),2),'G') data,
           concat(round(sum(index_length)/(1024*1024*1024),2),'G') idx,
           concat(round(sum(data_length+index_length)/(1024*1024*1024),2),'G') total_size,
           round(sum(index_length)/sum(data_length),2) idxfrac
           FROM information_schema.TABLES
           WHERE  table_name like "%table-name%"
    

    Find the largest table in MYSQL Databases we can use something like this

    SELECT CONCAT(table_schema, '.', table_name),
           CONCAT(ROUND(table_rows / 1000000, 2), 'M')                                    rows,
           CONCAT(ROUND(data_length / ( 1024 * 1024 * 1024 ), 2), 'G')                    DATA,
           CONCAT(ROUND(index_length / ( 1024 * 1024 * 1024 ), 2), 'G')                   idx,
           CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 * 1024 ), 2), 'G') total_size,
           ROUND(index_length / data_length, 2)                                           idxfrac
    FROM   information_schema.TABLES
    ORDER  BY data_length + index_length DESC
    LIMIT  10
    
    0 讨论(0)
  • 2020-12-09 05:10
    SELECT 
         table_name AS `Table`, 
         round(((data_length + index_length) / 1024 / 1024 ), 2) as `Size in MB`,
         round((AVG_ROW_LENGTH / 1024), 2) as `Avg row size in KB`
    FROM information_schema.TABLES WHERE table_schema = 'your_db_name'
    ORDER BY `Size in MB` DESC
    
    0 讨论(0)
  • 2020-12-09 05:11

    To get the average row length (including overhead), use the AVG_ROW_LENGTH column in information_schema.tables.

    As far as I'm aware, there's no way to calculate the exact actual size of a single, specific row in MySQL.

    0 讨论(0)
  • 2020-12-09 05:13

    Hi this might do the trick, we had a similar issue and had to find out which types of rows take up the most space. That's why here with a group by...

    SELECT groupval, (sum(length(somefield) + length(someotherfield)) / 1024) / 1024 as "fields_size_mb"
    FROM table
    GROUP BY groupval
    ORDER BY fields_size_mb desc;
    
    0 讨论(0)
  • 2020-12-09 05:16

    Not sure if you were looking for that, but I end up here looking for the theorical size of a row (addition of every fields' size).

    So I finaly came up with this request :

    select TABLE_NAME, sum(CHARACTER_MAXIMUM_LENGTH)/power(1024,3) 
    from COLUMNS  where TABLE_SCHEMA = 'schema' group by 1 order by 2 desc ;
    
    0 讨论(0)
  • 2020-12-09 05:20

    To calculate the size of a row, use length() method.

    For example:

    MariaDB [db]> select id,length(row_to_calcsize) from tablename order by id desc limit 2\G
    *************************** 1. row ***************************
         id: 501
    length(row_to_calcsize): 2192911
    *************************** 2. row ***************************
         id: 500
    length(row_to_calcsize): 51657
    2 rows in set (0.00 sec)
    
    MariaDB [db]> 
    

    To calculate size in GB, just divide it 3 times per 1024

    length(row_to_calcsize)/1024/1024/1024
    
    0 讨论(0)
提交回复
热议问题