데이터베이스별 / 테이블별 용량 확인 하기
MySQL을 사용하다 보면 현재 데이터베이스(Database) 혹은 테이블(Table)에 용량(Size)을 확인해야 하는 경우가 있다.
DB(디비)가 올라가 있는 시스템의 디스크 용량이 모자라거나, 아니면 백업(Backup) 혹은 마이그레이션(Migration) 할 때 용량을 알아야 그에 맞게 대응을 할 수 있다.
[MySQL] 데이터베이스 백업 하기
데이터베이스 백업 하기 데이터베이스를 운영시 디스크 공간 혹은 특수한 문제를 대비하기 위해서 백업을 진행한다. 그럼 MySQL에서는 어떻게 백업을 하는지 한번 알아보도록 하자. 1. 전체 백
happylie.tistory.com
그럼 데이터베이스 혹은 테이블별 용량을 확인하는 방법을 알아보도록 하자.
![[MySQL] 데이터베이스별 / 테이블별 용량 확인 하기](https://blog.kakaocdn.net/dna/tiV2q/btrtxmNPKJa/AAAAAAAAAAAAAAAAAAAAAH6652sHocMVLHSFVGRD6YmCzm3iuvzZ5PA1W75dGGef/img.png?credential=yqXZFxpELC7KVnFOS48ylbz2pIh7yKj8&expires=1772290799&allow_ip=&allow_referer=&signature=6koYJNc5RxL8nZt9IX9X2L59CPU%3D)
1. 데이터베이스(Database)별 용량 확인
여러 데이터베이스가 존재하는 경우 데이터베이스별 사용하는 용량을 확인해보자.
조금 편하게 보기 위해서 CONCAT 함수를 이용해서 용량 사이즈를 MB(메가바이트) 기준으로 작성하였다.
- mysql> SELECT table_schema AS DBMS, CONCAT((SUM(data_length + index_length) / 1024 / 1024)," MB") AS "Size" FROM information_schema.TABLES GROUP BY table_schema;
# 데이터베이스(DataBase)별 용량 확인
SELECT
table_schema AS DBMS,
CONCAT((SUM(data_length + index_length) / 1024 / 1024)," MB") AS "Size"
FROM
information_schema.TABLES
GROUP BY
table_schema;
+--------------------+---------------+
| DBMS | Size |
+--------------------+---------------+
| mysql | 2.48437500 MB |
| sys | 0.01562500 MB |
| information_schema | 0.00000000 MB |
| performance_schema | 0.00000000 MB |
| ars | 0.01562500 MB |
+--------------------+---------------+

2. 테이블(Table)별 용량 확인
데이터베이스 내에도 여러 테이블이 존재하기에 이번에는 테이블별 사용하는 용량을 확인해보자.
이번에도 역시 편하게 보기 위해서 CONCAT 함수를 이용해서 용량 사이즈를 MB(메가바이트) 기준으로 작성하였다.
- mysql> SELECT
concat(table_schema,'.',table_name) AS "table",
concat(round(data_length/(1024*1024),2)," MB") AS data,
concat(round(index_length/(1024*1024),2)," MB") AS idx,
concat(round((data_length+index_length)/(1024*1024),2)," MB") AS total_size,
round(index_length/data_length,2) idxfrac
FROM
information_schema.TABLES
WHERE
table_rows is not null;
# 테이블(Table)별 용량 확인
SELECT
concat(table_schema,'.',table_name) AS "table",
concat(round(data_length/(1024*1024),2)," MB") AS data,
concat(round(index_length/(1024*1024),2)," MB") AS idx,
concat(round((data_length+index_length)/(1024*1024),2)," MB") AS total_size,
round(index_length/data_length,2) idxfrac
FROM
information_schema.TABLES
WHERE
table_rows is not null;
+--------------------------------------------------------------------+---------+---------+------------+
| table | data | idx | total_size | idxfrac |
+--------------------------------------------------------------------+---------+---------+------------+
| ars.tb_user | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| information_schema.ADMINISTRABLE_ROLE_AUTHORIZATIONS | 0.00 MB | 0.00 MB | 0.00 MB | NULL |
| information_schema.APPLICABLE_ROLES | 0.00 MB | 0.00 MB | 0.00 MB | NULL |
| information_schema.CHARACTER_SETS | 0.00 MB | 0.00 MB | 0.00 MB | NULL |
| information_schema.CHECK_CONSTRAINTS | 0.00 MB | 0.00 MB | 0.00 MB | NULL |
| information_schema.COLLATIONS | 0.00 MB | 0.00 MB | 0.00 MB | NULL |
| information_schema.COLLATION_CHARACTER_SET_APPLICABILITY | 0.00 MB | 0.00 MB | 0.00 MB | NULL |
| information_schema.COLUMNS | 0.00 MB | 0.00 MB | 0.00 MB | NULL |
| information_schema.COLUMNS_EXTENSIONS | 0.00 MB | 0.00 MB | 0.00 MB | NULL |
| information_schema.COLUMN_PRIVILEGES | 0.00 MB | 0.00 MB | 0.00 MB | NULL |
| information_schema.COLUMN_STATISTICS | 0.00 MB | 0.00 MB | 0.00 MB | NULL |

3. 특정 데이터베이스의 테이블별 용량 확인
2번 항목에서 테이블별 확인 시 모든 데이터베이스의 테이블들의 용량이 나오게 된다.
데이터베이스도 적고, 테이블의 갯수도 적다면 보는데 불편함이 없겠지만 대부분은 불편할 것이다.
그럼 특정 특정 데이터베이스의 테이블에 대해서만 확인하는 방법을 알아보자.
- mysql> SELECT * FROM (
SELECT
concat(table_schema,'.',table_name) AS "table",
concat(round(data_length/(1024*1024),2)," MB") AS data,
concat(round(index_length/(1024*1024),2)," MB") AS idx,
concat(round((data_length+index_length)/(1024*1024),2)," MB") AS total_size,
round(index_length/data_length,2) idxfrac
FROM
information_schema.TABLES
WHERE
table_rows is not null
) as t
WHERE t.table like 'mysql.%'
# 특정 데이터베이스의 테이블별 용량 확인
SELECT * FROM (
SELECT
concat(table_schema,'.',table_name) AS "table",
concat(round(data_length/(1024*1024),2)," MB") AS data,
concat(round(index_length/(1024*1024),2)," MB") AS idx,
concat(round((data_length+index_length)/(1024*1024),2)," MB") AS total_size,
round(index_length/data_length,2) idxfrac
FROM
information_schema.TABLES
WHERE
table_rows is not null
) as t
WHERE t.table like 'mysql.%'
+---------------------------------+---------+---------+------------+---------+
| table | data | idx | total_size | idxfrac |
+---------------------------------+---------+---------+------------+---------+
| mysql.columns_priv | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| mysql.component | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| mysql.db | 0.02 MB | 0.02 MB | 0.03 MB | 1.00 |
| mysql.default_roles | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| mysql.engine_cost | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| mysql.func | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| mysql.general_log | 0.00 MB | 0.00 MB | 0.00 MB | NULL |
| mysql.global_grants | 0.05 MB | 0.00 MB | 0.05 MB | 0.00 |
| mysql.gtid_executed | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| mysql.help_category | 0.02 MB | 0.02 MB | 0.03 MB | 1.00 |
| mysql.help_keyword | 0.09 MB | 0.11 MB | 0.20 MB | 1.17 |
| mysql.help_relation | 0.09 MB | 0.00 MB | 0.09 MB | 0.00 |
| mysql.help_topic | 1.52 MB | 0.13 MB | 1.64 MB | 0.08 |
| mysql.innodb_index_stats | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| mysql.innodb_table_stats | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| mysql.password_history | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| mysql.plugin | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| mysql.procs_priv | 0.02 MB | 0.02 MB | 0.03 MB | 1.00 |
| mysql.proxies_priv | 0.02 MB | 0.02 MB | 0.03 MB | 1.00 |
| mysql.role_edges | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| mysql.server_cost | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| mysql.servers | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| mysql.slave_master_info | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| mysql.slave_relay_log_info | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| mysql.slave_worker_info | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| mysql.slow_log | 0.00 MB | 0.00 MB | 0.00 MB | NULL |
| mysql.tables_priv | 0.02 MB | 0.02 MB | 0.03 MB | 1.00 |
| mysql.time_zone | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| mysql.time_zone_leap_second | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| mysql.time_zone_name | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| mysql.time_zone_transition | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| mysql.time_zone_transition_type | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
| mysql.user | 0.02 MB | 0.00 MB | 0.02 MB | 0.00 |
+---------------------------------+---------+---------+------------+---------+
33 rows in set, 2 warnings (0.00 sec)

🌵댓글