15
votes

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 FROM information_schema.tables WHERE table_schema='db' and table_name='tablename'

Is it possible to get the size of a MySQL row in GB.

Or how to get the avg row size for the table in GB.

6
That looks like a reasonable way to do it. - nneonneo
@nneonneo From above query i'm getting size of table, but i need to get row size. - Parthi04
Divide by the number of rows? - nneonneo
@nneonneo Yes. I'm considering that too, but will it be a proper way to get ? - Parthi04
I presume that you can use AVG_ROW_LENGTH too if your MySQL supports it. - nneonneo

6 Answers

15
votes

To get the average row length (including overhead), use the AVG_ROW_LENGTH column in the information schema table:

select AVG_ROW_LENGTH from 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.

12
votes

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;
6
votes
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
votes

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
votes

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
-4
votes

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