sundry

OceanBase subquery

In data, sub queries can be divided into dependent sub queries and non dependent sub queries. A dependent subquery means that the execution of the subquery depends on the 'variable' of the external query, so the subquery is usually calculated multiple times. A subquery without dependency means that the execution of the subquery does not depend on the 'variable' of the external query. This subquery generally only needs to be calculated once. The following figure shows a sub query without dependency and a sub query with dependency.

  1. OceanBase ( root@test )> create table t1(a int primary key, b int, c int);   
  2. Query OK, 0 rows affected (0.70 sec)  
  3. OceanBase ( root@test )> create table t2(a int primary key, b int, c int);   
  4. Query OK, 0 rows affected (0.92 sec)  
  5. --Subquery without dependency
  6. OceanBase ( root@test )> select * from t1 where t1.a in (select t2.a from t2);   
  7. Empty  set (0.22 sec)  
  8. --For dependent sub queries, the outer query variable t1. b is used in the sub query
  9. OceanBase ( root@test )> select * from t1 where t1.a in (select t2.a from t2 where t2.b = t1.b);   
  10. Empty  set (0.05 sec)  

In Oceanbase, subqueries are executed through the subplan filter operator. The Subplan filter operator will traverse every row in the outer query, and for each row traversed, it will check the corresponding subquery to determine whether the conditions are met, which is somewhat similar to Nested Loop Join. In order to execute sub queries efficiently, sub queries that meet specific conditions will be rewritten into join statements, so that the optimizer can have more choices when selecting join algorithms (in addition to Nested Loop Join, you can also select Hash Join and Merge Join). In Oceanbase, subqueries without dependencies will be rewritten into join statements. For dependent subqueries, only when certain conditions are met will they be rewritten as join statements. The following figure shows a subquery that has not been rewritten as a join statement and a subquery that has not been rewritten as a join statement.

  1. OceanBase ( root@test )> create table t1(a int primary key, b int, c int);   
  2. Query OK, 0 rows affected (0.70 sec)  
  3. OceanBase ( root@test )> create table t2(a int primary key, b int, c int);   
  4. Query OK, 0 rows affected (0.92 sec)  
  5. --Subqueries with dependencies are rewritten as semi join, and hash semi join is used to implement
  6. OceanBase ( root@test )> explain select * from t1 where t1.a in (select t2.c from t2 where t2.b = t1.b);   
  7. | =======================================  
  8. |ID|OPERATOR      |NAME|EST.  ROWS|COST|  
  9. ---------------------------------------  
  10. |0 |HASH SEMI JOIN|    |1        |2924|  
  11. |1 | TABLE SCAN   |t1  |1000     |455 |  
  12. |2 | TABLE SCAN   |t2  |1000     |455 |  
  13. =======================================  
  14.   
  15. Outputs & filters:  
  16. -------------------------------------  
  17.   0 - output([t1.a], [t1.b], [t1.c]), filter(nil),  
  18.       equal_conds([t1.a = t2.c], [t2.b = t1.b]), other_conds(nil)  
  19.   1 - output([t1.b], [t1.a], [t1.c]), filter(nil),  
  20.       access([t1.b], [t1.a], [t1.c]), partitions(p0)  
  21.   2 - output([t2.b], [t2.c]), filter(nil),  
  22.       access([t2.b], [t2.c]), partitions(p0)  
  23. --Subqueries with dependencies cannot be rewritten as semi join. Subplan filter is used to implement
  24. OceanBase ( root@test )> explain select * from t1 where t1.a > (select sum(t2.c) from t2 where t2.b = t1.b);   
  25. |ID|OPERATOR        |NAME|EST.  ROWS|COST  |  
  26. -------------------------------------------  
  27. |0 |SUBPLAN FILTER  |    |334      |207683|  
  28. |1 | TABLE SCAN     |t1  |334      |176   |  
  29. |2 | SCALAR GROUP BY|    |1        |623   |  
  30. |3 |  TABLE SCAN    |t2  |2        |622   |  
  31. ===========================================  
  32.   
  33. Outputs & filters:  
  34. -------------------------------------  
  35.   0 - output([t1.a], [t1.b], [t1.c]), filter([t1.a > subquery(1)]),  
  36.       exec_params_([t1.b]), onetime_exprs_(nil), init_plan_idxs_(nil)  
  37.   1 - output([t1.b], [t1.a], [t1.c]), filter(nil),  
  38.       access([t1.b], [t1.a], [t1.c]), partitions(p0)  
  39.   2 - output([T_FUN_SUM(t2.c)]), filter(nil),  
  40.       group(nil), agg_func([T_FUN_SUM(t2.c)])  
  41.   3 - output([t2.c]), filter([t2.b = ?]),  
  42.       access([t2.b], [t2.c]), partitions(p0)  
fabulous ( one thousand three hundred and fourteen )

This article is written by Ji Changxin Author, article address: https://blog.isoyu.com/archives/oceanbasezichaxun.html
use Knowledge Sharing Attribution 4.0 International License Agreement. Unless the reprint/source is indicated, they are all original or translated by this website. Please sign your name before reprinting. Last editing time: September 16, 2018 at 04:36 p.m

key word:

Popular articles

Comments:

5 comments, 0 visitors, 0 bloggers
  1.  Ji Changxin
    Ji Changxin Published on:

    Morning

Post reply

[Required]

I am a human?

Please wait three seconds after submission to avoid unsubmission and repetition