我可以运行此查询来获取MySQL数据库中所有表的大小:
show table status from myDatabaseName;
我希望对了解结果有所帮助。我正在寻找尺寸最大的桌子。
我应该看哪一列?
我可以运行此查询来获取MySQL数据库中所有表的大小:
show table status from myDatabaseName;
我希望对了解结果有所帮助。我正在寻找尺寸最大的桌子。
我应该看哪一列?
Answers:
您可以使用此查询显示表的大小(尽管您需要先替换变量):
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;
AND table_name IN ('table_1', 'table_2', 'table_3');
VARCHAR
和BLOB
键入?
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
您可以在此处获得更多详细信息。
如果要查询使用当前选择的数据库。只需复制粘贴此查询即可。(无需修改)
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;
SIZE_MB
FROM information_schema.TABLES WHERE table_schema = DATABASE()ORDER BY((data_length + index_length)ASC;
所有表格的大小:
假设您的数据库或TABLE_SCHEMA
名称为“ news_alert”。然后,此查询将显示数据库中所有表的大小。
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)
对于特定表:
假设您TABLE_NAME
是“新闻”。然后,SQL查询将是-
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)
尝试使用以下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
根据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;
显示行数和占用空间以及排序的另一种方式。
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”。
我发现现有答案实际上并没有给出磁盘上表的大小,这会更有帮助。与基于data_length和index的表大小相比,此查询可提供更准确的磁盘估计。我必须将其用于无法物理检查磁盘和检查文件大小的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
最后计算数据库的总大小:
(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"
)