Execution plan of some embed subquery is not optimal
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Enhancement
The next two SQL have the same effect but has different execution plan.
```sql
mysql> explain select * from t1 a where exists ( select 1 from t1 b where exists (select 1 from t2 c where a.id = b.id));
+-------------------------------+---------+-----------+---------------+--------------------------------+
| id | estRows | task | access object | operator info |
+-------------------------------+---------+-----------+---------------+--------------------------------+
| Apply_14 | 4.00 | root | | CARTESIAN semi join |
| ├─TableReader_16(Build) | 4.00 | root | | data:TableFullScan_15 |
| │ └─TableFullScan_15 | 4.00 | cop[tikv] | table:a | keep order:false, stats:pseudo |
| └─HashJoin_17(Probe) | 0.01 | root | | CARTESIAN semi join |
| ├─TableReader_22(Build) | 12.00 | root | | data:TableFullScan_21 |
| │ └─TableFullScan_21 | 12.00 | cop[tikv] | table:c | keep order:false, stats:pseudo |
| └─TableReader_20(Probe) | 0.02 | root | | data:Selection_19 |
| └─Selection_19 | 0.02 | cop[tikv] | | eq(test.t1.id, test.t1.id) |
| └─TableFullScan_18 | 16.00 | cop[tikv] | table:b | keep order:false, stats:pseudo |
+-------------------------------+---------+-----------+---------------+--------------------------------+
9 rows in set (0.00 sec)
mysql> explain select * from t1 a where exists ( select 1 from t1 b where a.id = b.id and exists (select 1 from t2 c));
+------------------------------+---------+-----------+---------------+-----------------------------------------------+
| id | estRows | task | access object | operator info |
+------------------------------+---------+-----------+---------------+-----------------------------------------------+
| HashJoin_13 | 3.20 | root | | semi join, equal:[eq(test.t1.id, test.t1.id)] |
| ├─TableReader_19(Build) | 4.00 | root | | data:Selection_18 |
| │ └─Selection_18 | 4.00 | cop[tikv] | | not(isnull(test.t1.id)) |
| │ └─TableFullScan_17 | 4.00 | cop[tikv] | table:b | keep order:false, stats:pseudo |
| └─TableReader_16(Probe) | 4.00 | root | | data:Selection_15 |
| └─Selection_15 | 4.00 | cop[tikv] | | not(isnull(test.t1.id)) |
| └─TableFullScan_14 | 4.00 | cop[tikv] | table:a | keep order:false, stats:pseudo |
+------------------------------+---------+-----------+---------------+-----------------------------------------------+
7 rows in set (0.00 sec)
```
Contributor guide
Assessment
This issue has not been assessed yet.