Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

OLTP vs. OLAP and Columnar Databases
SQL

OLTP vs. OLAP and Columnar Databases

Understand the architectural split between transactional and analytical databases, including row vs. columnar storage performance.

Not all databases are built for the same purpose. The way you store and query data for a banking transaction is fundamentally different from how you analyse years of sales data.

OLTP — Online Transaction Processing

OLTP databases handle high volumes of small, real-time transactions. Every time you click “Buy Now” on an e-commerce site, an OLTP database records the order.

Characteristics:

  • High concurrency (thousands of small queries per second)
  • Row-oriented storage (fast reads/writes for individual records)
  • ACID compliant (strict consistency)
  • Normalised schema (reduces redundancy)

Examples: PostgreSQL, MySQL, SQL Server, Oracle.

OLAP — Online Analytical Processing

OLAP databases handle complex queries over large historical datasets. Data analysts run reports like “total revenue by region over the last 5 years.”

Characteristics:

  • Low concurrency (few but heavy queries)
  • Column-oriented storage (fast aggregation over specific columns)
  • Eventually consistent (tolerates stale data)
  • Denormalised schema (star/snowflake schemas for fast joins)

Examples: Redshift, Snowflake, BigQuery, ClickHouse.

Row vs. Columnar Storage

The fundamental difference is how data is physically stored on disk.

AspectRow-OrientedColumn-Oriented
StorageEntire row stored togetherEach column stored separately
Best forMany columns per row, frequent insertsFew columns per row, large scans
Read PatternSELECT * where id = XSELECT SUM(sales) by region
CompressionPoor (mixed data types per block)Excellent (same type per block)
IndexingB-Tree, HashZone maps, min/max sketches

Example: A table with 100 columns. Row storage reads all 100 columns even if you only need 3. Columnar storage reads only the 3 columns you need — massively less I/O for analytical queries.

The Row-Oriented Query

-- OLTP: fetch a single order by ID (row storage excels)
SELECT * FROM orders WHERE order_id = 12345;

The Columnar Query

-- OLAP: aggregate millions of rows (columnar excels)
SELECT region, SUM(revenue)
FROM sales
WHERE year = 2024
GROUP BY region;

HTAP — Hybrid Transactional/Analytical Processing

Modern databases like PostgreSQL (with Citus) and MySQL HeatWave blur the line between OLTP and OLAP. They aim to handle both workloads in a single system, though there are always trade-offs.

Choosing the Right System

Use CaseBest Fit
E-commerce checkoutOLTP (PostgreSQL, MySQL)
Monthly financial reportsOLAP (Snowflake, Redshift)
Real-time dashboardsOLAP with streaming (ClickHouse)
Mixed workload (mostly OLTP)PostgreSQL with read replicas for analytics
Fraud detection (real-time scans)Columnar with low-latency (Druid, Pinot)

Q: What is the difference between OLTP and OLAP?

A: OLTP handles high-volume, small, real-time transactions (banking, e-commerce). OLAP handles complex queries over large historical datasets (analytics, reporting). OLTP uses row-oriented storage; OLAP uses column-oriented storage.

Q: Why is columnar storage faster for analytics?

A: Columnar storage reads only the columns needed by the query, reducing I/O. It also compresses better (same data type per column) and supports vectorised processing (SIMD instructions).

Q: Can PostgreSQL be used for OLAP?

A: PostgreSQL is primarily an OLTP database but can handle analytical queries on moderate-sized datasets. For large-scale OLAP (100B+ rows), dedicated columnar stores like ClickHouse or Redshift are more efficient.

Q: What is a star schema?

A: A star schema is a denormalised data model used in OLAP, consisting of a central fact table (measures, foreign keys) surrounded by dimension tables (descriptive attributes). It optimises for aggregations and slicing.

1. Design for a banking system.

Choice: OLTP (Row-oriented, ACID compliant). Reason: Banking requires strict consistency for transactions. Each operation touches a few rows. Columnar storage adds no benefit here and would slow down writes.

2. Design for a sales analytics dashboard.

Choice: OLAP (Columnar storage). Reason: The dashboard aggregates terabytes of historical data. A columnar store reads only the year, region, and revenue columns, making queries 10-100x faster than row storage.

3. Compare performance: Row vs Columnar for a SELECT COUNT(*).

Answer: SELECT COUNT(*) is equally fast in both because it’s a metadata operation (row count). However, SELECT COUNT(DISTINCT city) on a 100-column table is much faster in columnar because only the city column is read.

4. When would you use a columnar index in a row-oriented DB?

Answer: PostgreSQL’s “columnar” extension (cstore_fdw) or MySQL’s “columnstore” engine can be used for specific tables that are read-heavy and analytical, while keeping transactional tables in the row engine.

My Private Notes

Notes are auto-saved locally to this device.