我可以运行这个查询来获得MySQL数据库中所有表的大小:
show table status from myDatabaseName;
我希望有人能帮助我理解结果。我在找尺寸最大的桌子。
我应该看哪一列?
我可以运行这个查询来获得MySQL数据库中所有表的大小:
show table status from myDatabaseName;
我希望有人能帮助我理解结果。我在找尺寸最大的桌子。
我应该看哪一列?
当前回答
改编自ChapMic的回答,以满足我的特殊需要。
只指定数据库名称,然后按降序对所有表进行排序——在所选数据库中从最大到最小的表。只需要替换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_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为单位)。
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上测试的
最后计算数据库的总大小:
(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"
)
Size of all tables: Suppose your database or TABLE_SCHEMA name is "news_alert". Then this query will show the size of all tables in the database. 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; Output: +---------+-----------+ | Table | Size (MB) | +---------+-----------+ | news | 0.08 | | keyword | 0.02 | +---------+-----------+ 2 rows in set (0.00 sec) For the specific table: Suppose your TABLE_NAME is "news". Then SQL query will be- 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; Output: +-------+-----------+ | 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_schematable WHERE table_schema = \"DB_NAME\" ORDER BY (data_length + index_length) DESC;"|头
对于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