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.
Syntax
1 2 3 |
EXPLAIN PLAN [ SET STATEMENT_ID = name ] FOR statement ; |
Parameters
- name
Specifies a query tag.
Value range: a string.
- 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.
If SET STATEMENT_ID is not specified when the EXPLAIN PLAN statement is executed, STATEMENT_ID is left empty by default. In addition, the value of STATEMENT_ID cannot exceed 30 bytes. Otherwise, an error will be reported.
Precautions
- EXPLAIN PLAN cannot be executed on a DN.
- Plan information cannot be collected for SQL statements that failed to be 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_TABLE cannot be joined with GDS foreign tables.
- For a query that cannot be pushed down, object information cannot be collected and only such information as REMOTE_QUERY and CTE can be collected. For details, see Example 2.
Example 1
You can perform the following steps to collect execution plans of SQL statements by running EXPLAIN PLAN:
- Run the EXPLAIN PLAN statement.
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.
1 2 3 4 5 6
-- Create tables foo1 and foo2. openGauss=# CREATE TABLE foo1(f1 int, f2 text, f3 text[]); openGauss=# CREATE TABLE foo2(f1 int, f2 text, f3 text[]); -- Run EXPLAIN PLAN. openGauss=# 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.
openGauss=# SELECT * FROM plan_table;
- Delete data from PLAN_TABLE.
1 2 3
openGauss=# DELETE FROM plan_table WHERE STATEMENT_ID = 'TPCH-Q4'; openGauss=# DROP TABLE foo1; openGauss=# DROP TABLE foo2;
Example 2
For a query that cannot be pushed down, only such information as REMOTE_QUERY and CTE can be collected from PLAN_TABLE after EXPLAIN PLAN is executed.
1 2 3 4 5 |
explain plan set statement_id = 'test remote query' for select current_user from customer; |
1
|
SELECT * FROM PLAN_TABLE; |
Scenario 2: For a query with WITH RECURSIVE that cannot be pushed down, only CTE can be collected.
1
|
set enable_stream_recursive = off; |
1 2 3 4 5 6 7 8 9 10 |
explain plan set statement_id = 'cte can not be push down' for with recursive rq as ( select id, name from chinamap where id = 11 union all select origin.id, rq.name || ' > ' || origin.name from rq join chinamap origin on origin.pid = rq.id ) select id, name from rq order by 1; |
1
|
SELECT * FROM PLAN_TABLE; |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.