Files
2nd/10_Wiki/Topic_Programming/Coding/DB_OLTP_OLAP_HTAP.md
T
Antigravity Agent 9148c358d0 docs(10_Wiki): 위키 전체 재구성 — Topic_* 폴더를 4개 카테고리로 통합 + 대규모 중복 제거
Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를
Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류.

- 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로
  자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거,
  동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거.
- 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming,
  Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business,
  Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로,
  나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는
  title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백).
  원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지.
- 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서.
- 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는
  지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지.
- Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경.
- 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
2026-07-05 00:33:48 +09:00

7.8 KiB

id, title, category, status, source_trust_level, verification_status, created_at, updated_at, tags, tech_stack, applied_in, aliases
id title category status source_trust_level verification_status created_at updated_at tags tech_stack applied_in aliases
db-oltp-olap-htap OLTP vs OLAP vs HTAP — workload 별 DB Coding draft B conceptual 2026-05-09 2026-05-09
database
oltp
olap
vibe-coding
language applicable_to
SQL
Database
OLTP
OLAP
HTAP
transactional
analytical
columnar
row-based
mixed workload

OLTP / OLAP / HTAP

Workload 다름 = DB 다름. OLTP (작은 read/write 많이) vs OLAP (큰 query 적게) vs HTAP (둘 다). 잘못 선택 = 큰 성능 / cost 차이.

📖 핵심 개념

  • OLTP: Online Transaction Processing.
  • OLAP: Online Analytical Processing.
  • HTAP: Hybrid (둘 다).
  • Row vs Columnar storage.

💻 코드 패턴

OLTP 특징

Workload:
- 매 query 가 작음 (< 100 rows)
- 많은 query (1000+ QPS)
- Read + write mix
- Latency 중요 (< 50ms)
- ACID transaction

Examples:
- User login
- Order creation
- Cart update
- Profile read

DB:
- Postgres / MySQL (default)
- CockroachDB / Spanner (distributed)
- DynamoDB (NoSQL)

OLAP 특징

Workload:
- 매 query 가 큼 (M+ rows)
- 적은 query (분 / 시간)
- Read 거의만
- Throughput 중요
- Scan 가 큰 portion

Examples:
- Daily revenue
- User cohort analysis
- A/B test report
- Dashboard

DB:
- ClickHouse / DuckDB
- Snowflake / BigQuery / Redshift
- Druid / Pinot

Row vs Columnar

Row (OLTP):
[id, name, email, age]
[1,  'Alice', 'a@x', 25]
[2,  'Bob',   'b@x', 30]

→ 1 row 다 읽기 빠름. SELECT * WHERE id=1.

Columnar (OLAP):
id    : [1, 2, 3, ...]
name  : ['Alice', 'Bob', ...]
age   : [25, 30, ...]

→ 1 column 만 읽기 빠름. SELECT AVG(age).
+ Compression 강함 (같은 type 모임).

Postgres (OLTP default)

-- 빠른 single-row
SELECT * FROM users WHERE id = 123;

-- ACID transaction
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;

ClickHouse (OLAP)

CREATE TABLE events (
  ts DateTime,
  user_id UInt32,
  event String,
  value Float32
) ENGINE = MergeTree
ORDER BY (ts, user_id);

-- 빠른 aggregation
SELECT
  toStartOfHour(ts) AS hour,
  count() AS cnt,
  avg(value) AS avg_val
FROM events
WHERE ts > now() - INTERVAL 7 DAY
GROUP BY hour
ORDER BY hour;

→ 1B+ row aggregation = ms / 초.

DuckDB (single-node OLAP)

-- Parquet 직접
SELECT date, SUM(amount) AS total
FROM 's3://bucket/sales/*.parquet'
WHERE date > '2026-01-01'
GROUP BY date;

-- Postgres 도 query (foreign data wrapper)
ATTACH 'postgres://...' AS pg;
SELECT * FROM pg.public.users LIMIT 10;

→ TB 까지 single VM. Spark 보다 simple.

Snowflake (cloud OLAP)

CREATE TABLE events ... CLUSTER BY (ts);

-- Compute scaling
ALTER WAREHOUSE my_wh SET WAREHOUSE_SIZE = 'X-LARGE';

-- Time travel
SELECT * FROM events AT(TIMESTAMP => '2026-05-01');

→ Compute / storage 분리. Pay per query.

BigQuery

SELECT
  DATE(ts) AS day,
  COUNT(*) AS events
FROM `project.dataset.events`
WHERE _TABLE_SUFFIX BETWEEN '20260501' AND '20260509'
GROUP BY day;

→ Pay per scanned bytes. Partition + cluster 가 cost 줄임.

HTAP (Hybrid)

같은 DB 가 OLTP + OLAP 둘 다.

기술:
- TiDB (Raft + columnar)
- SingleStore (MemSQL)
- CockroachDB (column store 추가됨)
- Postgres + Citus columnar

→ "OLTP db 의 read replica = ClickHouse" 식 architecture 가 일반적.

Postgres + Citus (HTAP)

-- Convert to columnar
SELECT alter_table_set_access_method('events', 'columnar');

-- Insert 가 row, query 가 column

Architectures

1. Pure OLTP:
   App → Postgres
   
2. OLAP 분리:
   App → Postgres → CDC → Snowflake / ClickHouse
                                ↓
                              BI tool / dashboard
   
3. HTAP:
   App → TiDB (writes go to OLTP, reads to OLAP)

CDC (Change Data Capture)

Postgres → Debezium → Kafka → ClickHouse / Snowflake.

Real-time:
- Order placed (Postgres)
- 1초 후 dashboard 업데이트 (ClickHouse)

Star schema (OLAP)

-- Fact table (큰)
CREATE TABLE fact_sales (
  date_id INT,
  product_id INT,
  customer_id INT,
  amount DECIMAL,
  quantity INT
);

-- Dimension tables (작은)
CREATE TABLE dim_date (date_id INT, date DATE, year INT, month INT);
CREATE TABLE dim_product (product_id INT, name VARCHAR, category VARCHAR);
CREATE TABLE dim_customer (customer_id INT, country VARCHAR, segment VARCHAR);

-- Join
SELECT d.year, p.category, SUM(f.amount)
FROM fact_sales f
JOIN dim_date d USING (date_id)
JOIN dim_product p USING (product_id)
GROUP BY d.year, p.category;

→ OLAP 의 표준 schema.

Snowflake schema (정규화)

dim_product → dim_category → dim_dept

→ Star 가 simple 가, snowflake 가 정규화.

Data warehouse vs lake

Warehouse: schema-on-write (정형, SQL 친화).
Lake: schema-on-read (raw file, flexible).

Lakehouse: 둘 다 (Iceberg, Delta, Hudi).

Materialized view (OLAP boost)

-- 매일 dashboard query
CREATE MATERIALIZED VIEW daily_revenue AS
SELECT date, SUM(amount) FROM orders GROUP BY date;

-- 빠른 query
SELECT * FROM daily_revenue;

-- Refresh
REFRESH MATERIALIZED VIEW daily_revenue;

→ Pre-compute. Dashboard latency ↓.

Pre-aggregation (Cube.dev)

cube('Sales', {
  sql: 'SELECT * FROM orders',
  measures: {
    total: { sql: 'amount', type: 'sum' },
  },
  dimensions: {
    date: { sql: 'created_at', type: 'time' },
  },
  preAggregations: {
    daily: {
      type: 'rollup',
      measures: [Sales.total],
      timeDimension: Sales.date,
      granularity: 'day',
      refreshKey: { every: '1 hour' },
    },
  },
});

→ API layer 가 자동 pre-agg.

When OLTP, when OLAP?

Query frequency:
> 100 qps → OLTP db
< 1 qps → OLAP db

Query size:
< 1k row → OLTP
> 100k row → OLAP

Latency:
< 50ms → OLTP
> 1s OK → OLAP

Mixed: 두 DB + CDC.

Postgres 가 OLAP 가능?

GB 까지 OK (proper index + partition).
TB+ = 어려움.
PB = 안 됨.

→ Postgres 의 row store 가 큰 scan 약함.
Citus columnar / pg_duckdb / hydra 가 hybrid.

Cost 비교

Postgres RDS: $$
Snowflake: $$$ (compute hour)
BigQuery: $$ (scan bytes)
ClickHouse self-host: $$
Redshift: $$$
DuckDB self-host: $ (1 VM)

Pinot / Druid / StarRocks (real-time OLAP)

ClickHouse: 강력, batch 친화.
Pinot / Druid: real-time + low-latency aggregation.
StarRocks: ClickHouse 대안, MySQL 호환.

→ User-facing OLAP (사용자 dashboard) 가 Pinot / Druid.
Internal BI = ClickHouse / Snowflake.

LLM 가 SQL 작성

const sql = await llm.complete({
  system: 'Generate ClickHouse SQL.',
  prompt: `Schema: ${schema}\nQuestion: How many users active in last 7 days?`,
});

// Validate + run

→ Text-to-SQL. ClickHouse 의 syntax 가 약간 다름 → eval 필수.

🤔 의사결정 기준

상황 DB
일반 app Postgres / MySQL
Analytics dashboard ClickHouse / Snowflake
Single VM TB DuckDB
User-facing analytics Pinot / Druid / StarRocks
HTAP TiDB / SingleStore / Citus
데이터 < 100 GB Postgres OK
데이터 1+ TB analytics Specialized OLAP
Real-time + analytical Materialize / RisingWave

안티패턴

  • Postgres 가 모든 거 (BI): 큰 scan 느림.
  • OLAP 의 매 row update: 비효율.
  • Index 무시 (OLTP): full scan.
  • OLAP 도 transaction 가정: 약함.
  • ETL 없음: 매 query 가 raw.
  • Materialized view stale: 잘못 답.
  • Star schema 없음 OLAP: query 어려움.

🤖 LLM 활용 힌트

  • OLTP = Postgres. OLAP = ClickHouse / Snowflake / DuckDB.
  • HTAP 가 매력 — but mature 기술 적음.
  • CDC 가 OLTP → OLAP 의 답.
  • Star schema 가 OLAP 의 canonical.

🔗 관련 문서