Skip to content
About
Main content
Query Engines

Query Processing

Why can one question be answered by plans whose costs differ by a factor of ten thousand, and what does each operator force the engine to do?

Who this mechanism is for: a developer who writes code and basic SQL (SELECT, JOIN, GROUP BY) but has never looked inside a database engine. You do not need to know how an , a hash or an works. Every technical term is explained the first time it appears, and again in the glossary at the end. You will learn what each SQL operator forces the engine to do, why some queries are cheap and others are enormous, and why some wrong answers come with no error at all.

The story we will follow

Tarini is a data engineer at Shopsy, an online marketplace with about 40 million customers. She runs the company's analytics warehouse: a cluster of 200 (worker processes), each with 16 GB of memory for query operators, running a distributed SQL engine. Next to her sits Omar, a business analyst. Omar writes most of the SQL. He is good at his job and he trusts SQL to "just work". He is regularly surprised by the engine.

The main tables (all numbers are illustrations):

TableRowsWhat it holds
orders2 billionorder_id, customer_id, product_id, order_ts, amount, status (about 200 bytes per row)
customers40 millioncustomer_id, city, signup_ts, tier
products5 millionproduct data
events50 billionclickstream (every click on the site)
prices300 millionprice history: one row each time a product's price changed, with a valid_from time

Two details will matter later. Customer id 0 means "guest checkout": about 7% of all orders have customer_id = 0. And about 2% of orders have customer_id = NULL, because tracking was broken for a while.

The warehouse reads a fixed of the data. No one is writing to it while a query runs. That makes every query a pure calculation on frozen input, and this is the fact that makes this whole mechanism possible.

Over the next weeks Omar brings Tarini six problems, one at the start of each article below. Each problem looks like a bug in the engine. Each is actually a consequence of what the SQL operators demand. At the end of each article we go back and solve it.

This is the main idea of this mechanism:

How this mechanism is organized

After that: a summary, hands-on labs, interview questions and a glossary.

What do I do, and what does each option cost?

Can I see it myself?

Can I reason about it under questioning?

Trade-offs

The tensions this mechanism takes a position on. Each recurs across mechanisms.

Show all 7 trade-offs
Recurring principles

Ideas this mechanism meets that keep coming back, mechanism after mechanism.

Show all 9 principles