Mysql

如何檢查mysql中數據庫的增長?

  • September 23, 2021

我想知道有什麼方法可以檢查文件中數據庫的增長情況

例子

  • 數據庫 A包含****INNODB儲存引擎中的所有表
  • 數據庫 B包含MyISAM儲存引擎中的所有表
  • 數據庫 C包含 InnoDB 和MyISAM表的混合

這很有趣。我剛剛回答了一個問題,我在其中發布了可以執行的查詢,以告訴您數據庫的每個儲存引擎消耗了多少磁碟空間。

請參閱該文章Jan 13, 2013將備份大小轉換為數據庫大小

本質上,您需要查閱 information_schema 以獲取 mysqld 查看的數據和索引大小。以下是一些有關如何獲取所需資訊的範例:

MyISAM

要獲取所有 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;

通過數據庫獲取這個

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;

通過數據庫和引擎得到這個

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;

InnoDB

要獲取所有 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;

通過數據庫獲取這個

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;

通過數據庫和引擎得到這個

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;

警告 #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';

轉到作業系統並執行以下命令:

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

列出的字節總和可能比 information_schema 所說的要多。如果是這種情況,請執行OPTIMIZE TABLE tblname;並執行相同的查詢,您可能會注意到表會縮小。

話雖如此,請確保定期OPTIMIZE TABLE對所有經歷大量更新、刪除和插入的表執行。

這就是我在專用表中監控 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

外殼腳本:

#!/bin/sh

export MYSQL_PWD=XXXXXXXXXX
TABLES="table1 table2 table3"

for TABLE in $TABLES;
do
       FILEPATH=/var/lib/mysql/scarprd_self/$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

引用自:https://dba.stackexchange.com/questions/31864