Learn SQL
-- fundamentals.oltp_vs_olap

OLTP vs OLAP & Star Schemas in SQL

Row stores for transactions, columnar marts for analytics

--concept

OLTP handles many small concurrent reads/writes on normalized row stores — orders, banking. OLAP runs heavy aggregations over huge history — dashboards, warehouses, star schemas (central fact + denormalized dimensions). Design follows workload: indexes + normalized for OLTP; columnar + pre-aggregated marts for OLAP.

--how it works
1

OLTP transacts

Many small concurrent reads/writes, normalized row store, PK/FK indexes, short txns.

2

OLAP scans

Few huge aggregations over history — dashboards, warehouses, columnar.

3

Star the marts

Fact (sales) + dimensions (date/product/store) — denormalized for scan speed.

4

Feed both shapes

3NF system of record + ETL-fed marts; OLAP reads to replicas/matviews.

--visualization
orders(id PK, customer FK) — short txns, row locksMany small concurrent writes, 3NF + indexes
Stage 1 / 1
Row store + normalized + PK/FK indexes.
--example
query.sql
1-- star: fact joins dimensions
2SELECT d.month, SUM(f.amount) FROM fact_sales f JOIN dim_date d ON f.date_id = d.id GROUP BY d.month;
Result
monthrevenue
2024-01120000
2024-02135000

Fact holds one row per sale; dimensions decode ids to months, products, stores. Columnar engine scans amount only — not whole rows — for the sum.

--variants

Monthly revenue from star

Fact joins date dimension, columnar sums amount.

query.sql
1SELECT d.month, SUM(f.amount) AS revenue FROM fact_sales f JOIN dim_date d ON f.date_id = d.id GROUP BY d.month ORDER BY d.month;
Result
monthrevenue
2024-01120000
2024-02135000
1Fact contributes one row per sale.
2Dimension decodes month labels.
3Columnar engine scans amount only.

Pick the store

Workload decides storage + shape.

query.sql
1-- OLTP: orders(id PK, customer_id FK) — row store, short txn
2-- OLAP: fact_sales(date_id, amount) — columnar, bulk scan
Result
workloadshape
OLTProw + normalized + indexed
OLAPcolumnar + star + pre-agg
1Banking transfer wants row locks + ACID.
2Yearly dashboard wants column scans + rollups.
3Same business, two physical shapes.
--key facts
OLTPmany small txns, normalized, row store
OLAPfew huge scans, denormalized, columnar
Starfact + dimensions — analytics default
--use cases
Warehouse sizing: columnar marts + replica reads
ETL contracts: 3NF source, star serving layer
--under the hood
Columnar skips non-projected columns — wide-row OLTP would read 10× more.
OLAP pre-aggregation (rollups/matviews) trades freshness for scan avoidance.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.