Skip to content

TiDB does not eliminate duplicate GROUP BY expressions, causing a measurable slowdown #69925

Description

@chen8908917

Bug Report

Please answer these questions before submitting your issue. Thanks!
The original query and the mutated query are logically equivalent. The only difference is that the mutated query adds a duplicate expression to the GROUP BY list.

TiDB v8.5.7 keeps the duplicated grouping key in the execution plan instead of removing it. That makes aggregation work more expensive, and the mutated query is consistently slower in local reruns.

Observed timing over 30 alternating executions with the minimized SQL:

original median = 16.546 ms
mutated  median = 27.604 ms
ratio = 1.6684x slower

1. Minimal reproduce step (Required)

DROP DATABASE IF EXISTS tidb_gb_min;
CREATE DATABASE tidb_gb_min;
USE tidb_gb_min;

CREATE TABLE t (
  c1 INT NOT NULL,
  c6 MEDIUMTEXT NOT NULL
);

INSERT INTO t VALUES (8945, REPEAT('a', 734));

-- Original query
SELECT COUNT(*)
FROM t
GROUP BY REPEAT(c6, c1);

-- Mutated query: duplicate GROUP BY expression
SELECT COUNT(*)
FROM t
GROUP BY REPEAT(c6, c1), REPEAT(c6, c1);

-- Optional: inspect the plan difference
EXPLAIN
SELECT COUNT(*)
FROM t
GROUP BY REPEAT(c6, c1);

EXPLAIN
SELECT COUNT(*)
FROM t
GROUP BY REPEAT(c6, c1), REPEAT(c6, c1);

2. What did you expect to see? (Required)

Both queries should produce the same result and similar execution time.

3. What did you see instead (Required)

Original plan:

HashAgg_5
  group by:Column#5

Mutated plan:

HashAgg_5
  group by:Column#5, Column#5

The duplicate grouping key is preserved in the mutated plan.

4. What is your TiDB version? (Required)

Release Version: v8.5.7
Edition: Community
Git Commit Hash: 202b7f47286a1109b5c957401d34c9358d130ae0
Git Branch: HEAD
UTC Build Time: 2026-07-15 02:06:00
GoVersion: go1.25.10
Race Enabled: false
Check Table Before Drop: false
Store: unistore

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions