Simple, Slowly

ブログを引っ越ししました。http://48.jp

データベースとテーブルのサイズを確認する方法

テーブルのサイズはphpMyAdminでも見れますが、コマンドから見れたほうが何かと便利なので、調べてみました。

全てのデータベースのサイズを確認する

# MB単位
select table_schema, sum(data_length+index_length) /1024 /1024 as MB from information_schema.tables  group by table_schema order by sum(data_length+index_length) desc;

+--------------------------+---------------+
| table_schema             | MB                  |
+--------------------------+---------------+
| database1                 | 5579.89978790| 
| database2                 | 3658.48437786| 
+--------------------------+---------------+


# GB単位
select table_schema, sum(data_length+index_length) /1024 /1024/1024 as GB from information_schema.tables  group by table_schema order by sum(data_length+index_length) desc;

+--------------------------+----------------+
| table_schema             | GB                |
+--------------------------+----------------+
| database1                  | 5.449120886624|  
| database2                  | 3.572738650255| 
+--------------------------+----------------+

特定のデータベースのサイズを確認する

use database1;
select table_schema, sum(data_length+index_length) /1024 /1024 as MB from information_schema.tables where table_schema = database();

もしくは
select table_schema, sum(data_length+index_length) /1024 /1024 as MB from information_schema.tables where table_schema = 'databaseName';

+-----------------+---------------+
| table_schema | MB            |
+-----------------+---------------+
| database1      | 5579.89978790| 
+-----------------+---------------+

テーブル単位でサイズを確認する

use databaseName;
select  
table_name, engine, table_rows as tbl_rows, avg_row_length as rlen,  
floor((data_length+index_length)/1024/1024) as allMB,  #総容量
floor((data_length)/1024/1024) as dMB,  #データ容量
floor((index_length)/1024/1024) as iMB   #インデックス容量
from information_schema.tables  
where table_schema=database()  
order by (data_length+index_length) desc; 

+------------------------------+--------+----------+------+-------+------+------+
| table_name                | engine     | tbl_rows  | rlen | allMB | dMB | iMB |
+------------------------------+--------+----------+------+-------+------+------+
| table1                 | MyISAM   |   156382     | 3738 |   570 |  557 |   12 | 
| table2                 | MyISAM   |   185280     | 1624 |   311 |  287 |   24 | 
| table3                 | MyISAM   |   208823     |  394  |   103 |   78  |   24 | 
+------------------------------+--------+----------+------+-------+------+------+

ストアドプロシージャに登録する

いちいち入力するのは面倒なので、ストアドプロシージャに登録しておくと便利です。

mysql> delimiter //
mysql> create procedure show_table_size()
mysql> begin
mysql> select  
mysql> table_name, engine, table_rows as tbl_rows, avg_row_length as rlen,  
mysql> floor((data_length+index_length)/1024/1024) as allMB, 
mysql> floor((data_length)/1024/1024) as dMB,  
mysql> floor((index_length)/1024/1024) as iMB 
mysql> from information_schema.tables  
mysql> where table_schema=database()  
mysql> order by (data_length+index_length) desc; 
mysql> end
mysql> //
mysql> delimiter ;

これで登録完了です。
使うときは、

mysql> call show_table_size;

これで簡単に使えますね。


ストアドプロシージャの一覧を見るには以下のコマンドで。

mysql> show procedure status;

必要なくなったプローシージャは以下のコマンドで削除ができます。

mysql> drop procedure show_table_size;