如何获取MySQL数据库表的大小?

时间:2012-03-08 15:30:32

标签: mysql

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

show table status from myDatabaseName;

我想在理解结果方面提供一些帮助。我正在寻找最大尺寸的桌子。

我应该看哪一栏?

17 个答案:

答案 0 :(得分:1679)

您可以使用此查询来显示表的大小(尽管您需要先替换变量):

SELECT 
    table_name AS `Table`, 
    round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` 
FROM information_schema.TABLES 
WHERE table_schema = "$DB_NAME"
    AND table_name = "$TABLE_NAME";

或此查询列出每个数据库中每个表的大小,最大的第一个:

SELECT 
     table_schema as `Database`, 
     table_name AS `Table`, 
     round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` 
FROM information_schema.TABLES 
ORDER BY (data_length + index_length) DESC;

答案 1 :(得分:86)

SELECT TABLE_NAME AS "Table Name", 
table_rows AS "Quant of Rows", ROUND( (
data_length + index_length
) /1024, 2 ) AS "Total Size Kb"
FROM information_schema.TABLES
WHERE information_schema.TABLES.table_schema = 'YOUR SCHEMA NAME/DATABASE NAME HERE'
LIMIT 0 , 30

您可以从“ information_schema ” - >获取架构名称 SCHEMATA 表格 - > “ SCHEMA_NAME ”栏目


其他 您可以获得大小的mysql数据库,如下所示。

SELECT table_schema "DB Name", 
Round(Sum(data_length + index_length) / 1024 / 1024, 1) "DB Size in MB" 
FROM   information_schema.tables 
GROUP  BY table_schema; 

<强>结果

DB Name              |      DB Size in MB

mydatabase_wrdp             39.1
information_schema          0.0

你可以get additional details in here.

答案 2 :(得分:34)

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为单位)。

答案 3 :(得分:21)

如果要查询使用当前选定的数据库。只需复制粘贴此查询即可。 (无需修改)

SELECT table_name ,
  round(((data_length + index_length) / 1024 / 1024), 2) as SIZE_MB
FROM information_schema.TABLES
WHERE table_schema = DATABASE() ORDER BY SIZE_MB DESC;

答案 4 :(得分:13)

使用Workbench可以轻松获取许多信息:

  • 右键单击架构名称,然后单击&#34;架构检查器&#34;。

  • 在结果窗口中,您有多个标签。第一个标签 &#34;信息&#34;以MB为单位显示数据库大小的粗略估计。

  • 第二个标签&#34; Tables&#34;,显示每个表的数据长度和其他详细信息。

答案 5 :(得分:9)

假设您的数据库名称为 &#34; news_alert&#34;。 然后此查询将显示数据库中所有表的大小。

所有表格的大小:

SELECT
  TABLE_NAME AS `Table`,
  ROUND(((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024),2) AS `Size (MB)`
FROM
  information_schema.TABLES
WHERE
  TABLE_SCHEMA = "news_alert"
ORDER BY
  (DATA_LENGTH + INDEX_LENGTH)
DESC;

<强>输出:

    +---------+-----------+
    | Table   | Size (MB) |
    +---------+-----------+
    | news    |      0.08 |
    | keyword |      0.02 |
    +---------+-----------+
    2 rows in set (0.00 sec)

具体表:

SELECT
  TABLE_NAME AS `Table`,
  ROUND(((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024),2) AS `Size (MB)`
FROM
  information_schema.TABLES
WHERE
    TABLE_SCHEMA = "news_alert"
  AND
    TABLE_NAME = "news"
ORDER BY
  (DATA_LENGTH + INDEX_LENGTH)
DESC;

<强>输出:

+-------+-----------+
| Table | Size (MB) |
+-------+-----------+
| news  |      0.08 |
+-------+-----------+
1 row in set (0.00 sec)

答案 6 :(得分:7)

尝试以下shell命令(将DB_NAME替换为您的数据库名称):

  

mysql -uroot <<<"SELECT table_name AS 'Tables', round(((data_length + index_length) / 1024 / 1024), 2) 'Size in MB' FROM information_schema.TABLES WHERE table_schema = \"DB_NAME\" ORDER BY (data_length + index_length) DESC;" | head

对于Drupal / drush解决方案,请检查以下示例脚本,该脚本将显示正在使用的最大表:

#!/bin/sh
DB_NAME=$(drush status --fields=db-name --field-labels=0 | tr -d '\r\n ')
drush sqlq "SELECT table_name AS 'Tables', round(((data_length + index_length) / 1024 / 1024), 2) 'Size in MB' FROM information_schema.TABLES WHERE table_schema = \"${DB_NAME}\" ORDER BY (data_length + index_length) DESC;" | head -n20

答案 7 :(得分:6)

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

e.g。

Space usage
Data    1.5 MiB
Index   0   B
Total   1.5 Mi

答案 8 :(得分:5)

这是使用bash命令行解决此问题的另一种方法。

for i in mysql -NB -e 'show databases'; do echo $i; mysql -e "SELECT table_name AS 'Tables', round(((data_length+index_length)/1024/1024),2) 'Size in MB' FROM information_schema.TABLES WHERE table_schema =\"$i\" ORDER BY (data_length + index_length) DESC" ; done

答案 9 :(得分:4)

改编自ChapMic的答案以满足我的特殊需要。

仅指定数据库名称,然后按降序对所有表进行排序 - 从所选数据库中的LARGEST到SMALLEST表。只需要替换1个变量=您的数据库名称。

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_HERE"
ORDER BY size DESC;

答案 10 :(得分:2)

另一种显示行数和占用空间以及按顺序排序的方法。

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”。

答案 11 :(得分:1)

如果您拥有ssh访问权限,则可能只想尝试du -hc /var/lib/mysql(或datadir中设置的my.cnf)。

答案 12 :(得分:1)

计算最后的数据库总大小:

(SELECT 
  table_name AS `Table`, 
  round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` 
  FROM information_schema.TABLES 
  WHERE table_schema = "$DB_NAME"
)
UNION ALL
(SELECT 
  'TOTAL:',
  SUM(round(((data_length + index_length) / 1024 / 1024), 2) )
  FROM information_schema.TABLES 
  WHERE table_schema = "$DB_NAME"
)

答案 13 :(得分:1)

这应该在mysql中测试,而不是postgresql SELECT table_schema, # "DB Name", Round(Sum(data_length + index_length) / 1024 / 1024, 1) # "DB Size in MB" FROM information_schema.tables GROUP BY table_schema;

答案 14 :(得分:1)

SELECT TABLE_NAME AS table_name, 
table_rows AS QuantofRows, 
ROUND((data_length + index_length) /1024, 2 ) AS total_size_kb 
FROM information_schema.TABLES
WHERE information_schema.TABLES.table_schema = 'db'
ORDER BY (data_length + index_length) DESC; 

以上所有2个均在mysql上进行了测试

答案 15 :(得分:1)

我发现现有答案实际上并没有给出磁盘上表的大小,这会更有帮助。 与基于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

答案 16 :(得分:0)

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;