ํฌ์ŠคํŠธ

๐Ÿšง ์žฅ์•  ๋กœ๊ทธ. DB์— ํŒŒํ‹ฐ์…˜์„ ์„ค์ •ํ–ˆ์„ ๋•Œ

์ด์Šˆ ๋ฐœ์ƒ

์„œ๋ฒ„๊ฐ€ ๋ฐ”๋€ ๊ฒƒ์€ ํ•˜๋‚˜๋„ ์—†์—ˆ์Šต๋‹ˆ๋‹ค. ๋‹ฌ์ด ๋ฐ”๋€Œ์—ˆ์„ ๋ฟ์ธ๋ฐ ์ƒˆ๋ฒฝ 12์‹œ๊ฐ€ ์ง€๋‚˜์ž ์‚ฌ์šฉ์ž๋“ค์ด ์ ‘์†์„ ๋ชป ํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ์ปค๋ฎค๋‹ˆํ‹ฐ์—์„œ ๋‚œ๋ฆฌ๊ฐ€ ๋‚ฌ์Šต๋‹ˆ๋‹ค.

๋กœ๊ทธ๋ฅผ ์กฐ์‚ฌํ•ด ๋ณด๋‹ˆ DB์—์„œ ํŠน์ • ํ…Œ์ด๋ธ”์— ๊ฐ’์„ INSERT ํ•  ๋•Œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ํ…Œ์ด๋ธ”์— ์„ค์ •๋œ Partition ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค. ๋‹ฌ์„ ๊ธฐ์ค€์œผ๋กœ Partition์„ ๋‚˜๋ˆ„๋„๋ก ์„ธํŒ…์ด ๋˜์–ด์žˆ์—ˆ๋Š”๋ฐ, ๋ฐ”๋€ ๋‹ฌ์— ํ•ด๋‹นํ•˜๋Š” Partition์ด ์ƒ์„ฑ๋ผ ์žˆ์ง€ ์•Š์•„์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ผ๋‹จ ๊ธ‰ํ•œ ๋Œ€๋กœ Partition์„ ์—ฌ์œ ๋กญ๊ฒŒ 12๊ฐœ์›”๋ถ„ ์ถ”๊ฐ€๋กœ ์ƒ์„ฑํ•ด ์ฃผ๊ณ  ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์™œ ์ด๋Ÿฐ ๊ตฌ์กฐ๊ฐ€ ๋˜์—ˆ๋Š”์ง€ ์กฐ์‚ฌํ•ด ๋ดค์Šต๋‹ˆ๋‹ค. ์• ์ดˆ์— ๋ผ์ด๋ธŒ ์„œ๋น„์Šค์—์„œ ์Šค์ผ€์ฅด๋Ÿฌ๋„ ์•ˆ ๋งŒ๋“ค์–ด๋†“๊ณ  ์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„์„ ํ•œ ๊ฒŒ ์ดํ•ด๊ฐ€ ๋˜์ง„ ์•Š์•˜์ง€๋งŒ, ์„œ๋น„์Šค๋ฅผ ์‹œ์ž‘ํ•  ๋•Œ๋งŒ ํ•˜๋”๋ผ๋„ ์ด๋ ‡๊ฒŒ ์˜ค๋žœ ๊ธฐ๊ฐ„ ์„œ๋น„์Šคํ•  ์ˆ˜ ์žˆ์„ ๊ฑฐ๋ผ๊ณ ๋Š” ์ƒ๊ฐ์ง€ ๋ชปํ–ˆ์„ ์ˆ˜๋„ ์žˆ๊ฒ ๋‹ค ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค. ์šฐ์„  ๊ตฌํ˜„์„ ํ•ด๋‘๊ณ  ๋‚˜์ค‘์— ์ฒœ์ฒœํžˆ ์ˆ˜์ •ํ•˜๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด์—ˆ์„ ์ˆ˜๋„ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ ์ˆ˜์ •

์ด ์ด์Šˆ๋ฅผ ์•ž์œผ๋กœ๋„ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํŒŒํ‹ฐ์…˜ ์ƒ์„ฑ์ด ์ž๋™์œผ๋กœ ์ด๋ค„์ง€๋„๋ก ์„ค์ •ํ•  ํ•„์š”๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. MySQL ํ…Œ์ด๋ธ”์ด ์•„๋ž˜์™€ ๊ฐ™์ด ์ƒ์„ฑ๋ผ ์žˆ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ๋ฅผ ๊ฐ€์ •ํ•˜๊ณ  ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
CREATE TABLE partitioned_table (
    id INT NOT NULL,
    created_at DATETIME NOT NULL,
    text VARCHAR(255),
    PRIMARY KEY (id, created_at)
)
PARTITION BY RANGE (YEAR(created_at) * 100 + MONTH(created_at)) (
    PARTITION p202306 VALUES LESS THAN (202307),
    PARTITION p202307 VALUES LESS THAN (202308)
);

์ด ์˜ˆ์‹œ ํ…Œ์ด๋ธ”์€ 2023๋…„ 7์›”๊ณผ 2023๋…„ 8์›”์— ๋Œ€ํ•œ ํŒŒํ‹ฐ์…˜์„ ์ƒ์„ฑํ•œ ์ƒํƒœ์ž…๋‹ˆ๋‹ค. 2023๋…„ 9์›” ์ดํ›„๊ฐ€ ๋œ๋‹ค๋ฉด ํ•ด๋‹น ํŒŒํ‹ฐ์…˜์ด ์กด์žฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ด ํ…Œ์ด๋ธ”์— ์ƒˆ๋กœ์šด ๊ฐ’์„ ์“ฐ๋ ค๊ณ  ํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์—๋Ÿฌ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์Šค์ผ€์ฅด๋Ÿฌ๋ฅผ ์ƒ์„ฑํ•ด์„œ ์ž๋™์œผ๋กœ ํŒŒํ‹ฐ์…˜์„ ๋งŒ๋“ค์–ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
DELIMITER //

CREATE EVENT create_new_partition
ON SCHEDULE EVERY 1 MONTH
STARTS '2023-08-01 00:00:00'
DO
BEGIN
    DECLARE partition_name VARCHAR(16);
    DECLARE next_partition_value INT;
    
    SET @current_year_month = DATE_FORMAT(NOW(), '%Y%m');
    SET @next_year_month = DATE_FORMAT(DATE_ADD(NOW(), INTERVAL 1 MONTH), '%Y%m');
    SET partition_name = CONCAT('p', @next_year_month);
    SET next_partition_value = (YEAR(NOW()) * 100 + MONTH(NOW()) + 1);
    
    SET @create_partition_sql = CONCAT(
        'ALTER TABLE partitioned_table ADD PARTITION (PARTITION ',
        partition_name,
        ' VALUES LESS THAN (',
        next_partition_value,
        '));'
    );
    
    PREPARE stmt FROM @create_partition_sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END//

DELIMITER ;

์ด ์ฟผ๋ฆฌ๋Š” create_new_partition์ด๋ผ๋Š” ์ด๋ฒคํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. 2023๋…„ 8์›” 1์ผ ์ž์ • ์ดํ›„๋กœ ๋งค์›” 1์ผ ์ž์ •์— ์ด ์ด๋ฒคํŠธ๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ์ด ์ด๋ฒคํŠธ๋Š” ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ ๋‚ ์˜ ๋‹ค์Œ ๋‹ฌ์— ํ•ด๋‹น ํ•˜๋Š” ํŒŒํ‹ฐ์…˜์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์›”์ด ์•„๋‹Œ ๋‹ค์Œ ๋‹ฌ์˜ ํŒŒํ‹ฐ์…˜์„ ์ƒ์„ฑํ•ด์ฃผ๋Š” ์ด์œ ๋Š” ์Šค์ผ€์ฅด๋Ÿฌ๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๋™์•ˆ DB์— ๊ฐ’์ด ์“ฐ์—ฌ์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•œ๋‹ฌ ์ „์— ๋ฏธ๋ฆฌ ํŒŒํ‹ฐ์…˜์„ ์ƒ์„ฑํ•ด์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ MySQL ์ด๋ฒคํŠธ ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…Œ์ด๋ธ”์— ์ž๋™์œผ๋กœ ํŒŒํ‹ฐ์…˜์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฒฐ๋ก 

์• ์ดˆ์— ๋กœ๊ทธ๋Š” ํŒŒ์ผ์— ์“ฐ๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ €๋ ดํ•œ ๋ฐฉ๋ฒ•์ด๊ธฐ ๋•Œ๋ฌธ์— DB์— ๋กœ๊ทธ๋ฅผ ๋‚จ๊ธฐ๋Š” ๊ฒƒ์ด ์ถ”์ฒœํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋‹™๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํฌ๊ธฐ๊ฐ€ ์ž‘์€ ๊ฐ„๋‹จํ•œ ์ˆ˜์ค€์˜ ๋กœ๊ทธ์ด๊ณ , ์กฐํšŒ๊ฐ€ ๋นˆ๋ฒˆํžˆ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด DB์— ์“ฐ๋Š” ๊ฒƒ๋„ ํ•˜๋‚˜์˜ ๋ฐฉ๋ฒ•์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿด ๊ฒฝ์šฐ ํŒŒํ‹ฐ์…˜์„ ์ƒ์„ฑํ•ด์„œ ๋กœ๊ทธ๊ฐ€ ์ผ์ • ๊ธฐ๊ฐ„์ด ์ง€๋‚˜๋ฉด ํ•ด๋‹น ํŒŒํ‹ฐ์…˜์ด ์‚ญ์ œ๋˜๋„๋ก ํ•ด์„œ ๊ด€๋ฆฌ๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ, ์ด ๋•Œ, ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ํŒŒํ‹ฐ์…˜์„ ์–ด๋–ป๊ฒŒ ์ƒ์„ฑํ•˜๊ณ  ๊ด€๋ฆฌํ• ์ง€ ํ™•์‹คํ•˜๊ฒŒ ๊ณ„ํš์„ ์ƒˆ์›Œ๋‘๊ณ  ์ž‘์—…์„ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค.

์ด ๊ธ€์€ ์ €์ž‘๊ถŒ์ž์˜ CC BY 4.0 ๋ผ์ด์„ผ์Šค๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.