EXPLAIN

On this page Carat arrow pointing down
Warning:
CockroachDB v19.2 is no longer supported. For more details, see the Release Support Policy.

The EXPLAIN statement returns CockroachDB's query plan for an explainable statement. You can then use this information to optimize the query.

Tip:

To actually execute a statement and return a physical query plan with execution statistics, use EXPLAIN ANALYZE.

Query optimization

Using EXPLAIN's output, you can optimize your queries by taking the following points into consideration:

  • Queries with fewer levels execute more quickly. Restructuring queries to require fewer levels of processing will generally improve performance.

  • Avoid scanning an entire table, which is the slowest way to access data. You can avoid this by creating indexes that contain at least one of the columns that the query is filtering in its WHERE clause.

  • New in v19.2: By default, the vectorized execution engine is enabled for all supported operations and data types. If you are querying a table with a small number of rows, it might be more efficient to use row-oriented execution. The vectorize_row_count_threshold cluster setting specifies the minimum number of rows required to use the vectorized engine to execute a query plan.

You can find out if your queries are performing entire table scans by using EXPLAIN to see which:

  • Indexes the query uses; shown as the Description value of rows with the Field value of table

  • Key values in the index are being scanned; shown as the Description value of rows with the Field value of spans

For more information, see Find the Indexes and Key Ranges a Query Uses.

Synopsis

EXPLAIN ( VERBOSE TYPES OPT DISTSQL VEC , ) preparable_stmt

Required privileges

The user requires the appropriate privileges for the statement being explained.

Parameters

Parameter Description
VERBOSE Show as much information as possible about the query plan.
TYPES Include the intermediate data types CockroachDB chooses to evaluate intermediate SQL expressions.
OPT Display the query plan tree generated by the cost-based optimizer.

To include cost details used by the optimizer in planning the query, use OPT, VERBOSE. To include cost and type details, use OPT, TYPES. To include all details used by the optimizer, including statistics, use OPT, ENV.
VEC Show detailed information about the vectorized execution plan for a query. If the table queried includes unsupported data types, an unhandled data type error is returned.
preparable_stmt The statement you want details about. All preparable statements are explainable.
DISTSQL Generate a URL to a distributed SQL physical query plan tree.

The physical query plan is encoded into a byte string after the fragment identifier (#) in the generated URL. The fragment is not sent to the web server; instead, the browser waits for the web server to return a decode.html resource, and then JavaScript on the web page decodes the fragment into a physical query plan diagram. The query plan is, therefore, not logged by a server external to the CockroachDB cluster and not exposed to the public internet.
Warning:

EXPLAIN also includes other modes besides query plans that are useful only to CockroachDB developers, which are not documented here.

Success responses

Successful EXPLAIN statements return tables with the following columns:

Column Description
Tree A tree representation of the hierarchy of the query plan.
Field The name of a property for the query plan.

The distributed and vectorized properties apply to the entire query plan. All other properties apply to the query plan node in the Tree column.
Description Additional information about the parameter in Field.
Columns The columns provided to the processes at lower levels of the hierarchy. Included in TYPES and VERBOSE output.
Ordering The order in which results are presented to the processes at each level of the hierarchy, as well as other properties of the result set at each level. Included in TYPES and VERBOSE output.

Examples

The following examples use the startrek example dataset. To follow along, you can use cockroach demo startrek to start a temporary, in-memory cluster with the startrek dataset preloaded.

Default query plans

By default, EXPLAIN includes the least detail about the query plan but can be useful to find out which indexes and index key ranges are used by a query. For example:

icon/buttons/copy
> EXPLAIN SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC;
    tree    |    field    |   description
+-----------+-------------+------------------+
            | distributed | true
            | vectorized  | false
  sort      |             |
   │        | order       | +season
   └── scan |             |
            | table       | episodes@primary
            | spans       | ALL
            | filter      | season > 3
(8 rows)

The tree column of the output shows the tree structure of the query plan, in this case a sort and then a scan.

The field and description columns describe a set of properties, some global to the query, and some specific to an operation listed in the tree column (in this case, sort or scan):

  • distributed:true
    The query plan will be distributed to multiple nodes on the cluster.
  • vectorized:false
    The plan will be executed with the row-oriented execution engine, and not the vectorized engine.
  • order:+season
    The sort will be ordered ascending on the season column.
  • table:episodes@primary
    The table is scanned on the primary index.
  • spans:ALL
    The table is scanned on all key ranges of the primary index (i.e., a full table scan). For more information on indexes and key ranges, see the example below.
  • filter: season > 3
    The scan filters on the season column.

If you run EXPLAIN on a join query, the output will display which type of join will be executed. For example, the following EXPLAIN output shows that the query will perform a hash join:

icon/buttons/copy
> EXPLAIN SELECT * FROM quotes AS q
JOIN episodes AS e ON q.episode = e.id;
    tree    |       field        |   description
+-----------+--------------------+------------------+
            | distributed        | true
            | vectorized         | false
  hash-join |                    |
   │        | type               | inner
   │        | equality           | (episode) = (id)
   │        | right cols are key |
   ├── scan |                    |
   │        | table              | quotes@primary
   │        | spans              | ALL
   └── scan |                    |
            | table              | episodes@primary
            | spans              | ALL
(12 rows)

And the following output shows that the query will perform a simple cross join:

icon/buttons/copy
> EXPLAIN SELECT * FROM quotes AS q
JOIN episodes AS e ON q.episode = '2';
          tree         |    field    |        description
+----------------------+-------------+---------------------------+
                       | distributed | true
                       | vectorized  | false
  render               |             |
   └── cross-join      |             |
        │              | type        | cross
        ├── scan       |             |
        │              | table       | episodes@primary
        │              | spans       | ALL
        └── index-join |             |
             │         | table       | quotes@primary
             │         | key columns | rowid
             └── scan  |             |
                       | table       | quotes@quotes_episode_idx
                       | spans       | /2-/3
(14 rows)

VERBOSE option

The VERBOSE option:

  • Includes SQL expressions that are involved in each processing stage, providing more granular detail about which portion of your query is represented at each level.
  • Includes detail about which columns are being used by each level, as well as properties of the result set on that level.
icon/buttons/copy
> EXPLAIN (VERBOSE) SELECT * FROM quotes AS q
JOIN episodes AS e ON q.episode = e.id
WHERE e.season = '1'
ORDER BY e.stardate ASC;

New in v19.2:: The output of EXPLAIN has been updated to show whether equality cols are key for lookup joins, which means that the lookup columns form a key in the target table such that each lookup has at most one result.

            tree           |         field         |        description        |                                         columns                                         | ordering
+--------------------------+-----------------------+---------------------------+-----------------------------------------------------------------------------------------+-----------+
                           | distributed           | true                      |                                                                                         |
                           | vectorized            | false                     |                                                                                         |
  render                   |                       |                           | (quote, characters, stardate, episode, id, season, num, title, stardate)                | +stardate
   │                       | render 0              | quote                     |                                                                                         |
   │                       | render 1              | characters                |                                                                                         |
   │                       | render 2              | stardate                  |                                                                                         |
   │                       | render 3              | episode                   |                                                                                         |
   │                       | render 4              | id                        |                                                                                         |
   │                       | render 5              | season                    |                                                                                         |
   │                       | render 6              | num                       |                                                                                         |
   │                       | render 7              | title                     |                                                                                         |
   │                       | render 8              | stardate                  |                                                                                         |
   └── lookup-join         |                       |                           | (id, season, num, title, stardate, episode, rowid[hidden], quote, characters, stardate) | +stardate
        │                  | table                 | quotes@primary            |                                                                                         |
        │                  | type                  | inner                     |                                                                                         |
        │                  | equality              | (rowid) = (rowid)         |                                                                                         |
        │                  | equality cols are key |                           |                                                                                         |
        └── lookup-join    |                       |                           | (id, season, num, title, stardate, episode, rowid[hidden])                              | +stardate
             │             | table                 | quotes@quotes_episode_idx |                                                                                         |
             │             | type                  | inner                     |                                                                                         |
             │             | equality              | (id) = (episode)          |                                                                                         |
             └── sort      |                       |                           | (id, season, num, title, stardate)                                                      | +stardate
                  │        | order                 | +stardate                 |                                                                                         |
                  └── scan |                       |                           | (id, season, num, title, stardate)                                                      |
                           | table                 | episodes@primary          |                                                                                         |
                           | spans                 | ALL                       |                                                                                         |
                           | filter                | season = 1                |                                                                                         |
(27 rows)

TYPES option

The TYPES mode includes the types of the values used in the query plan. It also includes the SQL expressions that were involved in each processing stage, and includes the columns used by each level.

icon/buttons/copy
> EXPLAIN (TYPES) SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC;
    tree    |    field    |           description            |                            columns                            | ordering
+-----------+-------------+----------------------------------+---------------------------------------------------------------+----------+
            | distributed | true                             |                                                               |
            | vectorized  | false                            |                                                               |
  sort      |             |                                  | (id int, season int, num int, title string, stardate decimal) | +season
   │        | order       | +season                          |                                                               |
   └── scan |             |                                  | (id int, season int, num int, title string, stardate decimal) |
            | table       | episodes@primary                 |                                                               |
            | spans       | ALL                              |                                                               |
            | filter      | ((season)[int] > (3)[int])[bool] |                                                               |
(8 rows)

OPT option

The OPT option displays the query plan tree generated by the cost-based optimizer. For example:

icon/buttons/copy
> EXPLAIN (OPT) SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC;
            text
+---------------------------+
  sort
   └── select
        ├── scan episodes
        └── filters
             └── season > 3
(5 rows)

To include cost details used by the optimizer in planning the query, use OPT, VERBOSE:

icon/buttons/copy
> EXPLAIN (OPT, VERBOSE) SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC;
              text
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  sort
   ├── columns: id:1 season:2 num:3 title:4 stardate:5
   ├── stats: [rows=26.3333333, distinct(1)=26.3333333, null(1)=0, distinct(2)=1, null(2)=0]
   ├── cost: 98.6419109
   ├── key: (1)
   ├── fd: (1)-->(2-5)
   ├── ordering: +2
   ├── prune: (1,3-5)
   ├── interesting orderings: (+1)
   └── select
        ├── columns: id:1 season:2 num:3 title:4 stardate:5
        ├── stats: [rows=26.3333333, distinct(1)=26.3333333, null(1)=0, distinct(2)=1, null(2)=0]
        ├── cost: 95.62
        ├── key: (1)
        ├── fd: (1)-->(2-5)
        ├── prune: (1,3-5)
        ├── interesting orderings: (+1)
        ├── scan episodes
        │    ├── columns: id:1 season:2 num:3 title:4 stardate:5
        │    ├── stats: [rows=79, distinct(1)=79, null(1)=0, distinct(2)=3, null(2)=0]
        │    │   histogram(1)=  0  1  0  1  0  1  0  1  0  1  0  1  0  1  0  1  0  1  0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1
        │    │                <--- 1 --- 2 --- 3 --- 4 --- 5 --- 6 --- 7 --- 8 --- 9 --- 10 --- 11 --- 12 --- 13 --- 14 --- 15 --- 16 --- 17 --- 18 --- 19 --- 20 --- 21 --- 22 --- 23 --- 24 --- 25 --- 26 --- 27 --- 28 --- 29 --- 30 --- 31 --- 32 --- 33 --- 34 --- 35 --- 36 --- 37 --- 38 --- 39 --- 40 --- 41 --- 42 --- 43 --- 44 --- 45 --- 46 --- 47 --- 48 --- 49 --- 50 --- 51 --- 52 --- 53 --- 54 --- 55 --- 56 --- 57 --- 58 --- 59 --- 60 --- 61 --- 62 --- 63 --- 64 --- 65 --- 66 --- 67 --- 68 --- 69 --- 70 --- 71 --- 72 --- 73 --- 74 --- 75 --- 76 --- 77 --- 78 --- 79
        │    ├── cost: 94.82
        │    ├── key: (1)
        │    ├── fd: (1)-->(2-5)
        │    ├── prune: (1-5)
        │    └── interesting orderings: (+1)
        └── filters
             └── season > 3 [outer=(2), constraints=(/2: [/4 - ]; tight)]
(29 rows)

To include cost and type details, use OPT, TYPES:

icon/buttons/copy
> EXPLAIN (OPT, TYPES) SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC;
                                                                                  text
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  sort
   ├── columns: id:1(int!null) season:2(int!null) num:3(int) title:4(string) stardate:5(decimal)
   ├── stats: [rows=26.3333333, distinct(1)=26.3333333, null(1)=0, distinct(2)=1, null(2)=0]
   ├── cost: 98.6419109
   ├── key: (1)
   ├── fd: (1)-->(2-5)
   ├── ordering: +2
   ├── prune: (1,3-5)
   ├── interesting orderings: (+1)
   └── select
        ├── columns: id:1(int!null) season:2(int!null) num:3(int) title:4(string) stardate:5(decimal)
        ├── stats: [rows=26.3333333, distinct(1)=26.3333333, null(1)=0, distinct(2)=1, null(2)=0]
        ├── cost: 95.62
        ├── key: (1)
        ├── fd: (1)-->(2-5)
        ├── prune: (1,3-5)
        ├── interesting orderings: (+1)
        ├── scan episodes
        │    ├── columns: id:1(int!null) season:2(int) num:3(int) title:4(string) stardate:5(decimal)
        │    ├── stats: [rows=79, distinct(1)=79, null(1)=0, distinct(2)=3, null(2)=0]
        │    │   histogram(1)=  0  1  0  1  0  1  0  1  0  1  0  1  0  1  0  1  0  1  0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1   0  1
        │    │                <--- 1 --- 2 --- 3 --- 4 --- 5 --- 6 --- 7 --- 8 --- 9 --- 10 --- 11 --- 12 --- 13 --- 14 --- 15 --- 16 --- 17 --- 18 --- 19 --- 20 --- 21 --- 22 --- 23 --- 24 --- 25 --- 26 --- 27 --- 28 --- 29 --- 30 --- 31 --- 32 --- 33 --- 34 --- 35 --- 36 --- 37 --- 38 --- 39 --- 40 --- 41 --- 42 --- 43 --- 44 --- 45 --- 46 --- 47 --- 48 --- 49 --- 50 --- 51 --- 52 --- 53 --- 54 --- 55 --- 56 --- 57 --- 58 --- 59 --- 60 --- 61 --- 62 --- 63 --- 64 --- 65 --- 66 --- 67 --- 68 --- 69 --- 70 --- 71 --- 72 --- 73 --- 74 --- 75 --- 76 --- 77 --- 78 --- 79
        │    ├── cost: 94.82
        │    ├── key: (1)
        │    ├── fd: (1)-->(2-5)
        │    ├── prune: (1-5)
        │    └── interesting orderings: (+1)
        └── filters
             └── gt [type=bool, outer=(2), constraints=(/2: [/4 - ]; tight)]
                  ├── variable: season [type=int]
                  └── const: 3 [type=int]
(31 rows)

To include all details used by the optimizer, including statistics, use OPT, ENV.

icon/buttons/copy
> EXPLAIN (OPT, ENV) SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC;
                                                                                                                                                                                                                                                                                                                                                                                                                           text                                                                                                                                                                                                                                                                                                                                                                                                                            
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  https://cockroachdb.github.io/text/decode.html#eJzEVcFum0wQPmefYsQl9v8DYQl1bCJVImTT0mIcwTatFUWIwLZZBbMWLE2jqlIewsc-nZ-kwjGUXNIeGpkDEt_HNzP7DTNcsLLiorDBFeltKZL05vQEXNeHr3iim7qhlamOYfBtPIpHlpYslznTsqS84wW2VLiueS7BNPDkABsH2AI8ts2xbVoqfBFYx6b-aoiQGxKHEqDOiU-ALXklMlbBAO3xDLyAjiGYUQg--L6K9iqWVKLYwo9QUS-ePEsucwYRDb3gTSeTSZklksEpcb2p47e4OwsiGjpeQEFZlnyRlPcKnIfe1Ann8J7MYcAzcCJ3qKK9M2fq-fPeewOeqfBYkApFvVBhk1qFNtsQDY8RcnxKwu3pGkaW7FZf1tc5T_XutF7wjrgUIupQL6KeG8H-JQIA-L65N5eSirxeFJViw2UHbgieKR1wpfYEJUsky-JEKjYoTRs0jDXDAoxt07TNkW5aE-PI_N8wbMNQesqMV5IXqYxTUReN-mjSY294JUWcijyW90vWxG787-uLZLHB4zippYjjJ1yd511co0eU4q6Xb4P_UP_ahMdGvLgRh8_6sGMPinrx4gaYz38JO3ZgM4G7noZdT8J2_by8DaN_b4P1BxvQ1f4xQuTTue94AQxm51QFElwMISJ-s0H_g7NwNv39G_n4loRku6XhNRzCLDwlIZzMW8yJ3GOkaZqGKlFKBOvVar16WK8eoGI5S-W2nPXqZwunSdHF79hW9JnnkpVVvyP9iG0Z6FcAAAD__yq9370=  
(1 row)

The output of EXPLAIN (OPT,ENV) is now a URL with the data encoded in the fragment portion. Opening the URL shows a page with the decoded data. This change makes it easier to share debugging information across different systems without encountering formatting issues.

Note that the data is processed in the local browser session and is never sent out over the network. Keep in mind that if you are using any browser extensions, they may be able to access the data locally.

When you visit the URL above you should see the following output in your browser.

Version: CockroachDB CCL v19.2.0-rc.1 (x86_64-apple-darwin14, built 2019/10/14 18:28:24, go1.12.5)

CREATE TABLE episodes (
    id INT8 NOT NULL,
    season INT8 NULL,
    num INT8 NULL,
    title STRING NULL,
    stardate DECIMAL NULL,
    CONSTRAINT "primary" PRIMARY KEY (id ASC),
    FAMILY "primary" (id, season, num, title, stardate)
);

ALTER TABLE startrek.public.episodes INJECT STATISTICS '[
    {
        "columns": [
            "id"
        ],
        "created_at": "2019-11-04 11:22:26.249072+00:00",
        "distinct_count": 79,
        "histo_col_type": "INT8",
        "name": "__auto__",
        "null_count": 0,
        "row_count": 79
    },
    {
        "columns": [
            "season"
        ],
        "created_at": "2019-11-04 11:22:26.249072+00:00",
        "distinct_count": 3,
        "histo_col_type": "",
        "name": "__auto__",
        "null_count": 0,
        "row_count": 79
    },
    {
        "columns": [
            "num"
        ],
        "created_at": "2019-11-04 11:22:26.249072+00:00",
        "distinct_count": 29,
        "histo_col_type": "",
        "name": "__auto__",
        "null_count": 0,
        "row_count": 79
    },
    {
        "columns": [
            "title"
        ],
        "created_at": "2019-11-04 11:22:26.249072+00:00",
        "distinct_count": 79,
        "histo_col_type": "",
        "name": "__auto__",
        "null_count": 0,
        "row_count": 79
    },
    {
        "columns": [
            "stardate"
        ],
        "created_at": "2019-11-04 11:22:26.249072+00:00",
        "distinct_count": 76,
        "histo_col_type": "",
        "name": "__auto__",
        "null_count": 4,
        "row_count": 79
    }
]';

EXPLAIN (OPT, ENV) SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC;
----
sort
 └── select
      ├── scan episodes
      └── filters
           └── season > 3

VEC option

The VEC option shows details about the vectorized execution plan for the query.

icon/buttons/copy
> EXPLAIN (VEC) SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC;
                  text
+---------------------------------------+
  │
  â”” Node 1
    â”” *colexec.sortOp
      â”” *colexec.selGTInt64Int64ConstOp
        â”” *colexec.colBatchScan
(5 rows)

The output shows the different internal functions that will be used to process each batch of column-oriented data.

DISTSQL option

The DISTSQL option generates a URL for a physical query plan that provides high level information about how a query will be executed. For details about reading the physical query plan, see DistSQL Plan Viewer. For more information about distributed SQL queries, see the DistSQL section of our SQL Layer Architecture docs.

Note:

The physical query plan is encoded into a byte string after the fragment identifier (#) in the generated URL. The fragment is not sent to the web server; instead, the browser waits for the web server to return a decode.html resource, and then JavaScript on the web page decodes the fragment into a physical query plan diagram. The query plan is, therefore, not logged by a server external to the CockroachDB cluster and not exposed to the public internet.

For example, the following EXPLAIN(DISTSQL) statement generates a physical plan for a simple query against the TPC-H database loaded to a 3-node CockroachDB cluster:

icon/buttons/copy
> EXPLAIN (DISTSQL) SELECT l_shipmode, AVG(l_extendedprice) FROM lineitem GROUP BY l_shipmode;
 automatic |                      url
-----------+----------------------------------------------
   true    | https://cockroachdb.github.io/distsqlplan...

To view the DistSQL Plan Viewer, point your browser to the URL provided:

EXPLAIN (DISTSQL)

Find the indexes and key ranges a query uses

You can use EXPLAIN to understand which indexes and key ranges queries use, which can help you ensure a query isn't performing a full table scan.

icon/buttons/copy
> CREATE TABLE kv (k INT PRIMARY KEY, v INT);

Because column v is not indexed, queries filtering on it alone scan the entire table:

icon/buttons/copy
> EXPLAIN SELECT * FROM kv WHERE v BETWEEN 4 AND 5;
  tree |    field    |      description
+------+-------------+-----------------------+
       | distributed | true
       | vectorized  | false
  scan |             |
       | table       | kv@primary
       | spans       | ALL
       | filter      | (v >= 4) AND (v <= 5)
(6 rows)

If there were an index on v, CockroachDB would be able to avoid scanning the entire table:

icon/buttons/copy
> CREATE INDEX v ON kv (v);
icon/buttons/copy
> EXPLAIN SELECT * FROM kv WHERE v BETWEEN 4 AND 5;
  tree |    field    | description
+------+-------------+-------------+
       | distributed | false
       | vectorized  | false
  scan |             |
       | table       | kv@v
       | spans       | /4-/6
(5 rows)

Now, only part of the index v is getting scanned, specifically the key range starting at (and including) 4 and stopping before 6. Also note that this query plan is not distributed across nodes on the cluster.

See also


Yes No
On this page

Yes No