我可以运行这个查询来获得MySQL数据库中所有表的大小:
show table status from myDatabaseName;
我希望有人能帮助我理解结果。我在找尺寸最大的桌子。
我应该看哪一列?
我可以运行这个查询来获得MySQL数据库中所有表的大小:
show table status from myDatabaseName;
我希望有人能帮助我理解结果。我在找尺寸最大的桌子。
我应该看哪一列?
当前回答
尝试以下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
其他回答
你可以使用这个查询来显示表的大小(尽管你需要先替换变量):
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;
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
ORDER BY `DB Size in MB` DESC;
结果
DB Name | DB Size in MB
mydatabase_wrdp 39.1
information_schema 0.0
你可以在这里得到更多的细节。
我使用这个shell脚本来跟踪表大小(以字节和行数为单位)
#!/bin/sh
export MYSQL_PWD=XXXXXXXX
TABLES="table1 table2 table3"
for TABLE in $TABLES;
do
FILEPATH=/var/lib/mysql/DBNAME/$TABLE.ibd
TABLESIZE=`wc -c $FILEPATH | awk '{print $1}'`
#Size in Bytes
mysql -D scarprd_self -e "INSERT INTO tables_sizes (table_name,table_size,measurement_type) VALUES ('$TABLE', '$TABLESIZE', 'BYTES');"
#Size in rows
ROWSCOUNT=$(mysql -D scarprd_self -e "SELECT COUNT(*) AS ROWSCOUNT FROM $TABLE;")
ROWSCOUNT=${ROWSCOUNT//ROWSCOUNT/}
mysql -D scarprd_self -e "INSERT INTO tables_sizes (table_name,table_size,measurement_type) VALUES ('$TABLE', '$ROWSCOUNT', 'ROWSCOUNT');"
mysql -D scarprd_self -e "DELETE FROM tables_sizes WHERE measurement_datetime < TIMESTAMP(DATE_SUB(NOW(), INTERVAL 365 DAY));"
done
它假设有这个MySQL表
CREATE TABLE `tables_sizes` (
`table_name` VARCHAR(128) NOT NULL,
`table_size` VARCHAR(25) NOT NULL,
`measurement_type` VARCHAR(10) NOT NULL CHECK (measurement_type IN ('BYTES','ROWSCOUNT')),
`measurement_datetime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP()
) ENGINE=INNODB DEFAULT CHARSET=utf8
最后计算数据库的总大小:
(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"
)
如果您想查询使用当前选定的数据库。简单地复制粘贴这个查询。(无需修改)
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;