Updated on 2026-11-06 GMT+08:00

Join

Data skew may occur in various join scenarios, such as large-small table joins, large-medium table joins, and hotspot value long-tail distribution in joins.

Large-Small Table Join

  • Data skew example

    In the following example, t1 is a large table, and t2 and t3 are small tables.

    SELECT  t1.ip
           ,t1.is_anon
           ,t1.user_id
           ,t1.user_agent
           ,t1.referer
           ,t2.ssl_ciphers
           ,t3.shop_province_name
           ,t3.shop_city_name
    FROM    viewtable t1
    LEFT OUTER JOIN other_viewtable t2
           ON t1.header_eagleeye_traceid = t2.eagleeye_traceid
    LEFT OUTER JOIN (
               SELECT  shop_id
                      ,city_name AS shop_city_name
                      ,province_name AS shop_province_name
               FROM    tenanttable
               WHERE   ds = (SELECT max(ds) FROM tenanttable)
               AND     is_valid = 1
           ) t3
           ON t1.shopid = t3.shop_id
  • Solution

    Use query optimization hints to broadcast small tables. The following is an example:

    SELECT  /*+ BROADCAST(t2, t3) */
            t1.ip
           ,t1.is_anon
           ,t1.user_id
           ,t1.user_agent
           ,t1.referer
           ,t2.ssl_ciphers
           ,t3.shop_province_name
           ,t3.shop_city_name
    FROM    viewtable t1
    LEFT OUTER JOIN other_viewtable t2
           ON t1.header_eagleeye_traceid = t2.eagleeye_traceid
    LEFT OUTER JOIN (
               SELECT  shop_id
                      ,city_name AS shop_city_name
                      ,province_name AS shop_province_name
               FROM    tenanttable
               WHERE   ds = (SELECT max(ds) FROM tenanttable)
               AND     is_valid = 1
           ) t3
           ON t1.shopid = t3.shop_id
  • Precautions
    • Spark can automatically trigger broadcast. The threshold parameter is spark.sql.autoBroadcastJoinThreshold=10485760 (the default value is 10 MB). Manual hints are not restricted by the threshold parameter. Pay attention to the driver/executor memory bloat caused by the broadcast.
    • When referencing a small table or subquery, you need to use its alias.

Hotspot Value Long-Tail Distribution in a Join

  • Data skew example

    In the following table, eleme_uid contains a large amount of hotspot data, which can easily cause data skew.

    SELECT
        eleme_uid,
        ...
    FROM (
        SELECT
            eleme_uid,
            ...
        FROM <viewtable>
    ) t1
    LEFT JOIN (
        SELECT
            eleme_uid,
            ...
        FROM <customertable>
    ) t2
    ON t1.eleme_uid = t2.eleme_uid;
  • Solution

    You can resolve the issue using any of the following three methods.

    No.

    Solution

    Description

    Solution 1

    Manual splitting of hotspot values

    Filter hotspot values from the primary table, perform broadcast join on them, perform sort-merge join on the other non-hotspot records, and then perform UNION ALL.

    Solution 2

    AQE Skew Join / SkewJoin Hint

    Use AQE that automatically detects and splits skewed keys. Alternatively, use /*+ SKEW */ Hint.

    Solution 3

    Modulo operation on a multiplication table for equal join

    Use a multiplication table to multiply hotspot values by N, and use the user ID and number as join keys.

    • Manual splitting of hotspot values

      After obtaining hotspot values, filter them from the primary table, perform broadcast on them, perform sort-merge join on non-hotspot values, and combine the join results. The following is example code:

      -- Perform Broadcast Join on hotspot values.
      SELECT /*+ BROADCAST(t2) */
             eleme_uid, ...
      FROM (SELECT eleme_uid, ... FROM viewtable WHERE eleme_uid = <skewed_value>) t1
      LEFT JOIN (SELECT eleme_uid, ... FROM customertable WHERE eleme_uid = <skewed_value>) t2
             ON t1.eleme_uid = t2.eleme_uid
      UNION ALL
      
      -- Perform Sort Merge Join on non-hotspot values.
      SELECT eleme_uid, ...
      FROM (SELECT eleme_uid, ... FROM viewtable WHERE eleme_uid != <skewed_value>) t3
      LEFT JOIN (SELECT eleme_uid, ... FROM customertable WHERE eleme_uid != <skewed_value>) t4
             ON t3.eleme_uid = t4.eleme_uid
    • AQE Skew Join/SkewJoin Hint

      You are advised to enable AQE Skew Join by setting the following parameters:

      spark.sql.adaptive.enabled=true
      spark.sql.adaptive.skewJoin.enabled=true

      Spark also allows you to manually specify skew values using query optimization hints. The following is an example:

      SELECT /*+ SKEW('a', 'c0', (1, '2'), (3, '4')) */ *
      FROM T0 a JOIN T1 b
        ON a.c0 = b.c0 AND a.c1 = b.c1 AND a.c2 = b.c2;

      Supported join types:

      • Inner Join allows you to hint either table.
      • Left Join, Semi Join, and Anti Join allow you to hint only the left table.
      • Right Join allows you to hint only the right table.
      • Full Join does not support Skew Join Hint.

      You are advised to add hints only to joins where data skew is certain to occur because hints will run an aggregate operation, which incurs some costs. The join key types must be the same (the field types on both sides must be the same). Otherwise, a hint does not take effect. You can use CAST to unify the type in a subquery.

    • Modulo equality join using a multiplication table

      This solution is different from the other two in that it uses a multiplication table with only one column of values, that is, an int column. For example, the values in the column can range from 1 to N (the specific range depends on the skew degree). This multiplication table can be used to enlarge the user behavior table by N times. During a join, the user ID and number are used as join keys. The addition of the number join condition reduces the data skew caused by distribution based only on the user ID to 1/N of the original value. However, this approach also expands data volume by N times.

      SELECT eleme_uid, ...
      FROM (SELECT eleme_uid, ... FROM viewtable) t1
      LEFT JOIN (SELECT /*+ BROADCAST(multipletable) */
                        eleme_uid, number, ...
                 FROM customertable JOIN multipletable) t2
             ON t1.eleme_uid = t2.eleme_uid
            AND mod(t1.value_col, 10) + 1 = t2.number