我可以运行这个查询来获得MySQL数据库中所有表的大小:

show table status from myDatabaseName;

我希望有人能帮助我理解结果。我在找尺寸最大的桌子。

我应该看哪一列?


当前回答

select x.dbname as db_name, x.table_name as table_name, x.bytesize as the_size from
  (select
     table_schema as dbname,
     sum(index_length+data_length) as bytesize,
     table_name
   from
     information_schema.tables
   group by table_schema
  ) x
where
  x.bytesize > 999999
order by x.bytesize desc;

其他回答

SELECT 
    table_name AS "Table",  
    round(((data_length + index_length) / 1024 / 1024), 2) as size   
FROM information_schema.TABLES  
WHERE table_schema = "YOUR_DATABASE_NAME"  
ORDER BY size DESC; 

这将对大小进行排序(DB大小以MB为单位)。

如果您正在使用phpmyadmin,那么只需转到表结构

e.g.

Space usage
Data    1.5 MiB
Index   0   B
Total   1.5 Mi

我发现现有的答案实际上并没有给出磁盘上表的大小,这更有帮助。 与基于data_length的表大小相比,此查询提供了更准确的磁盘估计 和索引。我不得不在AWS RDS实例中使用这种方法,因为您无法物理地检查磁盘和检查文件大小。

select NAME as TABLENAME,FILE_SIZE/(1024*1024*1024) as ACTUAL_FILE_SIZE_GB
, round(((data_length + index_length) / 1024 / 1024/1024), 2) as REPORTED_TABLE_SIZE_GB 
from INFORMATION_SCHEMA.INNODB_SYS_TABLESPACES s
join INFORMATION_SCHEMA.TABLES t 
on NAME = Concat(table_schema,'/',table_name)
order by FILE_SIZE desc

如果你有ssh访问权限,你可能想简单地尝试du -hc /var/lib/mysql(或不同的datadir,在my.cnf中设置)。

另一种显示所占用的行数和空间并按其排序的方法。

SELECT
     table_schema as `Database`,
     table_name AS `Table`,
     table_rows AS "Quant of Rows",
     round(((data_length + index_length) / 1024 / 1024/ 1024), 2) `Size in GB`
FROM information_schema.TABLES
WHERE table_schema = 'yourDatabaseName'
ORDER BY (data_length + index_length) DESC;  

在这个查询中唯一需要替换的字符串是“yourDatabaseName”。