OceanBase logo

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Resources

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS

OceanBase Cloud

OceanBase Database

Tools

Connectors and Middleware

QUICK START

OceanBase Cloud

OceanBase Database

BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Company

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

International - English
中国站 - 简体中文
日本 - 日本語
Sign In
Start on Cloud

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS
OceanBase CloudOceanBase Database
ToolsConnectors and Middleware
QUICK START
OceanBase CloudOceanBase Database
BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

Start on Cloud
编组
All Products
    • Databases
    • iconOceanBase Database
    • iconOceanBase Cloud
    • iconOceanBase Tugraph
    • iconInteractive Tutorials
    • iconOceanBase Best Practices
    • Tools
    • iconOceanBase Cloud Platform
    • iconOceanBase Migration Service
    • iconOceanBase Developer Center
    • iconOceanBase Migration Assessment
    • iconOceanBase Admin Tool
    • iconOceanBase Loader and Dumper
    • iconOceanBase Deployer
    • iconKubernetes operator for OceanBase
    • iconOceanBase Diagnostic Tool
    • iconOceanBase Binlog Service
    • Connectors and Middleware
    • iconOceanBase Database Proxy
    • iconEmbedded SQL in C for OceanBase
    • iconOceanBase Call Interface
    • iconOceanBase Connector/C
    • iconOceanBase Connector/J
    • iconOceanBase Connector/ODBC
    • iconOceanBase Connector/NET
icon

OceanBase Database

SQL - V4.2.1

    Download PDF

    OceanBase logo

    The Unified Distributed Database for the AI Era.

    Follow Us
    Products
    OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
    Resources
    DocsBlogWhite PaperLive DemosTraining & CertificationTicket
    Company
    About OceanBaseTrust CenterLegalPartnerContact Us
    Follow Us

    © OceanBase 2026. All rights reserved

    Cloud Service AgreementPrivacy PolicySecurity
    Contact Us
    Document Feedback
    1. Documentation Center
    2. OceanBase Database
    3. SQL
    4. V4.2.1
    iconOceanBase Database
    SQL - V 4.2.1
    Databases
    • OceanBase Database
    • OceanBase Cloud
    • OceanBase Tugraph
    • Interactive Tutorials
    • OceanBase Best Practices
    Tools
    • OceanBase Cloud Platform
    • OceanBase Migration Service
    • OceanBase Developer Center
    • OceanBase Migration Assessment
    • OceanBase Admin Tool
    • OceanBase Loader and Dumper
    • OceanBase Deployer
    • Kubernetes operator for OceanBase
    • OceanBase Diagnostic Tool
    • OceanBase Binlog Service
    Connectors and Middleware
    • OceanBase Database Proxy
    • Embedded SQL in C for OceanBase
    • OceanBase Call Interface
    • OceanBase Connector/C
    • OceanBase Connector/J
    • OceanBase Connector/ODBC
    • OceanBase Connector/NET
    SQL
    KV
    • V 4.6.0
    • V 4.4.2
    • V 4.3.5
    • V 4.3.3
    • V 4.3.1
    • V 4.3.0
    • V 4.2.5
    • V 4.2.2
    • V 4.2.1
    • V 4.2.0
    • V 4.1.0
    • V 4.0.0
    • V 3.1.4 and earlier

    JOIN

    Last Updated:2026-04-28 09:23:26  Updated
    Share
    What is on this page
    NESTED-LOOP JOIN
    MERGE JOIN
    HASH JOIN

    folded

    Share

    JOIN operators join data in two tables based on specified conditions.

    Joins are classified into three types: inner join, outer join, and semi/anti join.

    OceanBase Database supports the following JOIN operators: NESTED-LOOP JOIN, MERGE JOIN, and HASH JOIN.

    NESTED-LOOP JOIN

    In the following example, queries Q1 and Q2 use NESTED-LOOP JOIN based on the hint. Operator 0 is a NESTED-LOOP JOIN operator and has two subnodes: operators 1 and 2. The execution logic of this operator is as follows:

    1. Run Operator 1 to read a row.

    2. Run Operator 2 to read all rows.

    3. Join the result sets of the operators 1 and 2, and apply the filter condition to export the results.

    4. Repeat Step 1 until Operator 1 stops iteration.

    obclient> CREATE TABLE t1 (c1 INT, c2 INT);
    Query OK, 0 rows affected
    
    obclient> CREATE TABLE t2 (d1 INT, d2 INT, PRIMARY KEY (d1));
    Query OK, 0 rows affected
    
    Q1:
    obclient> EXPLAIN SELECT /*+USE_NL(t1, t2)*/ t1.c2 + t2.d2 FROM t1, t2 WHERE c2 = d2;
    
    Query Plan:
    ===========================================
    |ID|OPERATOR        |NAME|EST. ROWS|COST  |
    -------------------------------------------
    |0 |NESTED-LOOP JOIN|    |9782     |411238|
    |1 | TABLE SCAN     |T1  |999      |647   |
    |2 | MATERIAL       |    |999      |1519  |
    |3 |  TABLE SCAN    |T2  |999      |647   |
    ===========================================
    Outputs & filters:
    -------------------------------------
      0 - output([T1.C2 + T2.D2]), filter(nil),
          conds([T1.C2 = T2.D2]), nl_params_(nil)
      1 - output([T1.C2]), filter(nil),
          access([T1.C2]), partitions(p0)
      2 - output([T2.D2]), filter(nil)
      3 - output([T2.D2]), filter(nil),
          access([T2.D2]), partitions(p0)
    

    The MATERIAL operator materializes the data output of subsequent operators. For more information, see MATERIAL.

    Q2:
    obclient> EXPLAIN SELECT /*+USE_NL(t1, t2)*/ t1.c2 + t2.d2 FROM t1, t2 WHERE c1 = d1;
    
    Query Plan:
    | ==========================================
    |ID|OPERATOR        |NAME|EST. ROWS|COST |
    ------------------------------------------
    |0 |NESTED-LOOP JOIN|    |990      |37346|
    |1 | TABLE SCAN     |T1  |999      |669  |
    |2 | TABLE GET      |T2  |1        |36   |
    ==========================================
    Outputs & filters:
    -------------------------------------
      0 - output([T1.C2 + T2.D2]), filter(nil),
          conds(nil), nl_params_([T1.C1])
      1 - output([T1.C1], [T1.C2]), filter(nil),
          access([T1.C1], [T1.C2]), partitions(p0)
      2 - output([T2.D2]), filter(nil),
          access([T2.D2]), partitions(p0)
    

    In the preceding example, the Outputs & filters section shows in detail the output information of the NESTED-LOOP JOIN operator.

    Field
    Description
    output The output expressions of the operator.
    filter The filter conditions of the operator. In this example, filter is set to nil because no filter condition is configured for the NESTED-LOOP JOIN operator.
    conds The join conditions. For example, the join condition in query Q1 is t1.c2 = t2.d2.
    nl_params_ The pushdown parameters generated based on the data of the table on the left of the NESTED-LOOP JOIN operator. For example, the pushdown parameter in query Q2 is t1.c1. For the iteration of each row of the table on the left, NESTED-LOOP JOIN creates a parameter based on nl_params and, based on this parameter and the original join condition c1 = d1, creates a filter condition applicable to the table on the right: d1 = ?. The filter condition is pushed down to the table on the right, extracting the query range of the index. The query range is the range of data to be scanned. In query Q2, Operator 2 is a TABLE GET operator because of the pushdown condition d1=?.

    In the following example, no join condition is specified in query Q3. Operator 0 is displayed as NESTED-LOOP JOIN CARTESIAN, which is logically a NESTED-LOOP JOIN operator with no join conditions.

    Q3:
    obclient> EXPLAIN SELECT t1.c2 + t2.d2 FROM t1, t2;
    *************************** 1. row ***************************
    Query Plan:
    | =====================================================
    |ID|OPERATOR                  |NAME|EST. ROWS|COST  |
    -----------------------------------------------------
    |0 |NESTED-LOOP JOIN CARTESIAN|    |998001   |747480|
    |1 | TABLE SCAN               |T1  |999      |647   |
    |2 | MATERIAL                 |    |999      |1519  |
    |3 |  TABLE SCAN              |T2  |999      |647   |
    =====================================================
    Outputs & filters:
    -------------------------------------
      0 - output([T1.C2 + T2.D2]), filter(nil),
          conds(nil), nl_params_(nil)
      1 - output([T1.C2]), filter(nil),
          access([T1.C2]), partitions(p0)
      2 - output([T2.D2]), filter(nil)
      3 - output([T2.D2]), filter(nil),
          access([T2.D2]), partitions(p0)
    

    MERGE JOIN

    In the following example, query Q4 uses MERGE JOIN by adding the hint USE_MERGE. Operator 0 is a MERGE JOIN operator and has two subnodes: operators 1 and 3. The operator merges data of the left and right subnodes, and therefore requires that the data of the two subnodes be sorted in relation to the join column.

    Take query Q4 as an example. The join condition is t1.c2 = t2.d2, which means sorting data of t1 by c2, and data of t2 by d2. In query Q4, the output of Operator 2 is not sorted, and the output of Operator 4 is sorted by d1. Both of them do not meet the sorting requirements of a merge join. Therefore, operators 1 and 3 are assigned for sorting.

    Q4:
    obclient> EXPLAIN SELECT /*+USE_MERGE(t1, t2)*/ t1.c2 + t2.d2 FROM t1, t2 WHERE c2 = d2 AND c1 + d1 > 10;
    
    Query Plan:
    | ======================================
    |ID|OPERATOR    |NAME|EST. ROWS|COST |
    --------------------------------------
    |0 |MERGE JOIN  |    |3261     |14199|
    |1 | SORT       |    |999      |4505 |
    |2 |  TABLE SCAN|T1  |999      |669  |
    |3 | SORT       |    |999      |4483 |
    |4 |  TABLE SCAN|T2  |999      |647  |
    ======================================
    Outputs & filters:
    -------------------------------------
      0 - output([T1.C2 + T2.D2]), filter(nil),
          equal_conds([T1.C2 = T2.D2]), other_conds([T1.C1 + T2.D1 > 10])
      1 - output([T1.C2], [T1.C1]), filter(nil), sort_keys([T1.C2, ASC])
      2 - output([T1.C2], [T1.C1]), filter(nil),
          access([T1.C2], [T1.C1]), partitions(p0)
      3 - output([T2.D2], [T2.D1]), filter(nil), sort_keys([T2.D2, ASC])
      4 - output([T2.D2], [T2.D1]), filter(nil),
          access([T2.D2], [T2.D1]), partitions(p0)
    

    In the following example, the join condition in query Q5 is t1.c1 = t2.d1, which means sorting data of t1 by c1, and data of t2 by d1. In this execution plan, the primary table scan is performed for t2, and the results are sorted by d1. Therefore, it is unnecessary to assign an additional SORT operator. Ideally, proper indexes are selected for tables on the left and right of the join, and the data order specified by the indexes can meet the requirements of a merge join. In this case, no SORT operator is needed.

    Q5:
    obclient> EXPLAIN SELECT /*+USE_MERGE(t1, t2)*/ t1.c2 + t2.d2 FROM t1, t2 WHERE c1 = d1;
    
    Query Plan:
    | =====================================
    |ID|OPERATOR    |NAME|EST. ROWS|COST|
    -------------------------------------
    |0 |MERGE JOIN  |    |990      |6096|
    |1 | SORT       |    |999      |4505|
    |2 |  TABLE SCAN|T1  |999      |669 |
    |3 | TABLE SCAN |T2  |999      |647 |
    =====================================
    Outputs & filters:
    -------------------------------------
      0 - output([T1.C2 + T2.D2]), filter(nil),
          equal_conds([T1.C1 = T2.D1]), other_conds(nil)
      1 - output([T1.C2], [T1.C1]), filter(nil), sort_keys([T1.C1, ASC])
      2 - output([T1.C1], [T1.C2]), filter(nil),
          access([T1.C1], [T1.C2]), partitions(p0)
      3 - output([T2.D1], [T2.D2]), filter(nil),
          access([T2.D1], [T2.D2]), partitions(p0)
    

    In the preceding example, the Outputs & filters section shows in detail the output information of the MERGE JOIN operator.

    Field
    Description
    output The output expressions of the operator.
    filter The filter conditions of the operator. In this example, filter is set to nil because no filter condition is configured for the MERGE JOIN operator.
    equal_conds The equality join conditions for MERGE JOIN. The result sets of the subnodes on the left and right must be sorted in relation to the join column.
    other_conds Other join conditions. For example, query Q4 has an additional condition t1.c1 + t2.d1 > 10.

    HASH JOIN

    In the following example, query Q6 uses HASH JOIN based on the USE_HASH hint. Operator 0 is a HASH JOIN operator and has two subnodes: operators 1 and 2. The execution logic of this operator is as follows:

    1. Read data from the subnode on the left to generate a hash value based on the join column, such as t1.c1, and then create a hash table.

    2. Read data from the subnode on the right to generate a hash value based on the join column, such as t2.d1, and try to join with the data of t1 in the corresponding hash table.

    Q6:
    obclient> EXPLAIN SELECT /*+USE_HASH(t1, t2)*/ t1.c2 + t2.d2 FROM t1, t2 WHERE c1 = d1 AND c2 + d2 > 1;
    
    Query Plan:
    | ====================================
    |ID|OPERATOR   |NAME|EST. ROWS|COST|
    ------------------------------------
    |0 |HASH JOIN  |    |330      |4850|
    |1 | TABLE SCAN|T1  |999      |669 |
    |2 | TABLE SCAN|T2  |999      |647 |
    ====================================
    Outputs & filters:
    -------------------------------------
      0 - output([T1.C2 + T2.D2]), filter(nil),
          equal_conds([T1.C1 = T2.D1]), other_conds([T1.C2 + T2.D2 > 1])
      1 - output([T1.C1], [T1.C2]), filter(nil),
          access([T1.C1], [T1.C2]), partitions(p0)
      2 - output([T2.D1], [T2.D2]), filter(nil),
          access([T2.D1], [T2.D2]), partitions(p0)
    

    In the preceding example, the Outputs & filters section shows in detail the output information of the HASH JOIN operator.

    Field
    Description
    output The output expressions of the operator.
    filter The filter conditions of the operator. In this example, filter is set to nil because no filter condition is configured for the HASH JOIN operator.
    equal_conds The equality join conditions. The join columns on the left and right sides are used to calculate the hash value.
    other_conds Other join conditions. For example, query Q6 has an additional join condition t1.c2 + t2.d2 > 1.

    Previous topic

    TABLE SCAN
    Last

    Next topic

    COUNT
    Next
    What is on this page
    NESTED-LOOP JOIN
    MERGE JOIN
    HASH JOIN