EXPLAIN

Updated at:

In MaxCompute SQL, you can use the EXPLAIN statement to analyze performance bottlenecks in query statements or table schemas. This topic describes the function, syntax, and examples of the EXPLAIN statement.

Function introduction

The EXPLAIN statement displays the structure of the execution plan for a MaxCompute SQL Data Manipulation Language (DML) statement. An execution plan is the program that executes the semantics of the SQL statement. The execution plan helps you understand how a SQL statement is processed and provides guidance for SQL optimization. A query statement can correspond to one or more jobs, and each job can correspond to one or more tasks.

Note

If a query statement is too complex, the EXPLAIN result may exceed the 4 MB API limit. This prevents you from retrieving the complete execution plan. In this case, you can split the query statement into smaller parts. Then, you can run the EXPLAIN statement on each part to understand the job structure.

Syntax

EXPLAIN <dml query>;

dml query: Required. A SELECT statement. For more information, see SELECT syntax.

Returns

The output of an EXPLAIN statement includes the following information:

  • Job dependency

    For example, job0 is root job. If a query requires only one job (job0), only one line of information is displayed.

  • Task dependency

    In Job job0:
    root Tasks: M1, M2
    J3_1_2_Stg1 depends on: M1, M2

    job0 contains three tasks: M1, M2, and J3_1_2_Stg1. The system first executes the M1 and M2 tasks. After they are complete, the system executes J3_1_2_Stg1.

    The task naming convention is as follows:

    • Task types: MapTask (M), ReduceTask (R), JoinTask (J), and LocalWork (L).

      Note

      The first letter of a task name indicates its type. For example, M2Stg1 is a MapTask.

    • The number immediately following the first letter is the task ID. This ID is unique for the current query.

    • Numbers separated by underscores (_) represent the direct dependencies of the current task. For example, J3_1_2_Stg1 indicates that the current task ID is 3 and it depends on two tasks with IDs 1 (M1) and 2 (M2).

  • The operator dependency structure within a task.

    An operator string describes the execution semantics of a task. The following code shows an example of the structure:

    In Task M2:
        Data source: mf_mc_bj.sale_detail_jt/sale_date=2013/region=china  # "Data source" describes the input of the current task.
        TS: mf_mc_bj.sale_detail_jt/sale_date=2013/region=china           # TableScanOperator
            FIL: ISNOTNULL(customer_id)                                   # FilterOperator
                RS: order: +                                              # ReduceSinkOperator
                    nullDirection: *
                    optimizeOrderBy: False
                    valueDestLimit: 0
                    dist: HASH
                    keys:
                          customer_id
                    values:
                          customer_id (string)
                          total_price (double)
                    partitions:
                          customer_id
    
    
    In Task J3_1_2:
        JOIN:                                                           # JoinOperator
             StreamLineRead1 INNERJOIN StreamLineRead2
             keys:
                 0:customer_id
                 1:customer_id
    
            AGGREGATE: group by:customer_id                            # GroupByOperator
             UDAF: SUM(total_price) (__agg_0_sum)[Complete],SUM(total_price) (__agg_1_sum)[Complete]
                RS: order: +
                    nullDirection: *
                    optimizeOrderBy: True
                    valueDestLimit: 10
                    dist: HASH
                    keys:
                          customer_id
                    values:
                          customer_id (string)
                          __agg_0 (double)
                          __agg_1 (double)
                    partitions:
    
    
    In Task R4_3:
        SEL: customer_id,__agg_0,__agg_1                               # SelectOperator
            LIM:limit 10                                               # LimitOperator
                FS: output: Screen                                     # FileSinkOperator
                    schema:
                      customer_id (string) AS ashop
                      __agg_0 (double) AS ap
                      __agg_1 (double) AS bp

    The operators are described as follows:

    • TableScanOperator (TS): Describes the logic of the FROM clause in the query statement. The EXPLAIN output shows the name (alias) of the input table.

    • SelectOperator (SEL): Describes the logic of the SELECT clause in the query statement. The EXPLAIN output shows the columns that are passed to the next operator, separated by commas.

      • A column reference is displayed as <alias>.<column_name>.

      • The result of an expression is displayed in a function format, such as func1(arg1_1, arg1_2, func2(arg2_1, arg2_2)).

      • A constant is displayed directly as its value.

    • FilterOperator (FIL): Describes the logic of the WHERE clause in the query statement. The EXPLAIN output shows a WHERE conditional expression, which is displayed in a similar way to the SelectOperator.

    • JoinOperator (JOIN): Describes the logic of the JOIN clause in the query statement. The EXPLAIN output shows which tables are joined and how they are joined.

    • GroupByOperator (for example, AGGREGATE): Describes the logic of an aggregate operation. This structure appears if the query uses aggregate functions. The EXPLAIN output shows the content of the aggregate functions.

    • ReduceSinkOperator (RS): Describes the logic of data distribution between tasks. If the output of the current task is passed to another task, a ReduceSinkOperator must be used at the end of the current task to perform data distribution. The EXPLAIN output shows the sorting method of the output, the distribution key and value, and the columns that are used to calculate the hash value.

    • FileSinkOperator (FS): Describes the final data storage operation. If the query contains an INSERT clause, the EXPLAIN output shows the name of the destination table.

    • LimitOperator (LIM): Describes the logic of the LIMIT clause in the query statement. The EXPLAIN output shows the LIMIT number.

    • MapjoinOperator (HASHJOIN): Similar to JoinOperator, this operator describes the JOIN operation for large tables.

Sample data

Sample source data is provided for you to better understand the examples in this topic. The following statements show how to create the sale_detail and sale_detail_jt tables and insert data into the tables.

-- Create two partitioned tables named sale_detail and sale_detail_jt. 
CREATE TABLE if NOT EXISTS sale_detail
(
shop_name     STRING,
customer_id   STRING,
total_price   DOUBLE
)
PARTITIONED BY (sale_date STRING, region STRING);

CREATE TABLE if NOT EXISTS sale_detail_jt
(
shop_name     STRING,
customer_id   STRING,
total_price   DOUBLE
)
PARTITIONED BY (sale_date STRING, region STRING);

-- Add partitions to the two tables. 
ALTER TABLE sale_detail ADD PARTITION (sale_date='2013', region='china') PARTITION (sale_date='2014', region='shanghai');
ALTER TABLE sale_detail_jt ADD PARTITION (sale_date='2013', region='china');

-- Insert data into the tables. 
INSERT INTO sale_detail PARTITION (sale_date='2013', region='china') VALUES ('s1','c1',100.1),('s2','c2',100.2),('s3','c3',100.3);
INSERT INTO sale_detail PARTITION (sale_date='2014', region='shanghai') VALUES ('null','c5',null),('s6','c6',100.4),('s7','c7',100.5);
INSERT INTO sale_detail_jt PARTITION (sale_date='2013', region='china') VALUES ('s1','c1',100.1),('s2','c2',100.2),('s5','c2',100.2);

-- Query data from the sale_detail and sale_detail_jt tables. Sample statements:
SET odps.sql.allow.fullscan=true;
SELECT * FROM sale_detail;
-- The following result is returned:
+------------+-------------+-------------+------------+------------+
| shop_name  | customer_id | total_price | sale_date  | region     |
+------------+-------------+-------------+------------+------------+
| s1         | c1          | 100.1       | 2013       | china      |
| s2         | c2          | 100.2       | 2013       | china      |
| s3         | c3          | 100.3       | 2013       | china      |
| null       | c5          | NULL        | 2014       | shanghai   |
| s6         | c6          | 100.4       | 2014       | shanghai   |
| s7         | c7          | 100.5       | 2014       | shanghai   |
+------------+-------------+-------------+------------+------------+

SET odps.sql.allow.fullscan=true;
SELECT * FROM sale_detail_jt;
-- The following result is returned:
+------------+-------------+-------------+------------+------------+
| shop_name  | customer_id | total_price | sale_date  | region     |
+------------+-------------+-------------+------------+------------+
| s1         | c1          | 100.1       | 2013       | china      |
| s2         | c2          | 100.2       | 2013       | china      |
| s5         | c2          | 100.2       | 2013       | china      |
+------------+-------------+-------------+------------+------------+

-- Create a table for the JOIN operation. 
SET odps.sql.allow.fullscan=true;
CREATE TABLE shop AS SELECT shop_name, customer_id, total_price FROM sale_detail;

Examples

The following examples are based on the Sample data.

  • Example 1

    • Query statement:

      SELECT a.customer_id AS ashop, SUM(a.total_price) AS ap,COUNT(b.total_price) AS bp 
      FROM (SELECT * FROM sale_detail_jt WHERE sale_date='2013' AND region='china') a 
      INNER JOIN (SELECT * FROM sale_detail WHERE sale_date='2013' AND region='china') b 
      ON a.customer_id=b.customer_id 
      GROUP BY a.customer_id 
      ORDER BY a.customer_id 
      LIMIT 10;
    • To retrieve the semantics of the query statement, run the following command:

      EXPLAIN 
      SELECT a.customer_id AS ashop, SUM(a.total_price) AS ap,COUNT(b.total_price) AS bp 
      FROM (SELECT * FROM sale_detail_jt WHERE sale_date='2013' AND region='china') a 
      INNER JOIN (SELECT * FROM sale_detail WHERE sale_date='2013' AND region='china') b 
      ON a.customer_id=b.customer_id 
      GROUP BY a.customer_id 
      ORDER BY a.customer_id 
      LIMIT 10;

      The following output is returned:

      job0 is root job
      
      In Job job0:
      root Tasks: M1
      M2_1 depends on: M1
      R3_2 depends on: M2_1
      R4_3 depends on: R3_2
      
      In Task M1:
          Data source: doc_****.default.sale_detail/sale_date=2013/region=china
          TS: doc_****.default.sale_detail/sale_date=2013/region=china
              Statistics: Num rows: 3.0, Data size: 324.0
              FIL: ISNOTNULL(customer_id)
                  Statistics: Num rows: 2.7, Data size: 291.6
                  RS: valueDestLimit: 0
                      dist: BROADCAST
                      keys:
                      values:
                            customer_id (string)
                            total_price (double)
                      partitions:
      
                      Statistics: Num rows: 2.7, Data size: 291.6
      
      In Task M2_1:
          Data source: doc_****.default.sale_detail_jt/sale_date=2013/region=china
          TS: doc_****.default.sale_detail_jt/sale_date=2013/region=china
              Statistics: Num rows: 3.0, Data size: 324.0
              FIL: ISNOTNULL(customer_id)
                  Statistics: Num rows: 2.7, Data size: 291.6
                  HASHJOIN:
                           Filter1 INNERJOIN StreamLineRead1
                           keys:
                               0:customer_id
                               1:customer_id
                           non-equals:
                               0:
                               1:
                           bigTable: Filter1
      
                      Statistics: Num rows: 3.6450000000000005, Data size: 787.32
                      RS: order: +
                          nullDirection: *
                          optimizeOrderBy: False
                          valueDestLimit: 0
                          dist: HASH
                          keys:
                                customer_id
                          values:
                                customer_id (string)
                                total_price (double)
                                total_price (double)
                          partitions:
                                customer_id
      
                          Statistics: Num rows: 3.6450000000000005, Data size: 422.82000000000005
      
      In Task R3_2:
          AGGREGATE: group by:customer_id
           UDAF: SUM(total_price) (__agg_0_sum)[Complete],COUNT(total_price) (__agg_1_count)[Complete]
              Statistics: Num rows: 1.0, Data size: 116.0
              RS: order: +
                  nullDirection: *
                  optimizeOrderBy: True
                  valueDestLimit: 10
                  dist: HASH
                  keys:
                        customer_id
                  values:
                        customer_id (string)
                        __agg_0 (double)
                        __agg_1 (bigint)
                  partitions:
      
                  Statistics: Num rows: 1.0, Data size: 116.0
      
      In Task R4_3:
          SEL: customer_id,__agg_0,__agg_1
              Statistics: Num rows: 1.0, Data size: 116.0
              SEL: customer_id ashop, __agg_0 ap, __agg_1 bp, customer_id
                  Statistics: Num rows: 1.0, Data size: 216.0
                  FS: output: Screen
                      schema:
                        ashop (string)
                        ap (double)
                        bp (bigint)
      
                      Statistics: Num rows: 1.0, Data size: 116.0
      
      OK
  • Example 2

    • Query statement:

      SELECT /*+ mapjoin(a) */
             a.customer_id AS ashop, SUM(a.total_price) AS ap,COUNT(b.total_price) AS bp 
      FROM (SELECT * FROM sale_detail_jt 
      WHERE sale_date='2013' AND region='china') a 
      INNER JOIN (SELECT * FROM sale_detail WHERE sale_date='2013' AND region='china') b 
      ON a.total_price<b.total_price 
      GROUP BY a.customer_id 
      ORDER BY a.customer_id 
      LIMIT 10;
    • Retrieve the semantics of a query statement:

      EXPLAIN 
      SELECT /*+ mapjoin(a) */
             a.customer_id AS ashop, SUM(a.total_price) AS ap,COUNT(b.total_price) AS bp 
      FROM (SELECT * FROM sale_detail_jt 
      WHERE sale_date='2013' AND region='china') a 
      INNER JOIN (SELECT * FROM sale_detail WHERE sale_date='2013' AND region='china') b 
      ON a.total_price<b.total_price 
      GROUP BY a.customer_id 
      ORDER BY a.customer_id 
      LIMIT 10;

      The following output is returned:

      job0 is root job
      
      In Job job0:
      root Tasks: M1
      M2_1 depends on: M1
      R3_2 depends on: M2_1
      R4_3 depends on: R3_2
      
      In Task M1:
          Data source: doc_****.sale_detail_jt/sale_date=2013/region=china
          TS: doc_****.sale_detail_jt/sale_date=2013/region=china
              Statistics: Num rows: 3.0, Data size: 324.0
              RS: valueDestLimit: 0
                  dist: BROADCAST
                  keys:
                  values:
                        customer_id (string)
                        total_price (double)
                  partitions:
      
                  Statistics: Num rows: 3.0, Data size: 324.0
      
      In Task M2_1:
          Data source: doc_****.sale_detail/sale_date=2013/region=china
          TS: doc_****.sale_detail/sale_date=2013/region=china
              Statistics: Num rows: 3.0, Data size: 24.0
              HASHJOIN:
                       StreamLineRead1 INNERJOIN TableScan2
                       keys:
                           0:
                           1:
                       non-equals:
                           0:
                           1:
                       bigTable: TableScan2
      
                  Statistics: Num rows: 9.0, Data size: 1044.0
                  FIL: LT(total_price,total_price)
                      Statistics: Num rows: 6.75, Data size: 783.0
                      AGGREGATE: group by:customer_id
                       UDAF: SUM(total_price) (__agg_0_sum)[Partial_1],COUNT(total_price) (__agg_1_count)[Partial_1]
                          Statistics: Num rows: 2.3116438356164384, Data size: 268.1506849315069
                          RS: order: +
                              nullDirection: *
                              optimizeOrderBy: False
                              valueDestLimit: 0
                              dist: HASH
                              keys:
                                    customer_id
                              values:
                                    customer_id (string)
                                    __agg_0_sum (double)
                                    __agg_1_count (bigint)
                              partitions:
                                    customer_id
      
                              Statistics: Num rows: 2.3116438356164384, Data size: 268.1506849315069
      
      In Task R3_2:
          AGGREGATE: group by:customer_id
           UDAF: SUM(__agg_0_sum)[Final] __agg_0,COUNT(__agg_1_count)[Final] __agg_1
              Statistics: Num rows: 1.6875, Data size: 195.75
              RS: order: +
                  nullDirection: *
                  optimizeOrderBy: True
                  valueDestLimit: 10
                  dist: HASH
                  keys:
                        customer_id
                  values:
                        customer_id (string)
                        __agg_0 (double)
                        __agg_1 (bigint)
                  partitions:
      
                  Statistics: Num rows: 1.6875, Data size: 195.75
      
      In Task R4_3:
          SEL: customer_id,__agg_0,__agg_1
              Statistics: Num rows: 1.6875, Data size: 195.75
              SEL: customer_id ashop, __agg_0 ap, __agg_1 bp, customer_id
                  Statistics: Num rows: 1.6875, Data size: 364.5
                  FS: output: Screen
                      schema:
                        ashop (string)
                        ap (double)
                        bp (bigint)
      
                      Statistics: Num rows: 1.6875, Data size: 195.75
      
      OK