Getting a query plan

Get a query plan:

  1. ydb table query explain \
  2. -q "SELECT season_id, episode_id, title
  3. FROM episodes
  4. WHERE series_id = 1
  5. AND season_id > 1
  6. ORDER BY season_id, episode_id
  7. LIMIT 3"

Query execution plan - 图1

The main section of the query plan, tables, contains information about querying tables. Reads are described in the reads section and writes in the writes section. The key characteristic of any table query is its type.

Types of reads:

  • FullScan: Full table scan. All entries on all shards are read.
  • Scan: A read of a certain range of entries.
  • Lookup: A read by key or key prefix.
  • MultiLookup: Multiple reads by key or key prefix. Supported, for example, in JOINs.

Types of writes:

  • Upsert: Add a single entry.
  • MultiUpsert: Add multiple entries.
  • Erase: A single delete by key.
  • MultiErase: Multiple deletes.

Let’s take the query plan from the example above.
The lookup_by parameter shows what columns (key or key prefix) reads are made by.
The scan_by parameter shows what columns a read of all entries in a certain range of values is made by.
The columns parameter lists the columns whose values will be read from the table.

Example of query modification

Adjust the query so that you get only the first seasons of all the series:

  1. ydb table query explain \
  2. -q "SELECT sa.title AS season_title, sr.title AS series_title, sr.series_id, sa.season_id
  3. FROM seasons AS sa
  4. INNER JOIN series AS sr ON sa.series_id = sr.series_id
  5. WHERE sa.season_id = 1"

Query execution plan - 图2

This query plan implies that a FullScan is made for the seasons table and multiple reads are made for the series table (the MultiLookup type) by the key series_id (lookup_by). The MultiLookup read type and the lookup_by section indicate that the series table is subject to multiple reads by the series_id key.