EXPLAIN 语句

EXPLAIN 语句用来解释一条 query 语句或者 INSERT 语句的逻辑计划和优化后的计划。

运行一条 EXPLAIN 语句

EXPLAIN 语句可以通过 TableEnvironmentexecuteSql() 执行,也可以在 SQL CLI 中执行 EXPLAIN 语句。 若 EXPLAIN 操作执行成功,executeSql() 方法返回解释的结果,否则会抛出异常。

以下的例子展示了如何在 TableEnvironment 和 SQL CLI 中执行一条 EXPLAIN 语句。

  1. StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
  2. StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
  3. // register a table named "Orders"
  4. tEnv.executeSql("CREATE TABLE MyTable1 (count bigint, work VARCHAR(256) WITH (...)");
  5. tEnv.executeSql("CREATE TABLE MyTable2 (count bigint, work VARCHAR(256) WITH (...)");
  6. // explain SELECT statement through TableEnvironment.explainSql()
  7. String explanation = tEnv.explainSql(
  8. "SELECT count, word FROM MyTable1 WHERE word LIKE 'F%' " +
  9. "UNION ALL " +
  10. "SELECT count, word FROM MyTable2");
  11. System.out.println(explanation);
  12. // explain SELECT statement through TableEnvironment.executeSql()
  13. TableResult tableResult = tEnv.executeSql(
  14. "EXPLAIN PLAN FOR " +
  15. "SELECT count, word FROM MyTable1 WHERE word LIKE 'F%' " +
  16. "UNION ALL " +
  17. "SELECT count, word FROM MyTable2");
  18. tableResult.print();
  1. val env = StreamExecutionEnvironment.getExecutionEnvironment()
  2. val tEnv = StreamTableEnvironment.create(env)
  3. // register a table named "Orders"
  4. tEnv.executeSql("CREATE TABLE MyTable1 (count bigint, work VARCHAR(256) WITH (...)")
  5. tEnv.executeSql("CREATE TABLE MyTable2 (count bigint, work VARCHAR(256) WITH (...)")
  6. // explain SELECT statement through TableEnvironment.explainSql()
  7. val explanation = tEnv.explainSql(
  8. "SELECT count, word FROM MyTable1 WHERE word LIKE 'F%' " +
  9. "UNION ALL " +
  10. "SELECT count, word FROM MyTable2")
  11. println(explanation)
  12. // explain SELECT statement through TableEnvironment.executeSql()
  13. val tableResult = tEnv.executeSql(
  14. "EXPLAIN PLAN FOR " +
  15. "SELECT count, word FROM MyTable1 WHERE word LIKE 'F%' " +
  16. "UNION ALL " +
  17. "SELECT count, word FROM MyTable2")
  18. tableResult.print()
  1. settings = EnvironmentSettings.new_instance()...
  2. table_env = StreamTableEnvironment.create(env, settings)
  3. t_env.execute_sql("CREATE TABLE MyTable1 (count bigint, work VARCHAR(256) WITH (...)")
  4. t_env.execute_sql("CREATE TABLE MyTable2 (count bigint, work VARCHAR(256) WITH (...)")
  5. # explain SELECT statement through TableEnvironment.explain_sql()
  6. explanation1 = t_env.explain_sql(
  7. "SELECT count, word FROM MyTable1 WHERE word LIKE 'F%' "
  8. "UNION ALL "
  9. "SELECT count, word FROM MyTable2")
  10. print(explanation1)
  11. # explain SELECT statement through TableEnvironment.execute_sql()
  12. table_result = t_env.execute_sql(
  13. "EXPLAIN PLAN FOR "
  14. "SELECT count, word FROM MyTable1 WHERE word LIKE 'F%' "
  15. "UNION ALL "
  16. "SELECT count, word FROM MyTable2")
  17. table_result.print()
  1. Flink SQL> CREATE TABLE MyTable1 (count bigint, work VARCHAR(256);
  2. [INFO] Table has been created.
  3. Flink SQL> CREATE TABLE MyTable2 (count bigint, work VARCHAR(256);
  4. [INFO] Table has been created.
  5. Flink SQL> EXPLAIN PLAN FOR SELECT count, word FROM MyTable1 WHERE word LIKE 'F%'
  6. > UNION ALL
  7. > SELECT count, word FROM MyTable2;

执行 EXPLAIN 语句后的结果为:

  1. == Abstract Syntax Tree ==
  2. LogicalUnion(all=[true])
  3. LogicalFilter(condition=[LIKE($1, _UTF-16LE'F%')])
  4. FlinkLogicalTableSourceScan(table=[[default_catalog, default_database, MyTable1]], fields=[count, word])
  5. FlinkLogicalTableSourceScan(table=[[default_catalog, default_database, MyTable2]], fields=[count, word])
  6. == Optimized Logical Plan ==
  7. DataStreamUnion(all=[true], union all=[count, word])
  8. DataStreamCalc(select=[count, word], where=[LIKE(word, _UTF-16LE'F%')])
  9. TableSourceScan(table=[[default_catalog, default_database, MyTable1]], fields=[count, word])
  10. TableSourceScan(table=[[default_catalog, default_database, MyTable2]], fields=[count, word])
  11. == Physical Execution Plan ==
  12. Stage 1 : Data Source
  13. content : collect elements with CollectionInputFormat
  14. Stage 2 : Data Source
  15. content : collect elements with CollectionInputFormat
  16. Stage 3 : Operator
  17. content : from: (count, word)
  18. ship_strategy : REBALANCE
  19. Stage 4 : Operator
  20. content : where: (LIKE(word, _UTF-16LE'F%')), select: (count, word)
  21. ship_strategy : FORWARD
  22. Stage 5 : Operator
  23. content : from: (count, word)
  24. ship_strategy : REBALANCE

语法

  1. EXPLAIN PLAN FOR <query_statement_or_insert_statement>

请参阅 Queries 页面获得 query 的语法。 请参阅 INSERT 页面获得 INSERT 的语法。