As described in SQL Execution Plan, EXPLAIN displays the execution plan, but will not actually run SQL statements. EXPLAIN ANALYZE and EXPLAIN PERFORMANCE both will actually run SQL statements and return the execution information. In this section, detailed execution plan and execution information are described.
The following SQL statement is used as an example:
select cjxh, count(1) from dwcjk group by cjxh;
Run the EXPLAIN command and the output is as follows:
Interpretation of the execution plan column (horizontal):
The operator of the Vector prefix refers to a vectorized execution engine operator, which exists in a query containing a column-store table.
Interpretation of the execution plan level (vertical):
The table scan operator scans the table dwcjk using Cstore Scan. The function of this layer is to read data in the table dwcjk from the buffer or disks, or transfers it to the upper-layer node to participate in the calculation.
Aggregation operators are used to perform aggregation operations (group by) on operators calculated from the lower layer.
The GATHER-typed Shuffle operator aggregates data from DNs to the CN.
Storage format conversion operator is used to convert data in columns of the memory to data in rows for client display.
It should be noted that when operators in the top layer are Data Node Scan, you need to set enable_fast_query_shipping to off to view detailed execution plan as follows:
explain select cjxh, count(1) from dwcjk group by cjxh; QUERY PLAN -------------------------------------------------- Data Node Scan (cost=0.00..0.00 rows=0 width=0) Node/s: All datanodes (2 rows)
After enable_fast_query_shipping is set, the execution plan is displayed as follows:
Keywords in the execution plan:
Scans all rows of the table in sequence.
The optimizer uses a two-step plan: the child plan node visits an index to find the locations of rows matching the index condition, and then the upper plan node actually fetches those rows from the table itself. Fetching rows separately is much more expensive than reading them sequentially, but because not all pages of the table have to be visited, this is still cheaper than a sequential scan. The upper-layer planning node first sort the location of index identifier rows based on physical locations before reading them. This minimizes the independent capturing overhead.
If there are separate indexes on multiple columns referenced in WHERE, the optimizer might choose to use an AND or OR combination of the indexes. However, this requires the visiting of both indexes, so it is not necessarily a win compared to using just one index and treating the other condition as a filter.
The following Index scans featured with different sorting mechanisms are involved:
Fetches data pages using a bitmap.
Fetches table rows in index order, which makes them even more expensive to read. However, there are so few rows that the extra cost of sorting the row locations is unnecessary. This plan type is used mainly for queries fetching just a single row and queries having an ORDER BY condition that matches the index order, because no extra sorting step is needed to satisfy ORDER BY.
Nested-loop is used for queries that have a smaller data set connected. In a Nested-loop join, the foreign table drives the internal table and each row returned from the foreign table should have a matching row in the internal table. The returned result set of all queries should be less than 10,000. The table that returns a smaller subset will work as a foreign table, and indexes are recommended for connection fields of the internal table.
A Hash join is used for large tables. The optimizer uses a hash join, in which rows of one table are entered into an in-memory hash table, after which the other table is scanned and the hash table is probed for matches to each row.
In most cases, the execution performance of a Merge join is lower than that of a Hash join. However, if the source data has been pre-sorted and no more sorting is needed during the Merge join, its performance excels.
Sorts the result set.
The EXPLAIN output shows the WHERE clause being applied as a Filter condition attached to the Seq Scan plan node. This means that the plan node checks the condition for each row it scans, and returns only the ones that meet the condition. The estimated number of output rows has been reduced because of the WHERE clause. However, the scan will still have to visit all 10000 rows. As a result, the cost is not decreased. It increases a bit (by 10000 x cpu_operator_cost) to reflect the extra CPU time spent on checking the WHERE condition.
LIMIT limits the number of output execution results. If a LIMIT condition is added, not all rows are retrieved.
You can use EXPLAIN ANALYZE or EXPLAIN PERFORMANCE to check the SQL statement execution information and compare the actual execution and the optimizer's estimation to find what to optimize. EXPLAIN PERFORMANCE provides the execution information on each DN, whereas EXPLAIN ANALYZE does not.
The following SQL statement is used as an example:
select count(1) from dwcjk group by cjxh;
The output of running EXPLAIN PERFORMANCE is as follows:
This figure shows that the execution information can be classified into the following 7 aspects.
This part displays the static information that does not change during the plan execution process, such as some join conditions and filter information.
This part displays the memory usage information printed by certain operators (mainly Hash and Sort), including the peak memory, control memory, operator memory, width, auto spread num, and early spilled.
This part displays the target columns provided by each operator.
The execution time, CPU, and buffer usage of each operator are printed in this part.
This part displays CNs and DNs, DN and DN connection time, and some execution information in the storage layer.
The total execution time and network traffic, including the maximum and minimum execution time in the initialization and end phases on each DN, initialization, execution, and time in the end phase on each CN, and the system available memory during the current statement execution, and statement estimation memory information.
Generally, the output from EXPLAIN ANALYZE or EXPLAIN PERFORMANCE is provided for the input for further optimization.