# Execution plans

> Read how the engine runs a query, spot what slows it down and compare the plan before and after an index.

Source: https://www.opendb.tools/en/docs/execution-plans

An **execution plan** describes how the database engine will run a query: which tables it scans, which indexes it uses, how it joins and sorts.

![Execution plan of a query in the SQL editor](/images/docs/en/execution-plan.webp)

## Get a plan

Three places give a plan:

- the **Plan** button of the [SQL editor](/en/docs/sql-editor);
- the agent, in the chat: "Why is this query slow?";
- the **Indexes** button of the [Tables tab](/en/docs/table-assistants), which uses it for its advice.

The query **is not run**. OpenDB asks the engine for its plan in a read-only transaction, which it then rolls back.

## Read the plan

The plan is shown in three forms.

| View | Content |
|---|---|
| Tree | The steps of the plan, linked in execution order |
| Table | The classic `EXPLAIN` output |
| Raw JSON | The engine's answer, untouched |

Click a step of the tree or a line of the query to see its detail. The arrow keys walk through the tree.

At the top of the plan are the estimated **cost**, the number of **rows read** and the number of **alerts**.

### The same words for every engine

Each engine has its own vocabulary. OpenDB maps it to common operations:

| Operation | What the engine does |
|---|---|
| Full scan | Reads every row of the table |
| Index scan | Reads a whole index |
| Range scan | Reads part of an index |
| Index lookup | Fetches rows through an index |
| Unique lookup | Fetches one row through a unique key |
| Nested loop, hash join, merge join | Three ways to join two tables |
| Group, sort, limit | `GROUP BY`, `ORDER BY`, `LIMIT` |

## Alerts

OpenDB flags what most often slows a query down.

| Alert | Meaning |
|---|---|
| Full scan | A table is read in full for lack of a usable index |
| Sort without index | Rows are sorted after being read, no index provides the order |
| Temporary table | The engine builds an intermediate table |
| Join without index | A join scans a whole table for each row |
| Dependent subquery | A subquery is run again for each row |
| Large estimate | The engine expects to read a very large number of rows |

Each alert gives the table and columns involved, with an explanation. These explanations are produced by OpenDB from the plan, not written by the model: they are stable from one time to the next.

## Compare before and after

In a conversation, OpenDB keeps the plan of each query. When you ask for the plan of the same query again, it shows the comparison:

- the alerts that are **resolved**, **still present** and **new**;
- the cost and the number of rows read, before and after.

The query is recognized even if its spacing, case or values have changed.

### Example

1. You ask: "Why is this query slow?"
2. The plan shows a full scan of `product_views` and a temporary table.
3. The agent suggests an index on `(viewed_at, product_id)`, without creating it.
4. You create the index.
5. You ask for the plan again: the full scan is marked as resolved, and the cost has dropped.

## What the model sees

The agent receives a **summary** of the plan, so that it can suggest relevant indexes. This summary only contains structure: tables, columns, indexes, estimates. It contains no data.

## Differences between engines

| Engine | Particularity |
|---|---|
| MySQL, MariaDB, PostgreSQL | Full plan, with costs and estimates |
| SQLite | Plan without costs: SQLite does not provide them |

OpenDB refuses to compute the plan of a query that would write while doing so: `EXPLAIN ANALYZE`, `SELECT ... INTO` and `SELECT ... FOR UPDATE`.
