LoginSignup
41
39

More than 5 years have passed since last update.

MySQL : 『DBサイズ』と『Tableサイズ』を確認するコマンドシート

Posted at

データストアリプレイスの際に、既存のDBテーブルのサイズ確認をして見積もりを行う際のコマンドをよく忘れるので記載します。

『DBサイズ確認』

まずは、DBのサイズから見ていきます。
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       |
+--------------------------+----------+
| sample_database1         | 5,243.14 | 
| sample_database2         | 6,443.14 | 
+--------------------------+----------+

GB単位 ※MBをさらに1024で割る。

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             | MB       |
+--------------------------+----------+
| sample_database1         | 5.12     | 
| sample_database2         | 6.2      | 
+--------------------------+----------+

補足

DBを指定してサイズを見たい場合は、上記のクエリの中でGROUP_BY, ORDER BYを消して、where 句でDBNAMEをつけてあげればよいです。

sample_database1を指定
SELECT
    table_schema, sum(data_length+index_length) /1024 /1024 AS MB
FROM
    information_schema.tables
WHERE 
    table_schema = 'sample_database1 ';

+--------------------------+----------+
| table_schema             | MB       |
+--------------------------+----------+
| sample_database1         | 5,243.14 | 
+--------------------------+----------+

『Tableサイズ確認』

つぎは、テーブルのサイズを確認していきます。

SELECT  
    table_name, engine, table_rows AS tbl_rows,
    avg_row_length AS rlen,  
    floor((data_length+index_length)/1024/1024) AS all_mb,
    floor((data_length)/1024/1024) AS data_mb,
    floor((index_length)/1024/1024) AS index_mb
FROM 
    information_schema.tables  
WHERE
    table_schema=database()  
ORDER BY
    (data_length+index_length) DESC;  

+------------------------+--------+----------+------+--------+---------+----------+
| table_name             | engine | tbl_rows | rlen | all_mb | data_mb | index_mb |
+------------------------+--------+----------+------+--------+---------+----------+
| table1                 | MyISAM |  576382  | 454  | 2334   |    2322 |  12      | 
| table2                 | MyISAM |  785480  | 245  | 55666  |   73633 |  12     | 
+------------------------+--------+----------+------+--------+---------+----------+

GBの場合は、DBサイズ同様に1024で割ってください。

column 意味
tbl_rows レコード数
rlen 平均行長
all_mb 総容量
data_mb データ容量
index_mb インデックス容量

参考 : http://d.hatena.ne.jp/sho-yamasaki/20120405/1333640589

41
39
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
41
39