如何在mysql中检查数据库的增长?

Mar*_*tin 4 mysql innodb myisam

我想知道有没有什么方法可以检查文件中数据库的增长情况

例子

  • 数据库A包含INNODB存储引擎中的所有表
  • 数据库 B包含MyISAM存储引擎中的所有表
  • 数据库 C包含 InnoDB 和MyISAM表的混合

Rol*_*DBA 6

这很有趣。我刚刚回答了一个问题,我在其中发布了您可以运行的查询,以告诉您数据库每个存储引擎消耗了多少磁盘空间。

请参阅来自Jan 13, 2013以下内容的帖子:将备份大小转换为数据库大小

本质上,您需要查阅information_schema 以了解mysqld 所查看的数据和索引大小。以下是如何获取所需信息的一些示例:

我的ISAM

要获取所有 MyISAM 表使用的磁盘空间量,请运行以下命令:

SELECT
    myisam_bytes/power(1024,1) myisam_kb,
    myisam_bytes/power(1024,2) myisam_mb,
    myisam_bytes/power(1024,3) myisam_gb
FROM
(
    SELECT SUM(data_length+index_length) myisam_bytes
    FROM information_schema.tables WHERE engine='MyISAM'
    AND table_schema NOT IN ('information_schema','mysql')
) A;
Run Code Online (Sandbox Code Playgroud)

通过数据库获取这个

SELECT db,
    myisam_bytes/power(1024,1) myisam_kb,
    myisam_bytes/power(1024,2) myisam_mb,
    myisam_bytes/power(1024,3) myisam_gb
FROM
(
    SELECT table_schema db,SUM(data_length+index_length) myisam_bytes
    FROM information_schema.tables WHERE engine='MyISAM'
    AND table_schema NOT IN ('information_schema','mysql')
    GROUP BY table_schema
) A;
Run Code Online (Sandbox Code Playgroud)

通过数据库和引擎获取

SELECT db,engine,
    myisam_bytes/power(1024,1) myisam_kb,
    myisam_bytes/power(1024,2) myisam_mb,
    myisam_bytes/power(1024,3) myisam_gb
FROM
(
    SELECT table_schema db,engine,SUM(data_length+index_length) myisam_bytes
    FROM information_schema.tables WHERE engine='MyISAM'
    AND table_schema NOT IN ('information_schema','mysql')
    GROUP BY table_schema,engine
) A;
Run Code Online (Sandbox Code Playgroud)

数据库

要获取所有 InnoDB 表使用的磁盘空间量,请运行以下命令:

SELECT
    innodb_bytes/power(1024,1) innodb_kb,
    innodb_bytes/power(1024,2) innodb_mb,
    innodb_bytes/power(1024,3) innodb_gb
FROM
(
    SELECT SUM(data_length+index_length) innodb_bytes
    FROM information_schema.tables WHERE engine='InnoDB'
) A;
Run Code Online (Sandbox Code Playgroud)

通过数据库获取这个

SELECT db,
    innodb_bytes/power(1024,1) innodb_kb,
    innodb_bytes/power(1024,2) innodb_mb,
    innodb_bytes/power(1024,3) innodb_gb
FROM
(
    SELECT table_schema db,SUM(data_length+index_length) innodb_bytes
    FROM information_schema.tables WHERE engine='InnoDB'
    GROUP BY table_schema
) A;
Run Code Online (Sandbox Code Playgroud)

通过数据库和引擎获取

SELECT db,engine,
    innodb_bytes/power(1024,1) innodb_kb,
    innodb_bytes/power(1024,2) innodb_mb,
    innodb_bytes/power(1024,3) innodb_gb
FROM
(
    SELECT table_schema db,engine,
    SUM(data_length+index_length) innodb_bytes
    FROM information_schema.tables WHERE engine='InnoDB'
    GROUP BY table_schema,engine
) A;
Run Code Online (Sandbox Code Playgroud)

警告 #1

我之前已经讨论过很多次了

警告#2

请记住,这些查询将报告数据和索引占用的实际空间量。实际的文件大小实际上可能比 information_schema 说的多。在这种情况下,您必须OPTIMIZE TABLE tblname;为每个有碎片的表运行。

例如,假设您有一个 MyISAM 表mydb.mytable并运行以下查询:

SELECT
    myisam_bytes/power(1024,1) myisam_kb,
    myisam_bytes/power(1024,2) myisam_mb,
    myisam_bytes/power(1024,3) myisam_gb
FROM information_schema.tables
WHERE table_schema='mydb'
AND table_name='mytable';
Run Code Online (Sandbox Code Playgroud)

转到操作系统并运行以下命令:

  • ls -l mytable.MY[DI] (Linux)
  • dir mytable.MY* (视窗)

列出的字节总和可能比 information_schema 所说的多。如果是这种情况,请运行OPTIMIZE TABLE tblname;并运行相同的查询,您可能会注意到表会缩小。

话虽如此,请确保OPTIMIZE TABLE对所有经历大量更新、删除和插入的表定期运行。