Updated on 2024-08-20 GMT+08:00

EXPLAIN PLAN

Description

EXPLAIN PLAN saves information about an execution plan into the PLAN_TABLE table. Different from the EXPLAIN statement, EXPLAIN PLAN only saves plan information and does not print information on the screen.

Precautions

  • The database node cannot collect plan information about SQL statements that are incorrectly executed.
  • Data in PLAN_TABLE is in a session-level lifecycle. Sessions are isolated from users, and therefore users can only view the data of the current session and current user.
  • PLAN: saves plan information into PLAN_TABLE. If information is stored successfully, "EXPLAIN SUCCESS" is returned.
  • STATEMENT_ID: tags each query. The tag information will be stored in PLAN_TABLE.
  • After the EXPLAIN PLAN statement is executed, plan information is automatically stored in PLAN_TABLE. INSERT, UPDATE, and ANALYZE cannot be performed on PLAN_TABLE. For details about PLAN_TABLE, see PLAN_TABLE.

Syntax

EXPLAIN PLAN
[ SET STATEMENT_ID = name ]
FOR statement ;

Parameters

  • name

    Specifies a query tag.

    Value range: a string

    If the EXPLAIN PLAN statement does not contain SET STATEMENT_ID, STATEMENT_ID is empty by default. In addition, the value of STATEMENT_ID cannot exceed 30 bytes. Otherwise, an error will be reported.

  • statement

    Specifies the SQL statement to explain.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
-- Create tables foo1 and foo2.
gaussdb=# CREATE TABLE foo1(f1 int, f2 text, f3 text[]);
gaussdb=# CREATE TABLE foo2(f1 int, f2 text, f3 text[]);

-- Run EXPLAIN PLAN.
gaussdb=# EXPLAIN PLAN SET STATEMENT_ID = 'TPCH-Q4' FOR SELECT f1, count(*) FROM foo1 WHERE f1 > 1 AND f1 < 3 AND EXISTS (SELECT * FROM foo2) GROUP BY f1;

-- Query PLAN_TABLE.
gaussdb=# SELECT * FROM plan_table;

-- Delete data from the PLAN_TABLE table and delete tables foo1 and foo2.
gaussdb=# DELETE FROM plan_table WHERE STATEMENT_ID = 'TPCH-Q4';
gaussdb=# DROP TABLE foo1;
gaussdb=# DROP TABLE foo2;