如何获取GB中的mysql表大小

时间:2013-03-30 05:54:25

标签: mysql

最后用以下查询计算了GB表中MySQL表的大小。

  

SELECT(data_length + index_length)/ power(1024,3)tablesize_gb FROM   information_schema.tables WHERE table_schema ='db'和   TABLE_NAME = '表名'

是否可以以GB为单位获取MySQL行的大小。

或者如何以GB为单位获取表格的平均行大小。

6 个答案:

答案 0 :(得分:13)

要获取平均行长度(包括开销),请使用information_schema.tables中的AVG_ROW_LENGTH列。

据我所知,没有办法计算MySQL中单个特定行的确切实际大小。

答案 1 :(得分:10)

嗨,这可能会成功,我们遇到了类似的问题,并且必须找出占用空间最多的行类型。这就是为什么这里有一群人......

SELECT groupval, (sum(length(somefield) + length(someotherfield)) / 1024) / 1024 as "fields_size_mb"
FROM table
GROUP BY groupval
ORDER BY fields_size_mb desc;

答案 2 :(得分:4)

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

答案 3 :(得分:0)

不确定是否要查找该行,但是我最后还是在这里查找行的理论大小(每个字段的大小相加)。

所以我最终提出了这个请求:

select TABLE_NAME, sum(CHARACTER_MAXIMUM_LENGTH)/power(1024,3) 
from COLUMNS  where TABLE_SCHEMA = 'schema' group by 1 order by 2 desc ;

答案 4 :(得分:0)

要计算行的大小,请使用length()方法。

例如:

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]> 

要以GB为单位计算大小,只需将其每1024划分3次

length(row_to_calcsize)/1024/1024/1024

答案 5 :(得分:-4)

要查找表格大小,我们可以使用类似的东西..

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%"

找到MYSQL数据库中最大的表,我们可以使用这样的

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
相关问题