2020年5月29日金曜日

テーブルの使用容量確認

時々テーブルのデータが蓄積しすぎでディスク容量を圧迫されることがあります。その時以下のコマンドを使えばテーブル毎の容量、行数など情報をみることができます。

SELECT
  table_name,
  engine,
  table_rows,
  (data_length+index_length)/1024/1024/1024 as allGB,
  (data_length)/1024/1024/1024 as dataGB,
  (index_length)/1024/1024/1024 as indexGB
FROM
  information_schema.tables
WHERE
  table_schema=database()
ORDER BY
  (data_length+index_length) DESC;
+--------------------+--------+------------+-----------------+-----------------+----------------+
| TABLE_NAME         | ENGINE | TABLE_ROWS | allGB           | dataGB          | indexGB        |
+--------------------+--------+------------+-----------------+-----------------+----------------+
user           | InnoDB |   33543611 | 17.791976928711 | 16.137695312500 | 1.654281616211 |
| product       | InnoDB |      43979 |  0.893157958984 |  0.893157958984 | 0.000000000000 |
| comment | InnoDB |          0 |  0.000244140625 |  0.000015258789 | 0.000228881836 |
| category | InnoDB |          0 |  0.000244140625 |  0.000015258789 | 0.000228881836 |
+--------------------+--------+------------+-----------------+-----------------+----------------+

先程テーブル単位のデータ容量を確認できましたが、以下のクエリーを使えばデータベース単位での容量も確認できます。

SELECT
    table_schema, sum(data_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              |
+---------------------+-----------------+
| bbs                 | 81.270685018040 |
user             | 13.613167230971 |
| product             |  1.102905860171 |
| test                |  0.629666061141 |
| log          |  0.017431590706 |
| auth                |  0.003235515207 |
| information_schema  |  0.000000000000 |
+---------------------+-----------------+


0 件のコメント:

コメントを投稿