pingcap / pingcap/tidb

optimizer: unnecessary CARTESIAN inner join is picked by optimizer

Open
#60,721 2 comments 0 reactions 1 assignee Claimed by @hawkingrei View on GitHub
severity/moderate sig/planner tiara type/enhancement
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

### 1. Minimal reproduce step (Required)

```sql
create table tb (
pk int primary key,
uk1 int unique key,
uk2 int unique key,
a int,
b varchar(100)
);

explain select * from tb tb1, tb tb2
where tb1.uk1 = tb2.pk or tb1.uk1 = tb2.uk2;
```

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

The execution plan should do one full scan of table tb, and then use primary key and unique key to complete the join.

### 3. What did you see instead (Required)

unnecessary cartesian join

```sql
mysql> explain select * from tb tb1, tb tb2
-> where tb1.uk1 = tb2.pk or tb1.uk1 = tb2.uk2;
+-----------------------------+--------------+-----------+---------------+------------------------------------------------------------------------------------------------+
| id | estRows | task | access object | operator info |
+-----------------------------+--------------+-----------+---------------+------------------------------------------------------------------------------------------------+
| HashJoin_8 | 100000000.00 | root | | CARTESIAN inner join, other cond:or(eq(test.tb.uk1, test.tb.pk), eq(test.tb.uk1, test.tb.uk2)) |
| ├─TableReader_13(Build) | 10000.00 | root | | data:TableFullScan_12 |
| │ └─TableFullScan_12 | 10000.00 | cop[tikv] | table:tb2 | keep order:false, stats:pseudo |
| └─TableReader_11(Probe) | 10000.00 | root | | data:TableFullScan_10 |
| └─TableFullScan_10 | 10000.00 | cop[tikv] | table:tb1 | keep order:false, stats:pseudo |
+-----------------------------+--------------+-----------+---------------+------------------------------------------------------------------------------------------------+

```
### 4. What is your TiDB version? (Required)

v7.5.5

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.