[MySQL] JSON ํ์ ์ฌ์ฉํด์ JSON Array ๋ค๋ฃจ๊ธฐ
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'; -- ์กฐํ
๋ณ๊ฒฝ ๋ช ๋ น์ ์ ์ ์คํ์ ๋๋ ๊ฐ์ด ๋ณ๊ฒฝ๋์ง ์์์ ์ฒจ๋ถํ์ง ์์๋ค.
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์ ๋ชจ๋ ์์๊ฐ ํฌํจ๋ ๋์์ ๋ฐํํ๋ค.
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