Open ghost opened 4 years ago
The default execution plan I think is the same with unfolding the subquery and then execute the whole query in this case because of the IndexJoin:
mysql> EXPLAIN SELECT * FROM t1 WHERE pk IN (SELECT pk FROM t1 WHERE ip IN ('127.0.0.1'));
+------------------------------------+---------+-----------+-----------------------------+------------------------------------------------------------------------------+
| id | estRows | task | access object | operator info |
+------------------------------------+---------+-----------+-----------------------------+------------------------------------------------------------------------------+
| IndexJoin_16 | 0.00 | root | | inner join, inner:IndexLookUp_15, outer key:test.t1.pk, inner key:test.t1.pk |
| ├─IndexLookUp_55(Build) | 0.00 | root | | |
| │ ├─IndexRangeScan_53(Build) | 0.00 | cop[tikv] | table:t1, index:ip(ip) | range:["127.0.0.1","127.0.0.1"], keep order:false |
| │ └─TableRowIDScan_54(Probe) | 0.00 | cop[tikv] | table:t1 | keep order:false |
| └─IndexLookUp_15(Probe) | 1.00 | root | | |
| ├─IndexRangeScan_13(Build) | 1.00 | cop[tikv] | table:t1, index:PRIMARY(pk) | range: decided by [eq(test.t1.pk, test.t1.pk)], keep order:false |
| └─TableRowIDScan_14(Probe) | 1.00 | cop[tikv] | table:t1 | keep order:false |
+------------------------------------+---------+-----------+-----------------------------+------------------------------------------------------------------------------+
7 rows in set (0.00 sec)
In the above execution plan, the build side of IndexJoin is an IndexRangeScan, the probe side of the IndexJoin is another IndexRangeScan, the range is calculated based on the result of the build side IndexRangeScan.
There might be a change that the root operator is not IndexJoin_16
. For example, it might be a Hash Join depends on the selectivity of both join sides and the selectivity of join operator.
@winoros @eurekaka PTAL
This is similar to https://github.com/pingcap/tidb/issues/25540
Feature Request
Is your feature request related to a problem? Please describe:
This is a fork of https://github.com/pingcap/tidb/issues/5736 - with the original issue resolved, this is a feature request for an optimization for simple subqueries on the same table as the base table.
Describe the feature you'd like:
Consider the following two queries in the example. They could potentially use the same plan:
For context, this is not currently an optimization supported by MySQL:
Describe alternatives you've considered:
N/A
Teachability, Documentation, Adoption, Migration Strategy:
Should be a transparent optimization.