AlgoViz

OLAP

medium 26 min

OLAP (online analytical processing) answers big questions about a lot of history: totals, trends and comparisons.

See it run

How a question about millions of rows gets an answer: the pipeline, the column layout, skipped partitions, cube operations and summaries.

Operations

From orders to a dashboard

The whole pipeline in six stages, from the databases that run the shop to the screen an analyst looks at.

Step 1 of 6

Data sources

Our systemloadanswersOrders DBPostgreSQLPayment DBMySQLCustomer DBPostgreSQLInventory DBMySQLPipelinescheduledWarehouseemptyDashboardsanalysts

Four OLTP databases

Four operational databases hold today's business: orders, payments, customers and inventory. Each is built for small, fast transactions.

Step 1: Data sources

def nightly(sources, warehouse):    raw = [extract(s) for s in sources]    # copy what changed    warehouse.append("raw", raw)    clean = transform(raw)                 # types, duplicates, names    warehouse.replace_day("clean", clean)    warehouse.build("sales_fact", "dims")  # star schema    warehouse.build("daily_summary")       # small and fast
Reads
Batch
Freshness
Hours old, or minutes
Report on the orders database
fresh, but it competes with customers

Say who pays for the query. In a warehouse the big scan runs on machines no customer is waiting for.

In the real world

The same olap doing real jobs. Pick one and step through it.

Why the big question moves away from the orders database.

Step 1 of 3

Monday, one database

small writeshuge scanCustomersAnalystOrders DBrow store
Disk busy
97%

Checkout slows down

Customers and one analyst share the orders database. The analyst asks for two years of revenue by region.

Step 1: Monday, one database

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • SELECT * on a wide table

    critical

    A column store reads only the columns you name. Asking for all of them throws the main benefit away, and many services bill by data read.

    Wrong
    SELECT * FROM sales_fact
    Right
    SELECT region, SUM(amount)FROM sales_factWHERE sale_date >= DATE '2024-03-01'GROUP BY region
  • No filter on the partition column

    critical

    Without it every partition is opened, even when the answer needs one month.

    Wrong
    WHERE product = 'Lamp'
    Right
    WHERE product = 'Lamp'  AND sale_date >= DATE '2024-03-01'  AND sale_date <  DATE '2024-04-01'
  • Counting lines as orders

    major

    A fact table often has one row per product in an order. Counting rows counts lines. Decide the grain first and count distinct orders.

    Wrong
    COUNT(*) AS num_orders
    Right
    COUNT(DISTINCT order_id) AS num_orders
  • Using the warehouse for checkout

    major

    Column stores dislike single-row updates, and the data may be hours old. Keep transactions in the operational database.

    Wrong
    warehouse.update(order_id, status="paid")
    Right
    orders_db.update(order_id, status="paid")# the pipeline carries it to the warehouse

Interview tips

  • How to explain it

    • OLAP answers big questions about a lot of history.
    • It reads few columns of many rows, so it stores columns.
    • A pipeline copies data out of the operational databases.
  • Memory trick: data detective

    • O: oceans of data. L: long-running queries.
    • A: aggregations such as SUM, AVG, COUNT.
    • P: patterns and trends.
  • Reach for it when

    • A question covers months of history.
    • The answer is a total, an average or a trend.
    • Reports slow the operational database.
  • Do not forget

    • How old may the answer be?
    • What is one row of the fact table?
    • Which filter lets the engine skip data?