Parthi04
Parthi04

Reputation: 1151

how to get mysql row size in GB

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.

Upvotes: 18

Views: 41156

Answers (7)

artiebits
artiebits

Reputation: 5195

Use this query to get average row size and other info about your table:

SHOW TABLE STATUS LIKE 'table_name';

Doc.

Upvotes: 0

nneonneo
nneonneo

Reputation: 179452

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.

Upvotes: 18

Ilshat Karazbayev
Ilshat Karazbayev

Reputation: 41

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

Upvotes: 1

Django Janny
Django Janny

Reputation: 640

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 ;

Upvotes: 1

Entity
Entity

Reputation: 1044

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

Upvotes: 8

Ranil Wijeyratne
Ranil Wijeyratne

Reputation: 777

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;

Upvotes: 14

Masood Alam
Masood Alam

Reputation: 415

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

Upvotes: -4

Related Questions