C.W.K.
Stream
Lesson 03 of 10 · published

Relational vs Document vs Key-Value

~14 min · foundations, models

Level 0스키마 새싹
0 XP0/86 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

옷장 정리하는 세 가지 방식

Relational 은 라벨 붙은 서랍장 — 양말 여기, 셔츠 저기, 모든 서랍이 cross-reference 돼서 "어떤 셔츠가 어떤 바지랑 어울리지" 같은 질문 가능. Document 는 박스 줄 — 박스마다 완성된 한 벌 (셔츠+바지+양말 한 박스에). 잡기 빠르고, "전체 사람 중 빨간 셔츠 누구 갖고 있어?" 는 느려. Key-Value 는 옷걸이 줄 — 라벨 한 개, 아이템 한 개, 그 이상 구조 0.

각 모델이 이기는 때

Relational 은 관계가 중요하고 DB 가 그걸 강제해주길 바랄 때. Document 는 레코드 모양이 진짜로 다양하고 관계가 아닌 document 단위로 쿼리할 때. Key-Value 는 "정확한 키로 X 줘" 의 raw speed 가 필요하고 그것만 필요할 때.

PostgreSQL 의 반전

PostgreSQL 이 메이저 DB 중 유일하게 셋 다 합리적으로 해. Relational 이 기본. JSONB 컬럼이 document 스토리지에 인덱스까지 줘. hstore 가 key-value 줘. "MongoDB 필요한가?" 의 실용적 답은 거의 항상 "아니, JSONB 컬럼 추가해."

Code

Relational: cross-reference 테이블·sql
SELECT customers.name, orders.total
FROM   customers
JOIN   orders ON orders.customer_id = customers.id
WHERE  orders.total > 100;
Document: 같은 데이터를 Postgres JSONB 안에·sql
CREATE TABLE customer_orders (
    id        INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    customer  TEXT NOT NULL,
    payload   JSONB NOT NULL
);

INSERT INTO customer_orders (customer, payload) VALUES
('Alice', '{"orders":[{"product":"Widget","total":150},
                     {"product":"Gadget","total":45}]}');

-- JSON document 안 쿼리
SELECT customer,
       jsonb_array_elements(payload->'orders') ->> 'product' AS product
FROM   customer_orders;
Key-value: Redis 스타일 lookup·text
SET   user:42:session  "abc123"
GET   user:42:session
"abc123"

External links

Exercise

'Alice 의 $100 넘는 주문 다 찾아' 쿼리를 세 형태로 써 봐: 순수 relational (테이블 두 개 + JOIN), 단일 테이블 안 JSONB, pseudo key-value (Redis 명령). 어느 게 읽기 좋은지, 어느 걸 먼저 잡을지 비교.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.