Study Vault Search notes, aliases and full text
Graph
LectureDBISAI-written · not yet proofread

VL04 Query Planning Basics

The basic table operations projection, selection and Cartesian product, the translation of SELECT–FROM–WHERE queries into sequences of these operations, and simple cost estimates for choosing the cheapest sequence.

Held Source lecture-04

Organisation#

Basic Operations#

From SQL queries to operations#

Lecture 3 explained by example what SELECT … FROM … WHERE … computes. An RDBMS computes it in three steps (Slide 5):

  1. Internally, the RDBMS implements operations on tables (three today, more in Lecture 5).
  2. A SQL query is translated into a sequence of these operations: query planning (done manually for ES4).
  3. The sequence is executed: query processing (implemented in ES4).

The three basic operations are projection, selection and Cartesian product (“cross-product” is used synonymously). Each takes one or two tables as input and always outputs a single table (Slide 6).

Notation#

Recap from Lecture 3 (Relational Model, Slide 7): a table is a triple T=(C,D,R)T = (C, D, R) with column names C=(c1,…,ck)C = (c_1, \dots, c_k), domains D=(D1,…,Dk)D = (D_1, \dots, D_k) and a multiset RR of tuples (r1,…,rk)(r_1, \dots, r_k) with rj∈Djr_j \in D_j. A tuple from RR is called a row, hence the letter RR.

Projection#

Intuitively: keep a subset of the columns of a table.

Selection#

Intuitively: filter the rows of a table by the predicate φ\varphi.

Cartesian product#

Intuitively: all combinations of a row from T1T_1 with a row from T2T_2.

Translating SQL into operations#

With project, select and cartesian-product, every query of the form (Slide 15)

SELECT [comma-separated list of column names]
FROM   [comma-separated list of tables]
WHERE  [Boolean expression on column names];

can be translated into a sequence of operations that computes its result: Cartesian product of the FROM tables, then selection with the WHERE expression, then projection onto the SELECT columns. This sequence can be written in three equivalent ways.

As a tree (leaves are the input tables, inner nodes the operations, the root produces the result):

lecture-04, PDF page 18
lecture-04, PDF page 18

Query Planning#

Motivation#

A single SQL query has many sequences of operations that compute its result. Regarding the result they are all equivalent (up to a permutation of the columns), but their processing time can differ greatly (Slide 19). Choosing a good sequence, a Query Plan, is query planning.

Cost and size estimates#

To pick a sequence (Slide 23):

  1. estimate the cost of each operation in each sequence,
  2. sum these to estimate the total cost of each sequence,
  3. pick the sequence with the lowest total cost.

The cost of an operation depends on the size of its input, but input sizes are only known for the operations at the bottom of the tree (the actual tables). Each operation therefore also needs an estimate of the size of its result (rows and columns). Real query planners use even more statistics.

Let TT have nn rows and kk columns, TiT_i have nin_i rows and kik_i columns, and let k′=∣cols∣k' = |\mathit{cols}| be the number of columns kept by a projection.

Operationcostrowscols
Πcols, distinct(T)\Pi_{\mathit{cols},\,\mathit{distinct}}(T)∼n⋅k′\sim n \cdot k'∼n\sim nk′k'
σφ(T)\sigma_\varphi(T)∼n⋅k\sim n \cdot k∼n/2\sim n/2kk
T1×T2T_1 \times T_2∼n1⋅n2⋅(k1+k2)\sim n_1 \cdot n_2 \cdot (k_1 + k_2)n1⋅n2n_1 \cdot n_2k1+k2k_1 + k_2

Assumptions and reality check:

  • Projection (Slide 24): iterate over every row and copy the k′k' selected columns. Reasonable if tables are stored in row order; in column order one could just copy “pointers” to the columns. The row estimate ignores duplicate removal for distinct=true\mathit{distinct} = \text{true}.
  • Selection (Slide 25): iterate over every row, φ\varphi is evaluated in constant time and (rather arbitrarily) is true for half of the rows. Better: evaluate φ\varphi on sample rows to estimate its cost and selectivity. If φ\varphi has a special form (e.g. score ≥ 8.0) and the table is sorted by that column, not all rows need to be visited.
  • Cartesian product (Slide 26): these estimates are accurate, since the result of size n1⋅n2⋅(k1+k2)n_1 \cdot n_2 \cdot (k_1 + k_2) has to be produced anyway. Good query plans therefore avoid Cartesian products as far as possible; this leads to the join operations of Lecture 5.

Cost estimates for the examples#

Let movies have n1n_1 rows and 4 columns. In Example 1, persons has n2n_2 rows and 3 columns and roles has n3n_3 rows and 2 columns. In Examples 2 and 3, roles has n2n_2 rows and 2 columns. Handwritten annotations on Slides 27–29:

Query plans in SQLite3#

SQLite3 shows the plan it will use (Slide 30):

-- high-level description of the query plan
EXPLAIN QUERY PLAN
SELECT DISTINCT title FROM movies, roles WHERE score >= 8.0;

-- low-level execution plan
EXPLAIN
SELECT DISTINCT title FROM movies, roles WHERE score >= 8.0;

Concepts#

  • Relational Algebra — formal definitions of projection Π\Pi, selection σ\sigma and Cartesian product ×\times, with their cost and size estimates
  • Cartesian Product — all row combinations of two tables; #cols\#\mathrm{cols} add up, #rows\#\mathrm{rows} multiply, so plans avoid it
  • Query Plan — a sequence / nested expression / tree of operations computing a SQL query; choosing the cheapest one is query planning
  • Relational Model — the table definition (C,D,R)(C, D, R) on which the operations are defined
  • Cost model (query planning) — cost, rows and columns estimates per operation; total cost = sum over the sequence
  • Query processing — executing the chosen sequence of operations
  • EXPLAIN / EXPLAIN QUERY PLAN — inspecting SQLite3’s query plan

Search

Type In
↑ ↓ move ↵ open ⇥ next section Ctrl↵ results page Prefix # to search text only