在多表联接的场景中,优化器的一个很重要的任务是决定各个表之间的联接顺序(Join Order),因为不同的联接顺序会影响中间结果集的大小,进而影响到计划整体的执行代价。

    为了减少执行计划的搜索空间和计划执行的内存占用,OceanBase 数据库优化器在生成联接顺序时主要考虑左深树的联接形式。下图展示了左深树、右深树和多支树的计划形状。

    数

    OceanBase 数据库联接顺序的生成采用了 System-R 的动态规划算法,考虑的因素包括每一个表可能的访问路径、Interesting Order、联接算法(NESTED-LOOP、BLOCK-BASED NESTED-LOOP 或者 SORT-MERGE 等)以及不同表之间的联接选择率等。

    给定 N 个表的联接,OceanBase 数据库生成联接顺序的方法如下:

    1. 为每一个基表生成访问路径,保留代价最小的访问路径以及有所有有 Interesting Order 的路径。一个路径 如果具有 Interesting Order,它的序能够被后续的算子使用。

    2. 生成所有表集合的大小为 i (1 < i <= N) 的计划。 OceanBase 数据库一般只考虑左深树,表集合大小为 i 的计划可以由一个表集合大小为 i 的计划和一个基表的计划组成。OceanBase 数据库按照这种策略,考虑了所有的联接算法以及 Interesting Order 的继承等因素把所有表集合大小为 i 的计划生成。这里也只是保留代价最小的计划以及所有具有 Interesting Order 的计划。

    同时,OceanBase 数据库提供了 HINT 机制 /*+LEADING(table_name_list)*/去控制多表联接的顺序。

    如下例所示,开始选择的联接顺序是先做 t1、t2 的 JOIN 联接,然后再和 t3 做 JOIN 联接;如果用户希望先做 t2、t3 的 JOIN 联接,然后再和 t1做 JOIN 联接,则可以使用 HINT /*+LEADING(t2,t3,t1)*/去控制;如果用户希望先做 t1、t3 的 JOIN 联接,然后再和 t2 做 JOIN 联接,则可以使用 HINT /*+LEADING(t1,t3,t2)*/去控制。

    1. obclient>CREATE TABLE t1(c1 INT, c2 INT, PRIMARY KEY(c1));
    2. Query OK, 0 rows affected (0.31 sec)
    3. obclient>CREATE TABLE t2(c1 INT, c2 INT, PRIMARY KEY(c1));
    4. Query OK, 0 rows affected (0.33 sec)
    5. obclient>CREATE TABLE t3(c1 INT, c2 INT, PRIMARY KEY(c1));
    6. Query OK, 0 rows affected (0.44 sec)
    7. obclient>EXPLAIN SELECT * FROM t1,t2,t3 WHERE t1.c1 = t2.c2 AND t2.c1 = t3.c2;
    8. +-----------------------------------------------------------------+
    9. | Query Plan |
    10. +-----------------------------------------------------------------+
    11. | =======================================
    12. |ID|OPERATOR |NAME|EST. ROWS|COST |
    13. ---------------------------------------
    14. |0 |HASH JOIN | |98010 |926122|
    15. |1 | TABLE SCAN |T3 |100000 |61860 |
    16. |2 | HASH JOIN | |99000 |494503|
    17. |3 | TABLE SCAN|T1 |100000 |61860 |
    18. |4 | TABLE SCAN|T2 |100000 |61860 |
    19. =======================================
    20. Outputs & filters:
    21. -------------------------------------
    22. 0 - output([T1.C1], [T1.C2], [T2.C1], [T2.C2], [T3.C1], [T3.C2]), filter(nil),
    23. equal_conds([T2.C1 = T3.C2]), other_conds(nil)
    24. 1 - output([T3.C2], [T3.C1]), filter(nil),
    25. access([T3.C2], [T3.C1]), partitions(p0)
    26. 2 - output([T1.C1], [T1.C2], [T2.C1], [T2.C2]), filter(nil),
    27. equal_conds([T1.C1 = T2.C2]), other_conds(nil)
    28. 3 - output([T1.C1], [T1.C2]), filter(nil),
    29. access([T1.C1], [T1.C2]), partitions(p0)
    30. 4 - output([T2.C2], [T2.C1]), filter(nil),
    31. access([T2.C2], [T2.C1]), partitions(p0)
    32. obclient>EXPLAIN SELECT /*+LEADING(t2,t3,t1)*/* FROM t1,t2,t3 WHERE t1.c1 = t2.c2
    33. AND t2.c1 = t3.c2;
    34. +-----------------------------------------------------------------+
    35. | Query Plan |
    36. +-----------------------------------------------------------------+
    37. | ========================================
    38. |ID|OPERATOR |NAME|EST. ROWS|COST |
    39. ----------------------------------------
    40. |0 |HASH JOIN | |98010 |1096613|
    41. |1 | HASH JOIN | |99000 |494503 |
    42. |2 | TABLE SCAN|T2 |100000 |61860 |
    43. |3 | TABLE SCAN|T3 |100000 |61860 |
    44. |4 | TABLE SCAN |T1 |100000 |61860 |
    45. ========================================
    46. Outputs & filters:
    47. -------------------------------------
    48. 0 - output([T1.C1], [T1.C2], [T2.C1], [T2.C2], [T3.C1], [T3.C2]), filter(nil),
    49. equal_conds([T1.C1 = T2.C2]), other_conds(nil)
    50. 1 - output([T2.C1], [T2.C2], [T3.C1], [T3.C2]), filter(nil),
    51. equal_conds([T2.C1 = T3.C2]), other_conds(nil)
    52. 2 - output([T2.C2], [T2.C1]), filter(nil),
    53. access([T2.C2], [T2.C1]), partitions(p0)
    54. 3 - output([T3.C2], [T3.C1]), filter(nil),
    55. access([T3.C2], [T3.C1]), partitions(p0)
    56. 4 - output([T1.C1], [T1.C2]), filter(nil),
    57. access([T1.C1], [T1.C2]), partitions(p0)
    58. obclient>EXPLAIN SELECT /*+LEADING(t1,t3,t2)*/* FROM t1,t2,t3 WHERE t1.c1 = t2.c2
    59. AND t2.c1 = t3.c2;
    60. +-----------------------------------------------------------------+
    61. | Query Plan |
    62. +-----------------------------------------------------------------+
    63. | =============================================================
    64. |ID|OPERATOR |NAME|EST. ROWS |COST |
    65. -------------------------------------------------------------
    66. |0 |HASH JOIN | |98010 |53098071243|
    67. |1 | NESTED-LOOP JOIN CARTESIAN| |10000000000|7964490204 |
    68. |2 | TABLE SCAN |T1 |100000 |61860 |
    69. |3 | MATERIAL | |100000 |236426 |
    70. |4 | TABLE SCAN |T3 |100000 |61860 |
    71. |5 | TABLE SCAN |T2 |100000 |61860 |
    72. =============================================================
    73. Outputs & filters:
    74. -------------------------------------
    75. 0 - output([T1.C1], [T1.C2], [T2.C1], [T2.C2], [T3.C1], [T3.C2]), filter(nil),
    76. equal_conds([T1.C1 = T2.C2], [T2.C1 = T3.C2]), other_conds(nil)
    77. 1 - output([T1.C1], [T1.C2], [T3.C1], [T3.C2]), filter(nil),
    78. conds(nil), nl_params_(nil)
    79. 2 - output([T1.C1], [T1.C2]), filter(nil),
    80. access([T1.C1], [T1.C2]), partitions(p0)
    81. 3 - output([T3.C1], [T3.C2]), filter(nil)
    82. 4 - output([T3.C2], [T3.C1]), filter(nil),
    83. access([T3.C2], [T3.C1]), partitions(p0)
    84. 5 - output([T2.C2], [T2.C1]), filter(nil),
    85. access([T2.C2], [T2.C1]), partitions(p0)