๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
studies/Back-end

[MySQL] JSON ํƒ€์ž… ์‚ฌ์šฉํ•ด์„œ JSON Array ๋‹ค๋ฃจ๊ธฐ

by Vada Kim 2023. 9. 16.
728x90
๋ฐ˜์‘ํ˜•
๋ฐ˜์‘ํ˜•

MySQL์— JSON ํƒ€์ž…์ด ์ง€์›๋˜๊ธฐ ์‹œ์ž‘ํ•œ ๊ฑด version 5.7.8 in mid 2015 ๋ถ€ํ„ฐ์ด๋‹ค. MySQL 5.7 ๊ณต์‹ ๊ฐ€์ด๋“œ

์ด ๊ธ€์—์„  8.0 ๊ธฐ์ค€์œผ๋กœ ์„ค๋ช…ํ•œ๋‹ค.

๊ฐœ๋ฐœ ๊ณต๋ถ€ํ• ๋•Œ ์ง์ ‘ ์ฟผ๋ฆฌ๋ฅผ ๋Œ๋ ค๊ฐ€๋ณด๋ฉด์„œ ํ™•์ธํ•˜๊ธฐ ๊ท€์ฐฎ์€ ๋‚˜๊ฐ™์€ ์‚ฌ๋žŒ์—๊ฒŒ ์ด ๊ธ€์„ ๋ฐ”์นœ๋‹ค.

 


JSON ํƒ€์ž…์„ ์ ๊ทน ํ™œ์šฉํ•˜์ž!

JSON ํƒ€์ž… ์ปฌ๋Ÿผ์— ๊ฐ’์„ insertํ• ๋•Œ ์œ ํšจ์„ฑ๊ฒ€์‚ฌ๋ฅผ ์‹œ์Šคํ…œ์—์„œ ์ž๋™์œผ๋กœ ํ•ด์ฃผ๋ฏ€๋กœ, 5.7.8 ์ด์ƒ ๋ฒ„์ „์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๊ตฌ์ง€ varchar, text์™€ ๊ฐ™์€ ๋ฌธ์ž์—ด ํƒ€์ž… ์ปฌ๋Ÿผ์— json ํ˜•ํƒœ string์„ ์ €์žฅํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋˜ํ•œ JSON ์ปฌ๋Ÿผ์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋Š” ์‹œ์Šคํ…œ ์ž์ฒด์—์„œ json ๊ฐ์ฒด๋กœ ์ธ์‹ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ ํ•ฉํ•˜๊ณ  ์‚ฌ์šฉ์ด ํŽธ๋ฆฌํ•˜๋‹ค.

 

 

JSON ํƒ€์ž… ์ปฌ๋Ÿผ์˜ ์‚ฌ์ด์ฆˆ ํ•œ๋„

LONGBLOB, LONGTEXT ํƒ€์ž…๊ณผ ๊ฑฐ์˜ ์œ ์‚ฌํ•œ ์ €์žฅ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง„๋‹ค. ๋‹ค๋งŒ JSON ํƒ€์ž…์— ๋ถ€๊ณผ๋˜๋Š” ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— 4~10๋ฐ”์ดํŠธ๋Š” ์—ฌ์œ ๋ฅผ ๋‘๊ณ  ์‚ฌ์šฉํ•˜์—ฌ์•ผ ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  max_allowed_packet ์‚ฌ์ด์ฆˆ๋ณด๋‹ค ํด ์ˆ˜ ์—†๋‹ค. ์ด ๊ฐ’์€ mysql confํŒŒ์ผ์— ๋ช…์‹œ๋œ ์†์„ฑ์ด๋ฉฐ, MySQL ์„œ๋ฒ„์—์„œ ์ด ๊ฐ’์˜ ๋””ํดํŠธ๋Š” 64MB์ด๋‹ค.

SHOW VARIABLES LIKE 'max_allowed_packet'; -- ์กฐํšŒ

 

์•„๋ฌด๋Ÿฐ ์„ค์ •์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, 67108864 Byte(64MB)๋กœ ํ™•์ธ๋จ.

๋ณ€๊ฒฝ ๋ช…๋ น์€ ์ •์ƒ ์‹คํ–‰์€ ๋˜๋‚˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์•„์„œ ์ฒจ๋ถ€ํ•˜์ง€ ์•Š์•˜๋‹ค.

 

 

 

 

JSON ํƒ€์ž… ์ปฌ๋Ÿผ์„ ํฌํ•จํ•œ ํ…Œ์ด๋ธ” ์ƒ์„ฑ

์‚ฌ์ดํŠธ ๊ฒŒ์‹œ๊ธ€์˜ ํ•ด์‹œํƒœ๊ทธ ๋ชฉ๋ก์„ ์ €์žฅํ•˜๋Š” ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์–ด ๋ณด์•˜๋‹ค.

JSON Array๋ฅผ ์ €์žฅํ•  ์ปฌ๋Ÿผ์„ ๋งŒ๋“ค๊ณ , default value๋กœ๋Š” null์ด ์•„๋‹Œ ๋นˆ json array๊ฐ€ ๋“ค์–ด๊ฐ€๋„๋ก DDL์„ ์ž‘์„ฑํ•ด๋ณด์•˜๋‹ค.

create table post_hashtag(
    seq int auto_increment primary key,
    post_code varchar(20) not null,
    hashtag json not null default (json_array())
);

์ด๋•Œ ์ฃผ์˜์‚ฌํ•ญ์€,

  • MySQL 8.0.13 ์ด์ „ ๋ฒ„์ „์—์„œ๋Š” Not nullํ•œ JSON ์ปฌ๋Ÿผ์— default value๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์—†๋‹ค. 
    Prior to MySQL 8.0.13, a JSON column cannot have a non-NULL default value.
  • Create DDL์—์„œ default value ์„ค์ •์‹œ JSON value๋ฅผ ์†Œ๊ด„ํ˜ธ๋กœ ๋ฌถ์–ด์•ผ ์ •์ƒ ์‹คํ–‰๋œ๋‹ค(์†Œ๊ด„ํ˜ธ๊ฐ€ ์—†์œผ๋ฉด ๋ฌธ๋ฒ• ์˜ค๋ฅ˜ ๋ฐœ์ƒ). ์ด์œ ๋Š” ์•„์ง ์ฐพ์•„๋ณธ ์  ์—†์Œ.

+ Select์‹œ์—๋Š” ์†Œ๊ด„ํ˜ธ๊ฐ€ ์—†์–ด๋„ ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค.

select json_array() from dual; -- ์ •์ƒ ์‹คํ–‰

๊ฒฐ๊ณผ

 

์ด์ œ DML์„ ํ†ตํ•ด ์ปฌ๋Ÿผ์„ ๊ฒ€์ฆํ•ด๋ณด์ž. JSON ํƒ€์ž… ํ•ธ๋“ค ํ•จ์ˆ˜์— ๋Œ€ํ•ด์„  ๋’ค์—์„œ ์„ค๋ช…ํ•˜๊ฒ ๋‹ค.

 

์ปฌ๋Ÿผ ์ตœ์ดˆ ์‚ฝ์ž…

-- default ๊ฐ’์ด ์ž˜ ์ง€์ •๋˜๋Š”์ง€ ํ™•์ธ
insert into post_hashtag (post_code) value ('A100000001');
-- json array ๊ฐ’ ํ• ๋‹น
insert into post_hashtag (post_code, hashtag) value ('A100000002', json_array('mysql8'));

select * from post_hashtag;

๊ฒฐ๊ณผ

Array์— ์š”์†Œ ์ถ”๊ฐ€ ์ฟผ๋ฆฌ

-- json array์— ์š”์†Œ ์ถ”๊ฐ€
update post_hashtag set hashtag = json_merge_preserve(hashtag, json_array('common')) where post_code = 'A100000002';
update post_hashtag set hashtag = json_merge_preserve(hashtag, json_array('programming', 'intellij')) where post_code = 'A100000002';

select * from post_hashtag where post_code = 'A100000002';

๊ฒฐ๊ณผ

Array ์š”์†Œ ์‚ญ์ œ ์ฟผ๋ฆฌ

update post_hashtag set hashtag = json_remove(hashtag, '$[1]') where post_code = 'A100000002';
-- hashtag์—์„œ ์ธ๋ฑ์Šค 1๋ฒˆ ์š”์†Œ ์‚ญ์ œ

select * from post_hashtag where post_code = 'A100000002';

๊ฒฐ๊ณผ

Array ํ†ต์งธ๋กœ ๋ฎ์–ด์“ฐ๊ธฐ

update post_hashtag set hashtag = '["kafka"]' where post_code = 'A100000002';
-- json_array() ํ•จ์ˆ˜ ๋Œ€์‹  jsonํ˜•์‹์— text๋ฅผ ๋Œ€์ž…ํ•ด๋„ ๋จ. ๋‹ค๋งŒ ์ž๋™ ์œ ํšจ์„ฑ๊ฒ€์‚ฌ๋ฅผ ๊ฑฐ์น˜๋ฏ€๋กœ jsonํ˜•์‹์„ ๊ผญ ์ง€์ผœ์•ผ ํ•จ.

select * from post_hashtag where post_code = 'A100000002';

๊ฒฐ๊ณผ

Array ๊ฒ€์ƒ‰ ์ฟผ๋ฆฌ

์š”์†Œ ์ˆœ์„œ์— ์ƒ๊ด€์—†์ด ๊ฒ€์ƒ‰์กฐ๊ฑด์— ๋ช…์‹œํ•œ array์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํฌํ•จ๋œ ๋Œ€์ƒ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

 

๋Œ€์ƒ row ์—†์Œ
๋Œ€์ƒ row ์—†์Œ

hastag์— 'mysql5'์ด๋ผ๋Š” ์š”์†Œ๋ฅผ ๊ฐ€์ง„ ๋ ˆ์ฝ”๋“œ๊ฐ€ ์—†๊ธฐ์— ์กฐํšŒ ๊ฒฐ๊ณผ๊ฐ€ ์—†๋Š” ๋ชจ์Šต์ด๋‹ค.

 

 

 


JSON Object ํƒ€์ž…์„ ์ €์žฅํ•˜๋Š” ์ปฌ๋Ÿผ์„ ์‚ฌ์šฉํ•˜๋Š” ํฌ์ŠคํŒ…์€ ํ›„์†ํŽธ์œผ๋กœ ์ถ”๊ฐ€ํ•  ์˜ˆ์ • :)

 

 

 

 

์ฐธ๊ณ :

https://dev.mysql.com/doc/refman/8.0/en/json.html#json-path-syntax

https://stackoverflow.com/questions/5688403/how-to-check-and-set-max-allowed-packet-mysql-variable

https://dev.mysql.com/doc/refman/8.0/en/storage-requirements.html#data-types-storage-reqs-json

728x90
๋ฐ˜์‘ํ˜•