OceanBase 数据库的优化器会分两阶段来生成分布式的执行计划。

第一阶段,不考虑数据的物理分布,生成所有基于本地关系优化的最优执行计划。在本地计划生成后,优化器会检查数据是否访问了多个分区,或者是否是本地单分区表但是用户用 hint 强制指定了采用并行查询执行。

第二阶段,生成分布式计划。根据执行计划树,在需要进行数据重分布的地方,插入 exchange 节点,从而将原先的本地计划树变成分布式计划。

分布式执行计划的算子

生成分布式计划的过程就是在原始计划树上寻找恰当位置插入 exchange 算子的过程,在自顶向下遍历计划树的时候,需要根据相应算子的数据处理的情况以及输入算子的数据分区情况,决定是否需要插入 exchange 算子。

如下示例为最简单的单表扫描:

  1. explain select * from t2\G
  2. *************************** 1. row ***************************
  3. Query Plan:
  4. ============================================
  5. |ID|OPERATOR |NAME|EST. ROWS|COST|
  6. --------------------------------------------
  7. |0 |EXCHANGE IN DISTR | |4000 |878 |
  8. |1 | EXCHANGE OUT DISTR| |4000 |499 |
  9. |2 | TABLE SCAN |t2 |4000 |499 |
  10. ============================================
  11. Outputs & filters:
  12. -------------------------------------
  13. 0 - output([t2.c1], [t2.c2]), filter(nil)
  14. 1 - output([t2.c1], [t2.c2]), filter(nil)
  15. 2 - output([t2.c1], [t2.c2]), filter(nil),
  16. access([t2.c1], [t2.c2]), partitions(p[0-3])

当 t2 是一个分区表,可以在 table scan 上插入配对的 exchange 算子,从而将 table scan 和 exchange out 封装成一个 job,可以用于并行的执行。

单输入可下压算子

单输入可下压算子主要包括 aggregation, sort, group by 和 limit 算子等,除了 limit 算子以外,其余所列举的算子都会有一个操作的键,如果操作的键和输入数据的数据分布是一致的,则可以做一阶段聚合操作,也即 Partition Wise Aggregation。如果操作的键和输入数据的数据分布是不一致的,则需要做两阶段聚合操作,聚合算子需要做下压操作。

一阶段聚合操作如下例所示:

  1. explain select sum(v1) from t2 group by v1;
  2. | ======================================================
  3. |ID|OPERATOR |NAME |EST. ROWS|COST |
  4. ------------------------------------------------------
  5. |0 |PX COORDINATOR | |101 |357302|
  6. |1 | EXCHANGE OUT DISTR |:EX10000|101 |357297|
  7. |2 | PX PARTITION ITERATOR| |101 |357297|
  8. |3 | MERGE GROUP BY | |101 |357297|
  9. |4 | TABLE SCAN |t2 |400000 |247403|
  10. ======================================================
  11. Outputs & filters:
  12. -------------------------------------
  13. 0 - output([T_FUN_SUM(t2.v1)]), filter(nil)
  14. 1 - output([T_FUN_SUM(t2.v1)]), filter(nil), dop=1
  15. 2 - output([T_FUN_SUM(t2.v1)]), filter(nil)
  16. 3 - output([T_FUN_SUM(t2.v1)]), filter(nil),
  17. group([t2.v1]), agg_func([T_FUN_SUM(t2.v1)])
  18. 4 - output([t2.v1]), filter(nil),
  19. access([t2.v1]), partitions(p[0-3])
  20. |

二阶段聚合操作如下例所示:

  1. | ============================================================
  2. |ID|OPERATOR |NAME |EST. ROWS|COST |
  3. ------------------------------------------------------------
  4. |0 |PX COORDINATOR | |101 |561383|
  5. |1 | EXCHANGE OUT DISTR |:EX10001|101 |561374|
  6. |2 | HASH GROUP BY | |101 |561374|
  7. |3 | EXCHANGE IN DISTR | |101 |408805|
  8. |4 | EXCHANGE OUT DISTR (HASH)|:EX10000|101 |408795|
  9. |5 | HASH GROUP BY | |101 |408795|
  10. |6 | PX PARTITION ITERATOR | |400000 |256226|
  11. |7 | TABLE SCAN |t2 |400000 |256226|
  12. ============================================================
  13. Outputs & filters:
  14. -------------------------------------
  15. 0 - output([T_FUN_SUM(T_FUN_SUM(t2.v1))]), filter(nil)
  16. 1 - output([T_FUN_SUM(T_FUN_SUM(t2.v1))]), filter(nil), dop=1
  17. 2 - output([T_FUN_SUM(T_FUN_SUM(t2.v1))]), filter(nil),
  18. group([t2.v2]), agg_func([T_FUN_SUM(T_FUN_SUM(t2.v1))])
  19. 3 - output([t2.v2], [T_FUN_SUM(t2.v1)]), filter(nil)
  20. 4 - (#keys=1, [t2.v2]), output([t2.v2], [T_FUN_SUM(t2.v1)]), filter(nil), dop=1
  21. 5 - output([t2.v2], [T_FUN_SUM(t2.v1)]), filter(nil),
  22. group([t2.v2]), agg_func([T_FUN_SUM(t2.v1)])
  23. 6 - output([t2.v1], [t2.v2]), filter(nil)
  24. 7 - output([t2.v1], [t2.v2]), filter(nil),
  25. access([t2.v1], [t2.v2]), partitions(p[0-3])

二元输入算子

二元输入算子主要考虑 join 算子的情况。对于 join 算子来说,主要基于规则来生成分布式的计划和数据重分布方法,主要有三种方式。

  • partition-wise join
    当左右表都是分区表且分区方式相同,物理分布一样,且 join 的连接条件为分区键时,可以使用以分区为单位的连接方法。如下例所示:
  1. explain select * from t2, t3 where t2.v1 = t3.v1\G
  2. Query Plan: ===========================================================
  3. |ID|OPERATOR |NAME |EST. ROWS |COST |
  4. |0 |PX COORDINATOR | |1568160000|1227554264|
  5. |1 | EXCHANGE OUT DISTR |:EX10000|1568160000|930670004 |
  6. |2 | PX PARTITION ITERATOR| |1568160000|930670004 |
  7. |3 | MERGE JOIN | |1568160000|930670004 |
  8. |4 | TABLE SCAN |t2 |400000 |256226 |
  9. |5 | TABLE SCAN |t3 |400000 |256226 |
  10. ===========================================================
  11. Outputs & filters:
  12. -------------------------------------
  13. 0 - output([t2.v1], [t2.v2], [t3.v1], [t3.v2]), filter(nil)
  14. 1 - output([t2.v1], [t2.v2], [t3.v1], [t3.v2]), filter(nil), dop=1
  15. 2 - output([t2.v1], [t2.v2], [t3.v1], [t3.v2]), filter(nil)
  16. 3 - output([t2.v1], [t2.v2], [t3.v1], [t3.v2]), filter(nil),
  17. equal_conds([t2.v1 = t3.v1]), other_conds(nil)
  18. 4 - output([t2.v1], [t2.v2]), filter(nil),
  19. access([t2.v1], [t2.v2]), partitions(p[0-3])
  20. 5 - output([t3.v1], [t3.v2]), filter(nil),
  21. access([t3.v1], [t3.v2]), partitions(p[0-3])
  • partial partition-wise join

当左右表中一个表为分区表,另一个表为非分区表,或者两者皆为分区表但是连接键仅和其中一个分区表的分区键相同的情况下,会以该分区表的分区分布为基准,重新分布另一个表的数据。计划如下例所示:

  1. explain select * from t4, t2 where t2.v1 = t4.v1\G
  2. Query Plan: ===========================================================
  3. |ID|OPERATOR |NAME |EST. ROWS|COST |
  4. -----------------------------------------------------------
  5. |0 |PX COORDINATOR | |11880 |17658|
  6. |1 | EXCHANGE OUT DISTR |:EX10001|11880 |15409|
  7. |2 | NESTED-LOOP JOIN | |11880 |15409|
  8. |3 | EXCHANGE IN DISTR | |3 |37 |
  9. |4 | EXCHANGE OUT DISTR (PKEY)|:EX10000|3 |37 |
  10. |5 | PX PARTITION ITERATOR | |3 |37 |
  11. |6 | TABLE SCAN |t4 |3 |37 |
  12. |7 | PX PARTITION ITERATOR | |3960 |2561 |
  13. |8 | TABLE SCAN |t2 |3960 |2561 |
  14. ===========================================================
  15. Outputs & filters:
  16. -------------------------------------
  17. 0 - output([t4.v1], [t4.v2], [t2.v1], [t2.v2]), filter(nil)
  18. 1 - output([t4.v1], [t4.v2], [t2.v1], [t2.v2]), filter(nil), dop=1
  19. 2 - output([t4.v1], [t4.v2], [t2.v1], [t2.v2]), filter(nil),
  20. conds(nil), nl_params_([t4.v1])
  21. 3 - output([t4.v1], [t4.v2]), filter(nil)
  22. 4 - (#keys=1, [t4.v1]), output([t4.v1], [t4.v2]), filter(nil), dop=1
  23. 5 - output([t4.v1], [t4.v2]), filter(nil)
  24. 6 - output([t4.v1], [t4.v2]), filter(nil),
  25. access([t4.v1], [t4.v2]), partitions(p[0-2])
  26. 7 - output([t2.v1], [t2.v2]), filter(nil)
  27. 8 - output([t2.v1], [t2.v2]), filter(nil),
  28. access([t2.v1], [t2.v2]), partitions(p[0-3])
  • 数据重分布

连接键和左右表的分区键都没有关系的情况下, 可以根据规则计算来选择使用 broadcast 还是 hash hash 的数据重分布方式,如下例所示:

注意:

只有在并行度大于 1 时, 以下两种 shuffle 方式才有可能被选中。

  1. explain select /*+ parallel(2)*/* from t4, t2 where t2.v2 = t4.v2
  2. Query Plan: =================================================================
  3. |ID|OPERATOR |NAME |EST. ROWS|COST |
  4. -----------------------------------------------------------------
  5. |0 |PX COORDINATOR | |11880 |396863|
  6. |1 | EXCHANGE OUT DISTR |:EX10001|11880 |394614|
  7. |2 | HASH JOIN | |11880 |394614|
  8. |3 | EXCHANGE IN DISTR | |3 |37 |
  9. |4 | EXCHANGE OUT DISTR (BROADCAST)|:EX10000|3 |37 |
  10. |5 | PX BLOCK ITERATOR | |3 |37 |
  11. |6 | TABLE SCAN |t4 |3 |37 |
  12. |7 | PX PARTITION ITERATOR | |400000 |256226|
  13. |8 | TABLE SCAN |t2 |400000 |256226|
  14. =================================================================
  15. Outputs & filters:
  16. -------------------------------------
  17. 0 - output([t4.v1], [t4.v2], [t2.v1], [t2.v2]), filter(nil)
  18. 1 - output([t4.v1], [t4.v2], [t2.v1], [t2.v2]), filter(nil), dop=2
  19. 2 - output([t4.v1], [t4.v2], [t2.v1], [t2.v2]), filter(nil),
  20. equal_conds([t2.v2 = t4.v2]), other_conds(nil)
  21. 3 - output([t4.v1], [t4.v2]), filter(nil)
  22. 4 - output([t4.v1], [t4.v2]), filter(nil), dop=2
  23. 5 - output([t4.v1], [t4.v2]), filter(nil)
  24. 6 - output([t4.v1], [t4.v2]), filter(nil),
  25. access([t4.v1], [t4.v2]), partitions(p[0-2])
  26. 7 - output([t2.v1], [t2.v2]), filter(nil)
  27. 8 - output([t2.v1], [t2.v2]), filter(nil),
  28. access([t2.v1], [t2.v2]), partitions(p[0-3])
  29. explain select /*+ pq_distribute(t2 hash hash) parallel(2)*/* from t4, t2 where t2.v2 = t4.v2\G
  30. *************************** 1. row ***************************
  31. Query Plan: ============================================================
  32. |ID|OPERATOR |NAME |EST. ROWS|COST |
  33. ------------------------------------------------------------
  34. |0 |PX COORDINATOR | |11880 |434727|
  35. |1 | EXCHANGE OUT DISTR |:EX10002|11880 |432478|
  36. |2 | HASH JOIN | |11880 |432478|
  37. |3 | EXCHANGE IN DISTR | |3 |37 |
  38. |4 | EXCHANGE OUT DISTR (HASH)|:EX10000|3 |37 |
  39. |5 | PX BLOCK ITERATOR | |3 |37 |
  40. |6 | TABLE SCAN |t4 |3 |37 |
  41. |7 | EXCHANGE IN DISTR | |400000 |294090|
  42. |8 | EXCHANGE OUT DISTR (HASH)|:EX10001|400000 |256226|
  43. |9 | PX PARTITION ITERATOR | |400000 |256226|
  44. |10| TABLE SCAN |t2 |400000 |256226|
  45. ============================================================
  46. Outputs & filters:
  47. -------------------------------------
  48. 0 - output([t4.v1], [t4.v2], [t2.v1], [t2.v2]), filter(nil)
  49. 1 - output([t4.v1], [t4.v2], [t2.v1], [t2.v2]), filter(nil), dop=2
  50. 2 - output([t4.v1], [t4.v2], [t2.v1], [t2.v2]), filter(nil),
  51. equal_conds([t2.v2 = t4.v2]), other_conds(nil)
  52. 3 - output([t4.v1], [t4.v2]), filter(nil)
  53. 4 - (#keys=1, [t4.v2]), output([t4.v1], [t4.v2]), filter(nil), dop=2
  54. 5 - output([t4.v1], [t4.v2]), filter(nil)
  55. 6 - output([t4.v1], [t4.v2]), filter(nil),
  56. access([t4.v1], [t4.v2]), partitions(p[0-2])
  57. 7 - output([t2.v1], [t2.v2]), filter(nil)
  58. 8 - (#keys=1, [t2.v2]), output([t2.v1], [t2.v2]), filter(nil), dop=2
  59. 9 - output([t2.v1], [t2.v2]), filter(nil)
  60. 10 - output([t2.v1], [t2.v2]), filter(nil),
  61. access([t2.v1], [t2.v2]), partitions(p[0-3])