Skip to content

Commit

Permalink
Add minimal documentation for EXPLAIN (tableau#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
vogelsgesang authored Apr 21, 2023
1 parent 260e938 commit 4920bd3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
84 changes: 84 additions & 0 deletions website/docs/sql/command/explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# EXPLAIN

— show the query plan for a query

## Synopsis

```sql_template
EXPLAIN (<option>, ...) <query>;
EXPLAIN [VERBOSE] [ANALYZE] <query>;
```

where the available `<option>`s are

```sql_template
VERBOSE
ANALYZE
OPTIMIZERSTEPS
```

## Description

:::warning Unstable Interface
The returned query plans map directly to Hyper's internal representation of query plans.
As Hyper evolves, the query plans might change considerably.
Don't expect query plans (neither their formatting nor their semantics) to be stable across Hyper versions.
:::


This command retrieves the query plan which Hyper uses to execute the given query.
The query plan describes which tables are accessed in which order, how they are joined together, which expressions are evaluated and all other details of query execution.

One or more `<option>`s can be provided to change which information is displayed and how it is displayed.
In general, options are provided within parentheses after the `EXPLAIN` keyword.
In addition, `VERBOSE` and `ANALYZE` can also be provided without parentheses.

There are three types of plans which can be queried:

* The *optimized plan*. By default, if no other behavior is requested through an `<option>`, `EXPLAIN` will display the optimized plan.
* The *optimizer steps*. If the `OPTIMIZERSTEPS` option is present, Hyper will output the plan at multiple intermediate steps during query optimization, e.g., before and after join reordering.
* The *analyzed plan*. When invoked with the `ANALYZE` option, Hyper will actually execute the query, including all side effects (inserted/deleted tuples, etc.). Instead of the normal query results, you will however receive the query plan of the query, annotated with runtime statistics such as the number of tuples processed by each operator.

By default, plans are formatted as ASCII art.
The ASCII art is useful for quickly grasping the overall query structure, but is lacking details, such as the exact join conditions.
Those details are present in the JSON output format, which can be requested using the `VERBOSE` option.
The `VERBOSE` option can be combined with any other choice.
To visualize a JSON query plan and interactively explore it, copy-paste the plan into [Hyper's Plan Viewer](https://tableau.github.io/query-graphs/).

## Examples

The following will give us the query plan of TPC-H query 6, formatted as ASCII art.

```
EXPLAIN
SELECT sum(l_extendedprice * l_discount) as revenue
FROM lineitem
WHERE l_shipdate >= date '1994-01-01'
AND l_shipdate < date '1995-01-01'
AND l_discount BETWEEN 0.06 - 0.01 AND 0.06 + 0.01
AND l_quantity < 24
```

If we want to understand how the query plan changed during the optimization steps, we can use `OPTIMIZERSTEPS`:

```
EXPLAIN (OPTIMIZERSTEPS)
SELECT sum(l_extendedprice * l_discount) as revenue
FROM lineitem
WHERE l_shipdate >= date '1994-01-01'
AND l_shipdate < date '1995-01-01'
AND l_discount BETWEEN 0.06 - 0.01 AND 0.06 + 0.01
AND l_quantity < 24
```

If we want to view the plan in a graphical plan viewer, or want to see additional details in the plan, we can use the `VERBOSE` option and then copy-paste the resulting JSON into [Hyper's Plan Viewer](https://tableau.github.io/query-graphs/).

```
EXPLAIN (VERBOSE, OPTIMIZERSTEPS)
SELECT sum(l_extendedprice * l_discount) as revenue
FROM lineitem
WHERE l_shipdate >= date '1994-01-01'
AND l_shipdate < date '1995-01-01'
AND l_discount BETWEEN 0.06 - 0.01 AND 0.06 + 0.01
AND l_quantity < 24
```
1 change: 1 addition & 0 deletions website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const sidebars = {
'sql/command/drop_table',
'sql/command/drop_schema',
'sql/command/drop_database',
'sql/command/explain',
'sql/command/prepare',
'sql/command/execute',
'sql/command/deallocate',
Expand Down

0 comments on commit 4920bd3

Please sign in to comment.