The inner query can't be used as the driving table after using SEMI_JOIN_REWRITE() to rewrite the semi-join query
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Bug Report
Please answer these questions before submitting your issue. Thanks!
### 1. Minimal reproduce step (Required)
```
mysql> create table t(a int primary key, b int);
mysql> create table t1(a int primary key, b int);
mysql> create table t2(a int primary key, b int);
mysql> explain select * from t where not exists (select 1 from t2 where t2.a=t.a) and exists (select /*+ SEMI_JOIN_REWRITE() */ 1 FROM t1 where t1.a=t.a);
+--------------------------------+----------+-----------+---------------+----------------------------------------------------------------+
| id | estRows | task | access object | operator info |
+--------------------------------+----------+-----------+---------------+----------------------------------------------------------------+
| MergeJoin_16 | 8000.00 | root | | inner join, left key:cmpddb.t.a, right key:cmpddb.t1.a |
| ├─StreamAgg_36(Build) | 8000.00 | root | | group by:cmpddb.t1.a, funcs:firstrow(cmpddb.t1.a)->cmpddb.t1.a |
| │ └─TableReader_37 | 8000.00 | root | | data:StreamAgg_33 |
| │ └─StreamAgg_33 | 8000.00 | cop[tikv] | | group by:cmpddb.t1.a, |
| │ └─TableFullScan_35 | 10000.00 | cop[tikv] | table:t1 | keep order:true, stats:pseudo |
| └─MergeJoin_19(Probe) | 8000.00 | root | | anti semi join, left key:cmpddb.t.a, right key:cmpddb.t2.a |
| ├─TableReader_32(Build) | 10000.00 | root | | data:TableFullScan_31 |
| │ └─TableFullScan_31 | 10000.00 | cop[tikv] | table:t2 | keep order:true, stats:pseudo |
| └─TableReader_30(Probe) | 10000.00 | root | | data:TableFullScan_29 |
| └─TableFullScan_29 | 10000.00 | cop[tikv] | table:t | keep order:true, stats:pseudo |
+--------------------------------+----------+-----------+---------------+----------------------------------------------------------------+
10 rows in set (0.00 sec)
```
After adding leading hint, it looks like there is no warning, but it does not work.
```
mysql> explain select /*+ leading(t1@sel_3, t) */ * from t where not exists (select 1 from t2 where t2.a=t.a) and exists (select /*+ SEMI_JOIN_REWRITE() */ 1 FROM t1 where t1.a=t.a);
+--------------------------------+----------+-----------+---------------+----------------------------------------------------------------+
| id | estRows | task | access object | operator info |
+--------------------------------+----------+-----------+---------------+----------------------------------------------------------------+
| MergeJoin_16 | 8000.00 | root | | inner join, left key:cmpddb.t.a, right key:cmpddb.t1.a |
| ├─StreamAgg_36(Build) | 8000.00 | root | | group by:cmpddb.t1.a, funcs:firstrow(cmpddb.t1.a)->cmpddb.t1.a |
| │ └─TableReader_37 | 8000.00 | root | | data:StreamAgg_33 |
| │ └─StreamAgg_33 | 8000.00 | cop[tikv] | | group by:cmpddb.t1.a, |
| │ └─TableFullScan_35 | 10000.00 | cop[tikv] | table:t1 | keep order:true, stats:pseudo |
| └─MergeJoin_19(Probe) | 8000.00 | root | | anti semi join, left key:cmpddb.t.a, right key:cmpddb.t2.a |
| ├─TableReader_32(Build) | 10000.00 | root | | data:TableFullScan_31 |
| │ └─TableFullScan_31 | 10000.00 | cop[tikv] | table:t2 | keep order:true, stats:pseudo |
| └─TableReader_30(Probe) | 10000.00 | root | | data:TableFullScan_29 |
| └─TableFullScan_29 | 10000.00 | cop[tikv] | table:t | keep order:true, stats:pseudo |
+--------------------------------+----------+-----------+---------------+----------------------------------------------------------------+
10 rows in set (0.00 sec)
```
But after changing the order of `exists` and `not exists` , it works automatically.
```
mysql> explain select * from t where exists (select /*+ SEMI_JOIN_REWRITE() */ 1 FROM t1 where t1.a=t.a) and not exists (select 1 from t2 where t2.a=t.a);
+----------------------------------+----------+-----------+---------------+----------------------------------------------------------------+
| id | estRows | task | access object | operator info |
+----------------------------------+----------+-----------+---------------+----------------------------------------------------------------+
| MergeJoin_16 | 8000.00 | root | | anti semi join, left key:cmpddb.t.a, right key:cmpddb.t2.a |
| ├─TableReader_38(Build) | 10000.00 | root | | data:TableFullScan_37 |
| │ └─TableFullScan_37 | 10000.00 | cop[tikv] | table:t2 | keep order:true, stats:pseudo |
| └─MergeJoin_27(Probe) | 10000.00 | root | | inner join, left key:cmpddb.t.a, right key:cmpddb.t1.a |
| ├─StreamAgg_33(Build) | 8000.00 | root | | group by:cmpddb.t1.a, funcs:firstrow(cmpddb.t1.a)->cmpddb.t1.a |
| │ └─TableReader_34 | 8000.00 | root | | data:StreamAgg_30 |
| │ └─StreamAgg_30 | 8000.00 | cop[tikv] | | group by:cmpddb.t1.a, |
| │ └─TableFullScan_32 | 10000.00 | cop[tikv] | table:t1 | keep order:true, stats:pseudo |
| └─TableReader_29(Probe) | 10000.00 | root | | data:TableFullScan_28 |
| └─TableFullScan_28 | 10000.00 | cop[tikv] | table:t | keep order:true, stats:pseudo |
+----------------------------------+----------+-----------+---------------+----------------------------------------------------------------+
10 rows in set (0.01 sec)
```
And after adding the inl_join hint, it looks like it's working, but there is a warning.
```
mysql> explain select /*+ inl_join(t) */ * from t where exists (select /*+ SEMI_JOIN_REWRITE() */ 1 FROM t1 where t1.a=t.a) and not exists (select 1 from t2 where t2.a=t.a);
+----------------------------------+----------+-----------+---------------+-----------------------------------------------------------------------------------------------------------------------+
| id | estRows | task | access object | operator info |
+----------------------------------+----------+-----------+---------------+-----------------------------------------------------------------------------------------------------------------------+
| HashJoin_26 | 8000.00 | root | | anti semi join, equal:[eq(cmpddb.t.a, cmpddb.t2.a)] |
| ├─TableReader_67(Build) | 10000.00 | root | | data:TableFullScan_66 |
| │ └─TableFullScan_66 | 10000.00 | cop[tikv] | table:t2 | keep order:false, stats:pseudo |
| └─IndexJoin_61(Probe) | 10000.00 | root | | inner join, inner:TableReader_58, outer key:cmpddb.t1.a, inner key:cmpddb.t.a, equal cond:eq(cmpddb.t1.a, cmpddb.t.a) |
| ├─StreamAgg_49(Build) | 8000.00 | root | | group by:cmpddb.t1.a, funcs:firstrow(cmpddb.t1.a)->cmpddb.t1.a |
| │ └─TableReader_50 | 8000.00 | root | | data:StreamAgg_41 |
| │ └─StreamAgg_41 | 8000.00 | cop[tikv] | | group by:cmpddb.t1.a, |
| │ └─TableFullScan_48 | 10000.00 | cop[tikv] | table:t1 | keep order:true, stats:pseudo |
| └─TableReader_58(Probe) | 8000.00 | root | | data:TableRangeScan_57 |
| └─TableRangeScan_57 | 8000.00 | cop[tikv] | table:t | range: decided by [cmpddb.t1.a], keep order:false, stats:pseudo |
+----------------------------------+----------+-----------+---------------+-----------------------------------------------------------------------------------------------------------------------+
10 rows in set, 1 warning (0.01 sec)
mysql> show warnings;
+---------+------+--------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------------------------------------+
| Warning | 1815 | Optimizer Hint /*+ INL_JOIN(t) */ or /*+ TIDB_INLJ(t) */ is inapplicable |
+---------+------+--------------------------------------------------------------------------+
1 row in set (0.00 sec)
```
### 2. What did you expect to see? (Required)
The join order is independent of the table order in SQL statement.
### 3. What did you see instead (Required)
Need to rewrite SQL to match the optimizer, and the hints of leading and inl_join don't match their behavior.
### 4. What is your TiDB version? (Required)
v7.1.0
Contributor guide
Assessment
This issue has not been assessed yet.