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

Group By

The following is an example pseudocode snippet with Group By:

SELECT  shop_id
       ,sum(is_open) AS Business_days
FROM    table_xxx_di
WHERE   dt BETWEEN '${bizdate_365}' AND '${bizdate}'
GROUP BY shop_id;

The following table lists the two solutions to data skew.

No.

Solution

Description

Solution 1

Add random numbers.

Split the keys that cause the long tail.

Solution 2

Create a cumulative table.

Reduce costs and improve efficiency.

Solution 1: Add random numbers.

Rewrite the SQL statement, add random numbers, and split the keys that cause long tails. This is an effective solution to the long tail caused by GROUP BY.

SELECT Key, Count(*) AS Cnt FROM TableName GROUP BY Key;

If the combiner is not considered, the M node shuffles data to the R node, and then the R node performs the count operation. The corresponding execution plan is M->R.

Assume that the key causing the long tail has been found. Reallocate the key.

-- Assume that the key causing the long tail is KEY001.
SELECT  a.Key
       ,SUM(a.Cnt) AS Cnt
FROM (
    SELECT  Key
           ,COUNT(*) AS Cnt
    FROM    TableName
    GROUP BY Key
           ,CASE WHEN Key = 'KEY001' THEN pmod(hash(rand()), 50)
                 ELSE 0
            END
) a
GROUP BY a.Key;

After the change, the execution plan changes to M->R->R. Although the execution path is longer, the overall time consumption may be reduced because the key is processed in two steps. This solution consumes almost the same resources and time as the direct aggregation without random numbers. However, there is usually more than one key causing a long tail. Considering the costs of locating the keys and rewriting SQL statements, adding random numbers to the keys causing a long tail (without the two-phase rewrite) is more cost-effective.

Solution 2: Create a cumulative table.

To obtain merchant data of the past year through an online task, all partitions from T-1 to T-365 must be read, causing a huge waste of resources. Creating a cumulative table can reduce the number of partitions to be read without affecting the acquisition of data from the past year. This reduces costs and improves efficiency. The following is an example:

During the initialization, merchant business data of 365 days is summarized using Group By. The data update date is marked, and the result is recorded as table a. In subsequent online tasks, table a of the T-2 day is associated with the table_xxx_di table and then summarized using Group By. In this way, the number of data records read each day is reduced from 365 to 2, which greatly reduces the repetition of the primary key shopid and resource consumption.

-- Create a cumulative table.
CREATE TABLE IF NOT EXISTS m_xxx_365_df (
   shop_id STRING COMMENT 'Merchant ID',
   last_update_ds STRING COMMENT 'Last update date',
   `365d_open_days` BIGINT COMMENT 'Number of business days in 365 days'
)
PARTITIONED BY (ds STRING COMMENT 'Date partition');

-- Assume that 365 days are from May 1, 2021 to May 1, 2022. Perform initialization first.
INSERT OVERWRITE TABLE m_xxx_365_df PARTITION(ds = '20220501')
SELECT shop_id
      ,max(ds) AS last_update_ds
      ,sum(is_open) AS `365d_open_days`
FROM table_xxx_di
WHERE ds BETWEEN '20210501' AND '20220501'
GROUP BY shop_id;

-- The online task executes the following statements:
INSERT OVERWRITE TABLE m_xxx_365_df PARTITION(ds = '${bizdate}')
SELECT aa.shop_id
      ,aa.last_update_ds
      , `365d_open_days` - COALESCE(is_open, 0) AS `365d_open_days` -- Eliminate the infinite accumulation of business days.
FROM (
    SELECT shop_id
          ,max(last_update_ds) AS last_update_ds
          ,sum(`365d_open_days`) AS `365d_open_days`
    FROM (
        SELECT shop_id
              ,ds AS last_update_ds
              ,sum(is_open) AS `365d_open_days`
        FROM table_xxx_di
        WHERE ds = '${bizdate}'
        GROUP BY shop_id
        UNION ALL
        SELECT shop_id
              ,last_update_ds
              ,`365d_open_days`
        FROM m_xxx_365_df
        WHERE ds = '${bizdate_2}' AND last_update_ds >= '${bizdate_365}'
        GROUP BY shop_id
    )
    GROUP BY shop_id
) AS aa
LEFT JOIN (
    SELECT shop_id, is_open
    FROM table_xxx_di
    WHERE ds = '${bizdate_366}'
) AS bb
ON aa.shop_id = bb.shop_id;