MAPJOIN HINT

Updated at:

When you perform a join operation on a large table and one or more small tables, you can add a mapjoin hint to the select statement to improve query performance. This topic describes how to use the mapjoin hint.

Function introduction

A standard JOIN process includes three stages: Map, Shuffle, and Reduce. Typically, the join operation is performed in the Reduce stage.

  • A mapjoin performs the join operation in the Map stage instead of the Reduce stage. This approach shortens data transmission time, improves system resource utilization, and optimizes the job.

  • When you perform a join operation between a large table and one or more small tables, mapjoin loads all specified small tables into the memory of the program that executes the join operation, which completes the table join during the Map phase to accelerate the join execution.

  • MaxCompute SQL does not support complex join conditions, such as non-equi join expressions or OR logic, in the ON clause of a standard join. However, you can use these operations in a mapjoin.

Limits

The mapjoin operation is subject to the following limits:

  • A mapjoin loads all data from the specified small tables into memory during the Map stage. The total size of these tables in memory cannot exceed 512 MB. Because MaxCompute uses storage compression, the data volume of a small table expands significantly after it is loaded into memory.

    Note

    The 512 MB limit refers to the memory occupied by a small table after it is loaded and decompressed, not the size of its compressed file in MaxCompute.

  • The join operation in a mapjoin is subject to the following limits:

    • The left table in a left outer join must be the large table.

    • The right table in a right outer join must be the large table.

    • full outer join is not supported.

    • For an inner join, either the left or right table can be the large table.

  • mapjoin supports a maximum of 128 small tables. If you exceed this limit, a syntax error occurs.

Usage method

To execute a mapjoin, add the /*+ mapjoin(<table_name>) */ hint to the select statement. Take note of the following:

  • You must use an alias when you reference a small table or a subquery.

  • mapjoin supports using a subquery as a small table.

  • In a mapjoin, you can use non-equi joins or OR to combine multiple conditions. You can calculate a Cartesian product by omitting the ON clause and using the mapjoin on 1 = 1 format. For example, select /*+ mapjoin(a) */ a.id from shop a join table_name b on 1=1. However, this operation can cause the data volume to expand.

  • In a mapjoin hint, you can specify multiple small tables by separating them with commas (,), for example, /*+ mapjoin(a,b,c)*/.

Note

At execution time, the query optimizer often converts some subqueries, such as scalar, IN, NOT IN, EXISTS, or NOT EXISTS subqueries, into JOIN operations. MAPJOIN is an efficient broadcast join algorithm. If you determine that the result of a subquery is a small table, you can add a hint to the subquery to explicitly use the MAPJOIN algorithm. For more information, see SUBQUERY_MAPJOIN HINT.

Sample data

-- Create a partitioned table named sale_detail.
CREATE TABLE IF NOT EXISTS sale_detail (
  shop_name     STRING,
  customer_id   STRING,
  total_price   DOUBLE
)
PARTITIONED BY (
  sale_date STRING,
  region STRING
);

CREATE TABLE IF NOT EXISTS sale_detail_sj (
  shop_name     STRING,
  customer_id   STRING,
  total_price   DOUBLE
)
PARTITIONED BY (
  sale_date STRING,
  region STRING
);

-- Add partitions to the source tables.
ALTER TABLE sale_detail ADD PARTITION (sale_date='2026', region='china');
ALTER TABLE sale_detail_sj ADD PARTITION (sale_date='2026', region='china');

-- Insert data into the source tables.
INSERT INTO sale_detail PARTITION (sale_date='2026', region='china') VALUES ('s1','c1',100.1),('s2','c2',100.2),('s3','c3',100.3);
INSERT INTO sale_detail_sj PARTITION (sale_date='2026', region='china') VALUES ('s1','c1',100.1),('s2','c2',100.2),('s5','c2',100.2),('s2','c2',100.2);

Usage example

Perform a join operation on the sale_detail and sale_detail_sj tables. The join condition is that the total_price in sale_detail_sj is less than the total_price in sale_detail, or the sum of the total_price values from both tables is less than 500. The command is as follows:

--Allow a full table scan on a partitioned table.
SET odps.sql.allow.fullscan=true;

-- Use mapjoin for the query.
select /*+ mapjoin(a) */
        a.shop_name,
        a.total_price,
        b.total_price
from sale_detail_sj a join sale_detail b
on a.total_price < b.total_price or a.total_price + b.total_price < 500;

The following result is returned:

+-----------+-------------+--------------+
| shop_name | total_price | total_price2 |
+-----------+-------------+--------------+
| s1        | 100.1       | 100.1        |
| s2        | 100.2       | 100.1        |
| s5        | 100.2       | 100.1        |
| s2        | 100.2       | 100.1        |
| s1        | 100.1       | 100.2        |
| s2        | 100.2       | 100.2        |
| s5        | 100.2       | 100.2        |
| s2        | 100.2       | 100.2        |
| s1        | 100.1       | 100.3        |
| s2        | 100.2       | 100.3        |
| s5        | 100.2       | 100.3        |
| s2        | 100.2       | 100.3        |
+-----------+-------------+--------------+

For more information about how to use mapjoin with multiple small tables, see How to use MAPJOIN to cache multiple small tables.