Explan 语句
— 焉知非鱼Explan Statements
EXPLAIN 语句
EXPLAIN 语句用于解释一个查询或 INSERT 语句的逻辑和优化查询计划。
运行 EXPLAIN 语句 #
EXPLAIN 语句可以用 TableEnvironment 的 executeSql()
方法执行,也可以在 SQL CLI 中执行。executeSql()
方法在 EXPLAIN 操作成功后返回解释结果,否则将抛出一个异常。
下面的例子展示了如何在 TableEnvironment 和 SQL CLI 中运行 EXPLAIN 语句。
val env = StreamExecutionEnvironment.getExecutionEnvironment()
val tEnv = StreamTableEnvironment.create(env)
// register a table named "Orders"
tEnv.executeSql("CREATE TABLE MyTable1 (count bigint, work VARCHAR(256) WITH (...)")
tEnv.executeSql("CREATE TABLE MyTable2 (count bigint, work VARCHAR(256) WITH (...)")
// explain SELECT statement through TableEnvironment.explainSql()
val explanation = tEnv.explainSql(
"SELECT count, word FROM MyTable1 WHERE word LIKE 'F%' " +
"UNION ALL " +
"SELECT count, word FROM MyTable2")
println(explanation)
// explain SELECT statement through TableEnvironment.executeSql()
val tableResult = tEnv.executeSql(
"EXPLAIN PLAN FOR " +
"SELECT count, word FROM MyTable1 WHERE word LIKE 'F%' " +
"UNION ALL " +
"SELECT count, word FROM MyTable2")
tableResult.print()
EXPLAIN 的结果是:
== Abstract Syntax Tree ==
LogicalUnion(all=[true])
LogicalFilter(condition=[LIKE($1, _UTF-16LE'F%')])
FlinkLogicalTableSourceScan(table=[[default_catalog, default_database, MyTable1]], fields=[count, word])
FlinkLogicalTableSourceScan(table=[[default_catalog, default_database, MyTable2]], fields=[count, word])
== Optimized Logical Plan ==
DataStreamUnion(all=[true], union all=[count, word])
DataStreamCalc(select=[count, word], where=[LIKE(word, _UTF-16LE'F%')])
TableSourceScan(table=[[default_catalog, default_database, MyTable1]], fields=[count, word])
TableSourceScan(table=[[default_catalog, default_database, MyTable2]], fields=[count, word])
== Physical Execution Plan ==
Stage 1 : Data Source
content : collect elements with CollectionInputFormat
Stage 2 : Data Source
content : collect elements with CollectionInputFormat
Stage 3 : Operator
content : from: (count, word)
ship_strategy : REBALANCE
Stage 4 : Operator
content : where: (LIKE(word, _UTF-16LE'F%')), select: (count, word)
ship_strategy : FORWARD
Stage 5 : Operator
content : from: (count, word)
ship_strategy : REBALANCE
语法 #
EXPLAIN PLAN FOR <query_statement_or_insert_statement>