如何确定一个特定的 MySQL 表占用了多少磁盘空间?

是否有一种快速的方法来确定一个特定的 MySQL 表占用了多少磁盘空间?该表可以是 MyISAM 或 Innodb。

94676 次浏览

取自 如何检查数据库使用的磁盘空间?

中的 phpMyAdmin来检查 MySQL 表的大小 通过点击左边框中的数据库名称,您的控制面板 读出右边框里桌子的大小。

下面的查询也将有助于在 bytes中获得相同的信息

select SUM(data_length) + SUM(index_length) as total_size
from information_schema.tables
where table_schema = 'db_name'
and table_name='table_name';

你可以看看这些文件的大小。

每个表都存储在一个文件夹中的两个单独文件中,该文件夹的名称与数据库的名称一致。这些文件夹存储在 mysql 数据目录中。

在那里,您可以执行一个“ du-sh. *”来获得磁盘上表的大小。

对于 mydb.mytable表,运行以下命令:

字节

SELECT (data_length+index_length) tablesize
FROM information_schema.tables
WHERE table_schema='mydb' and table_name='mytable';

千字节

SELECT (data_length+index_length)/power(1024,1) tablesize_kb
FROM information_schema.tables
WHERE table_schema='mydb' and table_name='mytable';

兆字节

SELECT (data_length+index_length)/power(1024,2) tablesize_mb
FROM information_schema.tables
WHERE table_schema='mydb' and table_name='mytable';

GB

SELECT (data_length+index_length)/power(1024,3) tablesize_gb
FROM information_schema.tables
WHERE table_schema='mydb' and table_name='mytable';

通用的

下面是一个通用查询,其中最大单位显示为 TB (TeraBytes)

SELECT
CONCAT(FORMAT(DAT/POWER(1024,pw1),2),' ',SUBSTR(units,pw1*2+1,2)) DATSIZE,
CONCAT(FORMAT(NDX/POWER(1024,pw2),2),' ',SUBSTR(units,pw2*2+1,2)) NDXSIZE,
CONCAT(FORMAT(TBL/POWER(1024,pw3),2),' ',SUBSTR(units,pw3*2+1,2)) TBLSIZE
FROM
(
SELECT DAT,NDX,TBL,IF(px>4,4,px) pw1,IF(py>4,4,py) pw2,IF(pz>4,4,pz) pw3
FROM
(
SELECT data_length DAT,index_length NDX,data_length+index_length TBL,
FLOOR(LOG(IF(data_length=0,1,data_length))/LOG(1024)) px,
FLOOR(LOG(IF(index_length=0,1,index_length))/LOG(1024)) py,
FLOOR(LOG(IF(data_length+index_length=0,1,data_length+index_length))/LOG(1024)) pz
FROM information_schema.tables
WHERE table_schema='mydb'
AND table_name='mytable'
) AA
) A,(SELECT 'B KBMBGBTB' units) B;

试试看! ! !

在默认安装了 mysql 的 linux 中:

[you@yourbox]$ ls -lha /var/lib/mysql/<databasename>

基于 NIXCRAFT 的 mysql db 位置

这对于 InnoDB 表来说是不准确的。磁盘上的大小实际上比查询报告的要大。

有关详情,请参阅 Percona 的连结。

Https://www.percona.com/blog/2008/12/16/how-much-space-does-empty-innodb-table-take/

基于 RolandMySQLDBA 的回答,我认为我们可以使用上面的内容来得到表格中每个模式的大小:

SELECT table_schema, SUM((data_length+index_length)/power(1024,1)) tablesize_kb
FROM information_schema.tables GROUP BY table_schema;

真的很喜欢!

我只是使用“ Mysqldiskusing”工具如下

$ mysqldiskusage --server=user:password@localhost mydbname
# Source on localhost: ... connected.


# Database totals:
+------------+----------------+
| db_name    |         total  |
+------------+----------------+
| mydbaname  | 5,403,033,600  |
+------------+----------------+


Total database disk usage = 5,403,033,600 bytes or 5.03 GB

以 MB 为单位快速查询前20个最大的表。

SELECT table_schema, table_name,
ROUND((data_length+index_length)/POWER(1024,2),2) AS tablesize_mb
FROM information_schema.tables
ORDER BY tablesize_mb DESC LIMIT 20;

希望对谁有用!

对于 InnoDB 表,上面的大多数答案都是不准确的。

将10,000条虚拟记录添加到一个表中,并运行上述建议的查询。然后使用 delete 命令(而不是 truncate)删除所有10,000条记录。

再次运行以上建议的查询,您将看到大小不是0。

磁盘上的文件只会增长,即使在删除数据时也不会缩小。如果希望收缩,则需要截断或运行诸如 OPTIMIZETABLE 之类的操作。

似乎没有可靠的方法可以知道 InnoDB 表记录实际上消耗了多少空间。即使磁盘上的文件显示为100MB,DB 中的实际记录也可能为0。