LoginSignup
1
2

More than 5 years have passed since last update.

MySQL5.6 のデータベースごとやテーブルごとに使用しているデータ量を調べる

Last updated at Posted at 2018-10-30

概要

各データベースやテーブルがそれぞれどれくらいのデータを利用しているか調べるには、
関連するメタデータを保持している information_schema.tables テーブルを使用します。

MySQL :: MySQL 5.6 リファレンスマニュアル :: 21.22 INFORMATION_SCHEMA TABLES テーブル

各データベースごとの使用量

以下の例は、接続しているユーザが参照権限を持つ全てのデータベースのサイズ(データ+インデックス)を表示します。
単位はMB(メガバイト)で小数点第3位を四捨五入しています。

SELECT 
  table_schema as 'database name' 
  ,round(SUM(data_length+index_length)/1024 /1024,2) as 'size(MB)' 
FROM 
  information_schema.tables  
GROUP BY 
  table_schema 
ORDER by 
  SUM(data_length+index_length) DESC;

実行例

特定のデータベースのテーブルごとの使用量

WHERE句のtable_schemaに調査したいデータベース名を指定します。
table_typeは通常テーブル(BASE_TABLE)、ビュー(VIEW)のいずれかを取ります。
下の例では通常テーブルのみ表示しています。
なおTEMPORARY テーブルは対象外です。(information_schema.tablesには含まれない)

avg_row_lengthは1レコードの平均サイズです。

table_rowsはレコード数を意味します。MyISAMの場合は正確ですが、InnoDBの場合は概算です。
(InnoDBで正確に把握したい場合はselect count(*) from tablename;)

公式マニュアルより抜粋
InnoDB テーブルの場合、行カウントは SQL 最適化で使用される単なる概算です。

engineカラムにはInnoDBやMyISAMなどが表示されます。

SELECT
    table_name
    ,round((data_length + index_length) / 1024 / 1024,2) as 'total size(MB)'
    ,round((data_length) / 1024 / 1024,2) as 'data size(MB)'
    ,round((index_length) / 1024 / 1024,2) as 'index size(MB)'
    ,avg_row_length 
    ,table_rows
    ,engine
FROM
    information_schema.tables
WHERE
    table_schema = '<DATABASE NAME>'
    AND table_type = 'BASE TABLE'
ORDER BY
    (data_length + index_length) DESC
;

実行結果例

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2