EXPLAIN

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.

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(VERBOSETYPESOPTDISTSQL,)preparable_stmt

Required privileges

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

Parameters

ParameterDescription
VERBOSEShow as much information as possible about the query plan.
TYPESInclude the intermediate data types CockroachDB chooses to evaluate intermediate SQL expressions.
OPTDisplay a query plan tree if the query will be run with the cost-based optimizer. If it returns an "unsupported statement" error, the query will not be run with the cost-based optimizer and will be run with the heuristic planner.New in v19.1: 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.
DISTSQLGenerate a link to a distributed SQL physical query plan tree.
preparable_stmtThe statement you want details about. All preparable statements are explainable.

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:

ColumnDescription
TreeA tree representation showing the hierarchy of the query plan.
FieldThe name of a parameter relevant to the query plan node immediately above.
DescriptionAdditional information for the parameter in Field.
ColumnsThe columns provided to the processes at lower levels of the hierarchy. Included in TYPES and VERBOSE output.
OrderingThe 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

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:

  1. > EXPLAIN SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC;
  1. tree | field | description
  2. +-----------+--------+------------------+
  3. sort | |
  4. | order | +season
  5. └── scan | |
  6. | table | episodes@primary
  7. | spans | ALL
  8. | filter | season > 3
  9. (6 rows)

The first column shows the tree structure of the query plan; a set of properties is displayed for each node in the tree. Most importantly, for scans, you can see the index that is scanned (primary in this case) and what key ranges of the index you are scanning (in this case, a full table scan). For more information on indexes and key ranges, see the example below.

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.
  1. > EXPLAIN (VERBOSE) SELECT * FROM quotes AS q \
  2. JOIN episodes AS e ON q.episode = e.id \
  3. WHERE e.season = '1' \
  4. ORDER BY e.stardate ASC;
  1. tree | field | description | columns | ordering
  2. +----------------+--------------------+------------------+--------------------------------------------------------------------------+-----------+
  3. sort | | | (quote, characters, stardate, episode, id, season, num, title, stardate) | +stardate
  4. | order | +stardate | |
  5. └── hash-join | | | (quote, characters, stardate, episode, id, season, num, title, stardate) |
  6. | type | inner | |
  7. | equality | (episode) = (id) | |
  8. | right cols are key | | |
  9. ├── scan | | | (quote, characters, stardate, episode) |
  10. | table | quotes@primary | |
  11. | spans | ALL | |
  12. └── scan | | | (id, season, num, title, stardate) |
  13. | table | episodes@primary | |
  14. | spans | ALL | |
  15. | filter | season = 1 | |
  16. (13 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.

  1. > EXPLAIN (TYPES) SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC;
  1. tree | field | description | columns | ordering
  2. +-----------+--------+----------------------------------+---------------------------------------------------------------+----------+
  3. sort | | | (id int, season int, num int, title string, stardate decimal) | +season
  4. | order | +season | |
  5. └── scan | | | (id int, season int, num int, title string, stardate decimal) |
  6. | table | episodes@primary | |
  7. | spans | ALL | |
  8. | filter | ((season)[int] > (3)[int])[bool] | |
  9. (6 rows)

OPT option

The OPT option displays a query plan tree, if the query will be run with the cost-based optimizer. If it returns an "unsupported statement" error, the query will not be run with the cost-based optimizer and will be run with the legacy heuristic planner.

For example, the following query returns the query plan tree, which means that it will be run with the cost-based optimizer:

  1. > EXPLAIN (OPT) SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC;
  1. text
  2. +---------------------------+
  3. sort
  4. └── select
  5. ├── scan episodes
  6. └── filters
  7. └── season > 3
  8. (5 rows)

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

  1. > EXPLAIN (OPT, VERBOSE) SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC;
  1. text
  2. +----------------------------------------------------------------------------------------------------------+
  3. sort
  4. ├── columns: id:1 season:2 num:3 title:4 stardate:5
  5. ├── stats: [rows=26.3333333, distinct(1)=26.3333333, null(1)=0, distinct(2)=2.99993081, null(2)=0]
  6. ├── cost: 90.7319109
  7. ├── key: (1)
  8. ├── fd: (1)-->(2-5)
  9. ├── ordering: +2
  10. ├── prune: (1,3-5)
  11. └── select
  12. ├── columns: id:1 season:2 num:3 title:4 stardate:5
  13. ├── stats: [rows=26.3333333, distinct(1)=26.3333333, null(1)=0, distinct(2)=2.99993081, null(2)=0]
  14. ├── cost: 87.71
  15. ├── key: (1)
  16. ├── fd: (1)-->(2-5)
  17. ├── prune: (1,3-5)
  18. ├── scan episodes
  19. ├── columns: id:1 season:2 num:3 title:4 stardate:5
  20. ├── stats: [rows=79, distinct(1)=79, null(1)=0, distinct(2)=3, null(2)=0]
  21. ├── cost: 86.91
  22. ├── key: (1)
  23. ├── fd: (1)-->(2-5)
  24. └── prune: (1-5)
  25. └── filters
  26. └── season > 3 [outer=(2), constraints=(/2: [/4 - ]; tight)]
  27. (24 rows)

New in v19.1: To include cost and type details, use OPT, TYPES:

  1. > EXPLAIN (OPT, TYPES) SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC;
  1. text
  2. +----------------------------------------------------------------------------------------------------------+
  3. sort
  4. ├── columns: id:1(int!null) season:2(int!null) num:3(int) title:4(string) stardate:5(decimal)
  5. ├── stats: [rows=26.3333333, distinct(1)=26.3333333, null(1)=0, distinct(2)=2.99993081, null(2)=0]
  6. ├── cost: 90.7319109
  7. ├── key: (1)
  8. ├── fd: (1)-->(2-5)
  9. ├── ordering: +2
  10. ├── prune: (1,3-5)
  11. └── select
  12. ├── columns: id:1(int!null) season:2(int!null) num:3(int) title:4(string) stardate:5(decimal)
  13. ├── stats: [rows=26.3333333, distinct(1)=26.3333333, null(1)=0, distinct(2)=2.99993081, null(2)=0]
  14. ├── cost: 87.71
  15. ├── key: (1)
  16. ├── fd: (1)-->(2-5)
  17. ├── prune: (1,3-5)
  18. ├── scan episodes
  19. ├── columns: id:1(int!null) season:2(int) num:3(int) title:4(string) stardate:5(decimal)
  20. ├── stats: [rows=79, distinct(1)=79, null(1)=0, distinct(2)=3, null(2)=0]
  21. ├── cost: 86.91
  22. ├── key: (1)
  23. ├── fd: (1)-->(2-5)
  24. └── prune: (1-5)
  25. └── filters
  26. └── gt [type=bool, outer=(2), constraints=(/2: [/4 - ]; tight)]
  27. ├── variable: season [type=int]
  28. └── const: 3 [type=int]
  29. (26 rows)

New in v19.1: To include all details used by the optimizer, including statistics, use OPT, ENV:

  1. > EXPLAIN (OPT, ENV) SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC;
  1. text
  2. +-------------------------------------------------------------------------------------------------------------------------------+
  3. Version: CockroachDB CCL v19.1.0-beta.20190318-377-gc45b9a400f (x86_64-apple-darwin17.7.0, built 2019/03/26 19:46:42, go1.11)
  4. CREATE TABLE episodes (
  5. id INT8 NOT NULL,
  6. season INT8 NULL,
  7. num INT8 NULL,
  8. title STRING NULL,
  9. stardate DECIMAL NULL,
  10. CONSTRAINT "primary" PRIMARY KEY (id ASC),
  11. FAMILY "primary" (id, season, num, title, stardate)
  12. );
  13. ALTER TABLE startrek.public.episodes INJECT STATISTICS '[
  14. {
  15. "columns": [
  16. "id"
  17. ],
  18. "created_at": "2019-03-26 19:49:53.18699+00:00",
  19. "distinct_count": 79,
  20. "histo_col_type": "",
  21. "name": "__auto__",
  22. "null_count": 0,
  23. "row_count": 79
  24. },
  25. {
  26. "columns": [
  27. "season"
  28. ],
  29. "created_at": "2019-03-26 19:49:53.18699+00:00",
  30. "distinct_count": 3,
  31. "histo_col_type": "",
  32. "name": "__auto__",
  33. "null_count": 0,
  34. "row_count": 79
  35. },
  36. {
  37. "columns": [
  38. "num"
  39. ],
  40. "created_at": "2019-03-26 19:49:53.18699+00:00",
  41. "distinct_count": 29,
  42. "histo_col_type": "",
  43. "name": "__auto__",
  44. "null_count": 0,
  45. "row_count": 79
  46. },
  47. {
  48. "columns": [
  49. "title"
  50. ],
  51. "created_at": "2019-03-26 19:49:53.18699+00:00",
  52. "distinct_count": 79,
  53. "histo_col_type": "",
  54. "name": "__auto__",
  55. "null_count": 0,
  56. "row_count": 79
  57. },
  58. {
  59. "columns": [
  60. "stardate"
  61. ],
  62. "created_at": "2019-03-26 19:49:53.18699+00:00",
  63. "distinct_count": 75,
  64. "histo_col_type": "",
  65. "name": "__auto__",
  66. "null_count": 4,
  67. "row_count": 79
  68. }
  69. ]';
  70. EXPLAIN (OPT, ENV) SELECT * FROM episodes WHERE season > 3 ORDER BY season ASC;
  71. ----
  72. sort
  73. └── select
  74. ├── scan episodes
  75. └── filters
  76. └── season > 3
  77. (77 rows)

DISTSQL option

The DISTSQL option generates a physical query plan for a query. Query plans provide information around SQL execution, which can be used to troubleshoot slow queries. For more information about distributed SQL queries, see the DistSQL section of our SQL Layer Architecture docs.

EXPLAIN (DISTSQL) generates a physical query plan that provides high level information about how a query will be executed:

  1. > EXPLAIN (DISTSQL) SELECT l_shipmode, AVG(l_extendedprice) FROM lineitem GROUP BY l_shipmode;
  1. automatic | url
  2. -----------+----------------------------------------------
  3. 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.

  1. > CREATE TABLE kv (k INT PRIMARY KEY, v INT);

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

  1. > EXPLAIN SELECT * FROM kv WHERE v BETWEEN 4 AND 5;
  1. tree | field | description
  2. +------+--------+-----------------------+
  3. scan | |
  4. | table | kv@primary
  5. | spans | ALL
  6. | filter | (v >= 4) AND (v <= 5)
  7. (4 rows)

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

  1. > CREATE INDEX v ON kv (v);
  1. > EXPLAIN SELECT * FROM kv WHERE v BETWEEN 4 AND 5;
  1. tree | field | description
  2. ------+-------+-------------
  3. scan | |
  4. | table | kv@v
  5. | spans | /4-/6
  6. (3 rows)

Now, only part of the index v is getting scanned, specifically the key range starting at (and including) 4 and stopping before 6.

See also

Was this page helpful?
YesNo