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 customer_address_p1 table:

1
CREATE TABLE customer_address_p1 AS TABLE 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
EXPLAIN SELECT * FROM customer_address_p1;

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

1
EXPLAIN(FORMAT JSON) SELECT * FROM customer_address_p1;

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

1
EXPLAIN SELECT * FROM customer_address_p1 WHERE ca_address_sk=10000;

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

1
EXPLAIN(FORMAT YAML) SELECT * FROM customer_address_p1 WHERE ca_address_sk=10000;

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

1
EXPLAIN(COSTS FALSE)SELECT * FROM customer_address_p1 WHERE ca_address_sk=10000;

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

1
EXPLAIN SELECT SUM(ca_address_sk) FROM customer_address_p1 WHERE ca_address_sk<10000;

Delete the customer_address_p1 table:

1
DROP TABLE customer_address_p1;

Helpful Links

ANALYZE | ANALYSE