Hive的EXPLAIN命令可以显示执行计划,使用户可以了解Hive如何执行查询语句。执行计划由多个阶段组成,每个阶段都会执行一些操作,例如过滤、连接、聚合等。接下来,我们将详细解释EXPLAIN命令的输出并说明如何将其解释为查询的执行计划。
下面是一个示例查询语句和相应的EXPLAIN输出:
查询语句:
SELECT /*+ mapjoin(b) */ a.id, b.name FROM table1 a JOIN table2 b ON (a.id = b.id) WHERE a.value > 10;
EXPLAIN输出:
STAGE DEPENDENCIES:
Stage-1 is a root stage
Stage-2 depends on stages: Stage-1
Stage-0 depends on stages: Stage-2
STAGE PLANS:
Stage: Stage-1
Map Reduce
Map Operator Tree:
TableScan
alias: a
filterExpr: (value > 10) (type: boolean)
Statistics: Num rows: 500 Data size: 52500 Basic stats: COMPLETE Column stats: NONE
Select Operator
expressions: id (type: int)
outputColumnNames: id
Statistics: Num rows: 500 Data size: 5000 Basic stats: COMPLETE Column stats: NONE
Reduce Output Operator
key expressions: id (type: int)
sort order: +
Map-reduce partition columns: id (type: int)
Statistics: Num rows: 500 Data size: 5000 Basic stats: COMPLETE Column stats: NONE
Execution mode: vectorized
Map Join Operator
condition map:
Inner Join 0 to 1
keys:
0 id (type: int)
1 id (type: int)
outputColumnNames: _col0, _col1
input vertices:
1 Map 2
Statistics: Num rows: 550 Data size: 6050 Basic stats: COMPLETE Column stats: NONE
Select Operator
expressions: _col0 (type: int), _col1 (type: string)
outputColumnNames: _col0, _col1
Statistics: Num rows: 550 Data size: 6050 Basic stats: COMPLETE Column stats: NONE
Reduce Output Operator
key expressions: _col0 (type: int)
sort order: +
Map-reduce partition columns: _col0 (type: int)
Statistics: Num rows: 550 Data size: 6050 Basic stats: COMPLETE Column stats: NONE
value expressions: _col1 (type: string)
Execution mode: vectorized
Stage: Stage-2
Map Reduce
Map Operator Tree:
TableScan
alias: b
Statistics: Num rows: 1000 Data size: 106000 Basic stats: COMPLETE Column stats: NONE
Select Operator
expressions: id (type: int), name (type: string)
outputColumnNames: id, name
Statistics: Num rows: 1000 Data size: 106000 Basic stats: COMPLETE Column stats: NONE
Filter Operator
predicate: (id is not null and name is not null) (type: boolean)
Statistics: Num rows: 1000 Data size: 106000 Basic stats: COMPLETE Column stats: NONE
Map Join Operator
condition map:
Inner Join 0 to 1
keys:
0 id (type: int)
1 id (type: int)
outputColumnNames: _col0, _col1
input vertices:
0 Map 1
Statistics: Num rows: 605 Data size: 6655 Basic stats: COMPLETE Column stats: NONE
Select Operator
expressions: _col0 (type: int), _col1 (type: string)
outputColumnNames: _col0, _col1
Statistics: Num rows: 605 Data size: 6655 Basic stats: COMPLETE Column stats: NONE
Reduce Output Operator
key expressions: _col0 (type: int)
sort order: +
Map-reduce partition columns: _col0 (type: int)
Statistics: Num rows: 605 Data size: 6655 Basic stats: COMPLETE Column stats: NONE
value expressions: _col1 (type: string)
Execution mode: vectorized
Stage: Stage-0
Fetch Operator
limit: -1
Processor Tree:
ListSink
解析:
第一部分:
STAGE DEPENDENCIES:
Stage-1 is a root stage
Stage-2 depends on stages: Stage-1
Stage-0 depends on stages: Stage-2
该部分显示了查询中的阶段及其依赖关系。查询中包含3个阶段,其中第一阶段为根阶段,其余2个阶段依赖于前一个阶段。
第二部分:
STAGE PLANS:
Stage: Stage-1
Map Reduce
Map Operator Tree:
TableScan
alias: a
filterExpr: (value > 10) (type: boolean)
...
该部分展示了第一个阶段的详细执行计划。对于该查询,第一个阶段是一个“Map Reduce”阶段,其执行计划包括一个“TableScan”操作和一个“Select”操作。查询还包含一个过滤操作,该过滤操作也显示在表扫描器下方。
第三部分:
Stage: Stage-2
Map Reduce
Map Operator Tree:
TableScan
alias: b
...
该部分显示了第二个阶段的执行计划,它也是一个“Map Reduce”阶段,其中包含与第一个阶段类似的“TableScan”和“Map Join”操作。
第四部分:
Stage: Stage-0
Fetch Operator
limit: -1
Processor Tree:
ListSink
该部分显示了查询执行的最后一个阶段。该阶段执行“Fetch”操作,并将结果输出到“ListSink”。
要解析Hive中的执行计划,请仔细查看阶段的执行计划,并了解每个操作的含义。阶段之间的依赖关系可用于确定查询的全局执行顺序。