EXPLAIN Statements

EXPLAIN statements are used to explain the logical and optimized query plans of a query or an INSERT statement.

Run an EXPLAIN statement

Java

EXPLAIN statements can be executed with the executeSql() method of the TableEnvironment. The executeSql() method returns explain result for a successful EXPLAIN operation, otherwise will throw an exception.

The following examples show how to run an EXPLAIN statement in TableEnvironment.

Scala

EXPLAIN statements can be executed with the executeSql() method of the TableEnvironment. The executeSql() method returns explain result for a successful EXPLAIN operation, otherwise will throw an exception.

The following examples show how to run an EXPLAIN statement in TableEnvironment.

Python

EXPLAIN statements can be executed with the execute_sql() method of the TableEnvironment. The execute_sql() method returns explain result for a successful EXPLAIN operation, otherwise will throw an exception.

The following examples show how to run an EXPLAIN statement in TableEnvironment.

SQL CLI

EXPLAIN statements can be executed in SQL CLI.

The following examples show how to run an EXPLAIN statement in SQL CLI.

Java

  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, word VARCHAR(256)) WITH ('connector' = 'datagen')");
  5. tEnv.executeSql("CREATE TABLE MyTable2 (`count` bigint, word VARCHAR(256)) WITH ('connector' = 'datagen')");
  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();
  19. TableResult tableResult2 = tEnv.executeSql(
  20. "EXPLAIN ESTIMATED_COST, CHANGELOG_MODE, JSON_EXECUTION_PLAN " +
  21. "SELECT `count`, word FROM MyTable1 WHERE word LIKE 'F%' " +
  22. "UNION ALL " +
  23. "SELECT `count`, word FROM MyTable2");
  24. tableResult2.print();

Scala

  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, word VARCHAR(256)) WITH ('connector' = 'datagen')")
  5. tEnv.executeSql("CREATE TABLE MyTable2 (`count` bigint, word VARCHAR(256)) WITH ('connector' = 'datagen')")
  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()
  19. val tableResult2 = tEnv.executeSql(
  20. "EXPLAIN ESTIMATED_COST, CHANGELOG_MODE, JSON_EXECUTION_PLAN " +
  21. "SELECT `count`, word FROM MyTable1 WHERE word LIKE 'F%' " +
  22. "UNION ALL " +
  23. "SELECT `count`, word FROM MyTable2")
  24. tableResult2.print()

Python

  1. settings = EnvironmentSettings.new_instance()...
  2. table_env = StreamTableEnvironment.create(env, settings)
  3. t_env.execute_sql("CREATE TABLE MyTable1 (`count` bigint, word VARCHAR(256)) WITH ('connector' = 'datagen')")
  4. t_env.execute_sql("CREATE TABLE MyTable2 (`count` bigint, word VARCHAR(256)) WITH ('connector' = 'datagen')")
  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()
  18. table_result2 = t_env.execute_sql(
  19. "EXPLAIN ESTIMATED_COST, CHANGELOG_MODE, JSON_EXECUTION_PLAN "
  20. "SELECT `count`, word FROM MyTable1 WHERE word LIKE 'F%' "
  21. "UNION ALL "
  22. "SELECT `count`, word FROM MyTable2")
  23. table_result2.print()

SQL CLI

  1. Flink SQL> CREATE TABLE MyTable1 (`count` bigint, word VARCHAR(256)) WITH ('connector' = 'datagen');
  2. [INFO] Table has been created.
  3. Flink SQL> CREATE TABLE MyTable2 (`count` bigint, word VARCHAR(256)) WITH ('connector' = 'datagen');
  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;
  8. Flink SQL> EXPLAIN ESTIMATED_COST, CHANGELOG_MODE, JSON_EXECUTION_PLAN SELECT `count`, word FROM MyTable1
  9. > WHERE word LIKE 'F%'
  10. > UNION ALL
  11. > SELECT `count`, word FROM MyTable2;

The EXPLAIN result is:

EXPLAIN PLAN

  1. == Abstract Syntax Tree ==
  2. LogicalUnion(all=[true])
  3. :- LogicalProject(count=[$0], word=[$1])
  4. : +- LogicalFilter(condition=[LIKE($1, _UTF-16LE'F%')])
  5. : +- LogicalTableScan(table=[[default_catalog, default_database, MyTable1]])
  6. +- LogicalProject(count=[$0], word=[$1])
  7. +- LogicalTableScan(table=[[default_catalog, default_database, MyTable2]])
  8. == Optimized Physical Plan ==
  9. Union(all=[true], union=[count, word])
  10. :- Calc(select=[count, word], where=[LIKE(word, _UTF-16LE'F%')])
  11. : +- TableSourceScan(table=[[default_catalog, default_database, MyTable1]], fields=[count, word])
  12. +- TableSourceScan(table=[[default_catalog, default_database, MyTable2]], fields=[count, word])
  13. == Optimized Execution Plan ==
  14. Union(all=[true], union=[count, word])
  15. :- Calc(select=[count, word], where=[LIKE(word, _UTF-16LE'F%')])
  16. : +- TableSourceScan(table=[[default_catalog, default_database, MyTable1]], fields=[count, word])
  17. +- TableSourceScan(table=[[default_catalog, default_database, MyTable2]], fields=[count, word])

EXPLAIN PLAN WITH DETAILS

  1. == Abstract Syntax Tree ==
  2. LogicalUnion(all=[true])
  3. :- LogicalProject(count=[$0], word=[$1])
  4. : +- LogicalFilter(condition=[LIKE($1, _UTF-16LE'F%')])
  5. : +- LogicalTableScan(table=[[default_catalog, default_database, MyTable1]])
  6. +- LogicalProject(count=[$0], word=[$1])
  7. +- LogicalTableScan(table=[[default_catalog, default_database, MyTable2]])
  8. == Optimized Physical Plan ==
  9. Union(all=[true], union=[count, word], changelogMode=[I]): rowcount = 1.05E8, cumulative cost = {3.1E8 rows, 3.05E8 cpu, 4.0E9 io, 0.0 network, 0.0 memory}
  10. :- Calc(select=[count, word], where=[LIKE(word, _UTF-16LE'F%')], changelogMode=[I]): rowcount = 5000000.0, cumulative cost = {1.05E8 rows, 1.0E8 cpu, 2.0E9 io, 0.0 network, 0.0 memory}
  11. : +- TableSourceScan(table=[[default_catalog, default_database, MyTable1]], fields=[count, word], changelogMode=[I]): rowcount = 1.0E8, cumulative cost = {1.0E8 rows, 1.0E8 cpu, 2.0E9 io, 0.0 network, 0.0 memory}
  12. +- TableSourceScan(table=[[default_catalog, default_database, MyTable2]], fields=[count, word], changelogMode=[I]): rowcount = 1.0E8, cumulative cost = {1.0E8 rows, 1.0E8 cpu, 2.0E9 io, 0.0 network, 0.0 memory}
  13. == Optimized Execution Plan ==
  14. Union(all=[true], union=[count, word])
  15. :- Calc(select=[count, word], where=[LIKE(word, _UTF-16LE'F%')])
  16. : +- TableSourceScan(table=[[default_catalog, default_database, MyTable1]], fields=[count, word])
  17. +- TableSourceScan(table=[[default_catalog, default_database, MyTable2]], fields=[count, word])
  18. == Physical Execution Plan ==
  19. {
  20. "nodes" : [ {
  21. "id" : 37,
  22. "type" : "Source: TableSourceScan(table=[[default_catalog, default_database, MyTable1]], fields=[count, word])",
  23. "pact" : "Data Source",
  24. "contents" : "Source: TableSourceScan(table=[[default_catalog, default_database, MyTable1]], fields=[count, word])",
  25. "parallelism" : 1
  26. }, {
  27. "id" : 38,
  28. "type" : "Calc(select=[count, word], where=[LIKE(word, _UTF-16LE'F%')])",
  29. "pact" : "Operator",
  30. "contents" : "Calc(select=[count, word], where=[LIKE(word, _UTF-16LE'F%')])",
  31. "parallelism" : 1,
  32. "predecessors" : [ {
  33. "id" : 37,
  34. "ship_strategy" : "FORWARD",
  35. "side" : "second"
  36. } ]
  37. }, {
  38. "id" : 39,
  39. "type" : "Source: TableSourceScan(table=[[default_catalog, default_database, MyTable2]], fields=[count, word])",
  40. "pact" : "Data Source",
  41. "contents" : "Source: TableSourceScan(table=[[default_catalog, default_database, MyTable2]], fields=[count, word])",
  42. "parallelism" : 1
  43. } ]

ExplainDetails

  1. Print the plan for the statement with specified ExplainDetails.
  2. ESTIMATED_COST: generates cost information on physical node estimated by optimizer,
  3. e.g. TableSourceScan(..., cumulative cost ={1.0E8 rows, 1.0E8 cpu, 2.4E9 io, 0.0 network, 0.0 memory})
  4. CHANGELOG_MODE:generates changelog mode for every physical rel node.
  5. e.g. GroupAggregate(..., changelogMode=[I,UA,D])
  6. JSON_EXECUTION_PLAN: generates the execution plan in json format of the program.

Syntax

  1. EXPLAIN [([ExplainDetail[, ExplainDetail]*]) | PLAN FOR] <query_statement_or_insert_statement>

For query syntax, please refer to Queries page. For INSERT, please refer to INSERT page.