EXPLAIN 语句

    Java

    可以使用 的 executeSql() 方法执行 EXPLAIN 语句。如果 EXPLAIN 操作执行成功,executeSql() 方法会返回解释结果,否则会抛出异常。

    以下示例展示了如何在 TableEnvironment 中执行一条 EXPLAIN 语句。

    Scala

    可以使用 TableEnvironmentexecuteSql() 方法执行 EXPLAIN 语句。如果 EXPLAIN 操作执行成功,executeSql() 方法会返回解释结果,否则会抛出异常。

    以下示例展示了如何在 TableEnvironment 中执行一条 EXPLAIN 语句。

    可以使用 TableEnvironmentexecute_sql() 方法执行 EXPLAIN 语句。如果 EXPLAIN 操作执行成功,execute_sql() 方法会返回解释结果,否则会抛出异常。

    以下示例展示了如何在 TableEnvironment 中执行一条 EXPLAIN 语句。

    SQL CLI

    EXPLAIN 语句可以在 SQL CLI 中执行。

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

    Java

    1. val env = StreamExecutionEnvironment.getExecutionEnvironment()
    2. val tEnv = StreamTableEnvironment.create(env)
    3. // 注册名为 “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. // 调用 TableEnvironment.explainSql() 来解释 SELECT 语句
    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. // 调用 TableEnvironment.executeSql() 来解释 SELECT 语句
    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. "UNION ALL " +
    22. "SELECT `count`, word FROM MyTable2")
    23. tableResult2.print()

    Python

    SQL CLI

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

    EXPLAIN 的结果如下:

    EXPLAIN PLAN

    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, MyTable2]], fields=[count, word])
    17. {
    18. "nodes" : [ {
    19. "id" : 37,
    20. "type" : "Source: TableSourceScan(table=[[default_catalog, default_database, MyTable1]], fields=[count, word])",
    21. "pact" : "Data Source",
    22. "contents" : "Source: TableSourceScan(table=[[default_catalog, default_database, MyTable1]], fields=[count, word])",
    23. "parallelism" : 1
    24. }, {
    25. "id" : 38,
    26. "type" : "Calc(select=[count, word], where=[LIKE(word, _UTF-16LE'F%')])",
    27. "pact" : "Operator",
    28. "contents" : "Calc(select=[count, word], where=[LIKE(word, _UTF-16LE'F%')])",
    29. "parallelism" : 1,
    30. "predecessors" : [ {
    31. "id" : 37,
    32. "ship_strategy" : "FORWARD",
    33. "side" : "second"
    34. } ]
    35. }, {
    36. "id" : 39,
    37. "type" : "Source: TableSourceScan(table=[[default_catalog, default_database, MyTable2]], fields=[count, word])",
    38. "pact" : "Data Source",
    39. "contents" : "Source: TableSourceScan(table=[[default_catalog, default_database, MyTable2]], fields=[count, word])",
    40. "parallelism" : 1
    41. } ]
    1. EXPLAIN [([ExplainDetail[, ExplainDetail]*]) | PLAN FOR] <query_statement_or_insert_statement_or_statement_set>
    2. statement_set:
    3. EXECUTE STATEMENT SET
    4. BEGIN
    5. insert_statement;
    6. ...
    7. END;

    关于 query 的语法,请查阅 页面。 关于 INSERT 的语法,请查阅 INSERT 页面。