EXPLAIN

Function

EXPLAIN shows the execution plan of an SQL statement.

The execution plan shows how the tables referenced by the SQL statement will be scanned, for example, by plain sequential scan or index scan. If multiple tables are referenced, the execution plan also shows what join algorithms will be used to bring together the required rows from each input table.

The most critical part of the display is the estimated statement execution cost, which is the planner's guess at how long it will take to run the statement.

The ANALYZE option causes the statement to be executed, not only planned. Then actual runtime statistics are added to the display, including the total elapsed time expended within each plan node (in milliseconds) and the total number of rows it actually returned. This is useful to check whether the planner's estimates are close to reality.

Precautions

The statement is executed when the ANALYZE option is used. To use EXPLAIN ANALYZE on an INSERT, UPDATE, DELETE, CREATE TABLE AS, or EXECUTE statement without letting the command affect your data, use this approach:

1
2
3
START TRANSACTION;
EXPLAIN ANALYZE ...;
ROLLBACK;

Syntax

Parameter Description

Examples

Create the tpcds.customer_address_p1 table.

1
CREATE TABLE tpcds.customer_address_p1 AS TABLE tpcds.customer_address;

Change the value of explain_perf_mode to normal.

1
SET explain_perf_mode=normal;

Display an execution plan for simple queries in the table.

1
2
3
4
5
6
EXPLAIN SELECT * FROM tpcds.customer_address_p1;
			       QUERY PLAN
----------------------------------------------------------------------------
 Data Node Scan on "__REMOTE_FQS_QUERY__"  (cost=0.00..0.00 rows=0 width=0)
   Node/s: All datanodes
(2 rows)

Generate an execution plan in JSON format (assume explain_perf_mode is set to normal).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
EXPLAIN(FORMAT JSON) SELECT * FROM tpcds.customer_address_p1;
                    QUERY PLAN
---------------------------------------------------
 [                                                +
   {                                              +
     "Plan": {                                    +
       "Node Type": "Data Node Scan",             +
       "RemoteQuery name": "__REMOTE_FQS_QUERY__",+
       "Alias": "__REMOTE_FQS_QUERY__",           +
       "Startup Cost": 0.00,                      +
       "Total Cost": 0.00,                        +
       "Plan Rows": 0,                            +
       "Plan Width": 0,                           +
       "Nodes": "All datanodes"                   +
     }                                            +
   }                                              +
 ]
(1 row)

If there is an index and we use a query with an indexable WHERE condition, EXPLAIN might show a different pla.

1
2
3
4
5
6
EXPLAIN SELECT * FROM tpcds.customer_address_p1 WHERE ca_address_sk=10000;
                                  QUERY PLAN
------------------------------------------------------------------------------
 Data Node Scan on "__REMOTE_LIGHT_QUERY__"  (cost=0.00..0.00 rows=0 width=0)
   Node/s: datanode2
(2 rows)

Generate an execution plan in YAML format (assume explain_perf_mode is set to normal).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
EXPLAIN(FORMAT YAML) SELECT * FROM tpcds.customer_address_p1 WHERE ca_address_sk=10000;
                   QUERY PLAN
------------------------------------------------
 - Plan:                                       +
     Node Type: "Data Node Scan"               +
     RemoteQuery name: "__REMOTE_LIGHT_QUERY__"+
     Alias: "__REMOTE_LIGHT_QUERY__"           +
     Startup Cost: 0.00                        +
     Total Cost: 0.00                          +
     Plan Rows: 0                              +
     Plan Width: 0                             +
     Nodes: "datanode2"
(1 row)

Here is an example of an execution plan with cost estimates suppressed.

1
2
3
4
5
6
EXPLAIN(COSTS FALSE)SELECT * FROM tpcds.customer_address_p1 WHERE ca_address_sk=10000;
                 QUERY PLAN
--------------------------------------------
 Data Node Scan on "__REMOTE_LIGHT_QUERY__"
   Node/s: datanode2
(2 rows)

Here is an example of an execution plan for a query that uses an aggregate function.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
EXPLAIN SELECT SUM(ca_address_sk) FROM tpcds.customer_address_p1 WHERE ca_address_sk<10000;
                                      QUERY PLAN                                       
---------------------------------------------------------------------------------------
 Aggregate  (cost=18.19..14.32 rows=1 width=4)
   ->  Streaming (type: GATHER)  (cost=18.19..14.32 rows=3 width=4)
         Node/s: All datanodes
         ->  Aggregate  (cost=14.19..14.20 rows=3 width=4)
               ->  Seq Scan on customer_address_p1  (cost=0.00..14.18 rows=10 width=4)
                     Filter: (ca_address_sk < 10000)
(6 rows)

-- Delete the tpcds.customer_address_p1 table.

1
DROP TABLE tpcds.customer_address_p1;

Helpful Links

ANALYZE | ANALYSE