协慌网

登录 贡献 社区

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

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

show table status from myDatabaseName;

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

我应该看哪一栏?

答案

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

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;

结果

DB Name              |      DB Size in MB

mydatabase_wrdp             39.1
information_schema          0.0

您可以在此处获取其他详细信息。

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