关联查询—驱动表的选择
OPTIMIZER STATISTICS AND COMPUTATIONS 优化器的统计和计算
***************************************
GENERAL PLANS 选择执行计划
***************************************
Considering cardinality-based initial join order.
Permutations for Starting Table :0
Join order[1]: LEO4[LEO4]#0 LEO3[LEO3]#1 关联的对象
***************
Now joining: LEO3[LEO3]#1 现在要用leo4小表关联leo3大表,leo4做驱动表
***************
NL Join嵌套循环关联 leo4表中有99条,小表为驱动表
驱动表Outer table: Card: 99.00 Cost: 1.00 Resp: 1.00 Degree: 1 Bytes: 3
Access path analysis for LEO3
Inner table: LEO3 Alias: LEO3
Access Path: TableScan 全表扫描-嵌套循环关联COST=28253.17
NL Join: Cost: 28253.17 Resp: 28253.17 Degree: 1
Cost_io: 28183.00 Cost_cpu: 2237241142
Resp_io: 28183.00 Resp_cpu: 2237241142 并行访问代价
Access Path: index (index (FFS)) 索引快速全扫描
Index: IDX_LEO3
resc_io: 43.08 resc_cpu: 9768589 串行访问代价
ix_sel: 0.000000 ix_sel_with_filters: 1.000000
Inner table: LEO3 Alias: LEO3
Access Path: index (FFS)
NL Join: Cost: 4296.33 Resp: 4296.33 Degree: 1 并行访问
Cost_io: 4266.00 Cost_cpu: 967117228
Resp_io: 4266.00 Resp_cpu: 967117228
Access Path: index (AllEqJoinGuess)
Index: IDX_LEO3
resc_io: 1.00 resc_cpu: 8171
ix_sel: 0.000014 ix_sel_with_filters: 0.000014
NL Join (ordered): Cost: 100.03 Resp: 100.03 Degree: 1
Cost_io: 100.00 Cost_cpu: 835894
Resp_io: 100.00 Resp_cpu: 835894
Best NL cost: 100.03 leo4为驱动表,小表为驱动表,最后代价100.03
resc: 100.03 resc_io: 100.00 resc_cpu: 835894 串行方式的代价 IO代价+CPU代价
resp: 100.03 resp_io: 100.00 resc_cpu: 835894 并行方式的代价
Outer table: LEO4 Alias: LEO4
SM Join 先排序后合并关联
SM cost: 268.06 代价268.06
resc: 268.06 resc_io: 265.00 resc_cpu: 97470464
resp: 268.06 resp_io: 265.00 resp_cpu: 97470464
HA Join 哈希关联
HA cost: 47.03 代价47.03,最好是哈希代价最小
resc: 47.03 resc_io: 46.00 resc_cpu: 32949334
resp: 47.03 resp_io: 46.00 resp_cpu: 32949334
Best:: JoinMethod: Hash 最后关联方法选择:哈希hash
Cost: 47.03 Degree: 1 Resp: 47.03 Card: 99.00 Bytes: 8 返回记录数+字节
***************
Now joining: LEO4[LEO4]#0 现在要用leo3大表关联leo4小表,leo3做驱动表
***************
NL Join 嵌套循环关联 leo3表中有71969条,大表为驱动表
Outer table: Card: 71969.00 Cost: 45.31 Resp: 45.31 Degree: 1 Bytes: 5
Access path analysis for LEO4
Inner table: LEO4 Alias: LEO4
Access Path: TableScan
NL Join: Cost: 97632.61 Resp: 97632.61 Degree: 1
Cost_io: 97505.00 Cost_cpu: 4068618676
Resp_io: 97505.00 Resp_cpu: 4068618676
Access Path: index (index (FFS))
Index: IDX_LEO4
resc_io: 0.27 resc_cpu: 19001
ix_sel: 0.000000 ix_sel_with_filters: 1.000000
Inner table: LEO4 Alias: LEO4
Access Path: index (FFS)
NL Join: Cost: 19581.20 Resp: 19581.20 Degree: 1
Cost_io: 19538.00 Cost_cpu: 1377283224
Resp_io: 19538.00 Resp_cpu: 1377283224
Access Path: index (AllEqJoinGuess)
Index: IDX_LEO4
resc_io: 0.00 resc_cpu: 1050
ix_sel: 0.010101 ix_sel_with_filters: 0.010101
NL Join (ordered): Cost: 47.68 Resp: 47.68 Degree: 1
Cost_io: 45.00 Cost_cpu: 85336039
Resp_io: 45.00 Resp_cpu: 85336039
Best NL cost: 47.68 嵌套循环关联最后代价47.68
resc: 47.68 resc_io: 45.00 resc_cpu: 85336039
resp: 47.68 resp_io: 45.00 resc_cpu: 85336039
SM Join 先排序后合并关联
SM cost: 269.06 代价269.06
resc: 269.06 resc_io: 265.00 resc_cpu: 129384180
resp: 269.06 resp_io: 265.00 resp_cpu: 129384180
Hash join: Resc: 106.17 Resp: 106.17 [multiMatchCost=0.00] 哈希关联,代价=106.17
Final cost for query block SEL$1 (#0) - All Rows Plan:
Best join order: 1 最终代价选择47.0334,用leo4小表驱动表
Cost: 47.0334 Degree: 1 Card: 99.0000 Bytes: 792
Resc: 47.0334 Resc_io: 46.0000 Resc_cpu: 32949334
Resp: 47.0334 Resp_io: 46.0000 Resc_cpu: 32949334
SQL执行计划的选择
============
Plan Table
============
------------------------------------------+-----------------------------------+
| Id | Operation | Name | Rows | Bytes | Cost | Time |
------------------------------------------+-----------------------------------+
| 0 | SELECT STATEMENT | | | | 47 | |
| 1 | SORT AGGREGATE | | 1 | 8 | | |
| 2 | HASH JOIN | | 99 | 792 | 47 | 00:00:01 |
| 3 | INDEX FULL SCAN | IDX_LEO4| 99 | 297 | 1 | 00:00:01 |
| 4 | INDEX FAST FULL SCAN | IDX_LEO3| 70K | 351K | 45 | 00:00:01 |
------------------------------------------+-----------------------------------+
Predicate Information:
----------------------
2 - access("LEO3"."OBJECT_ID"="LEO4"."OBJECT_ID")
选择的执行计划和上面分析结果是相匹配的
来看看我们真实的执行计划的样子
LEO1@LEO1> set autotrace trace exp
LEO1@LEO1> select count(*) from leo3,leo4 where leo3.object_id=leo4.object_id;
Execution Plan
----------------------------------------------------------
Plan hash value: 172281424
-----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 8 | 47 (3)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 8 | | |
|* 2 | HASH JOIN | | 99 | 792 | 47 (3)| 00:00:01 |
| 3 | INDEX FULL SCAN | IDX_LEO4 | 99 | 297 | 1 (0)| 00:00:01 |
| 4 | INDEX FAST FULL SCAN | IDX_LEO3 | 71969 | 351K| 45 (0)| 00:00:01 |
-----------------------------------------------------------------------------------
小结:一模一样对吧,这说明我们的优化器在对比完不同代价后选择的执行计划是最优的,如果我们在实际工作中,遇到了执行计划选择错误的情景,我们可以通过10053事件来做详细的分析。
