Skip to content

Commit f912e2e

Browse files
committed
Add index lookup push down content (#22196)
1 parent e9a4dd1 commit f912e2e

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

optimizer-hints.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,60 @@ EXPLAIN SELECT /*+ NO_ORDER_INDEX(t, a) */ a FROM t ORDER BY a LIMIT 10;
474474

475475
The same as the example of `ORDER_INDEX` hint, the optimizer generates two types of plans for this query: `Limit + IndexScan(keep order: true)` and `TopN + IndexScan(keep order: false)`. When the `NO_ORDER_INDEX` hint is used, the optimizer will choose the latter plan to read the index out of order.
476476

477+
### INDEX_LOOKUP_PUSHDOWN(t1_name, idx1_name [, idx2_name ...]) <span class="version-mark">New in v8.5.5 and v9.0.0</span>
478+
479+
The `INDEX_LOOKUP_PUSHDOWN(t1_name, idx1_name [, idx2_name ...])` hint instructs the optimizer to access the specified table using only the specified indexes and push down the `IndexLookUp` operator to TiKV for execution.
480+
481+
The following example shows the execution plan generated when using this hint:
482+
483+
```sql
484+
CREATE TABLE t1(a INT, b INT, KEY(a));
485+
EXPLAIN SELECT /*+ INDEX_LOOKUP_PUSHDOWN(t1, a) */ a, b FROM t1;
486+
```
487+
488+
```sql
489+
+-----------------------------+----------+-----------+----------------------+--------------------------------+
490+
| id | estRows | task | access object | operator info |
491+
+-----------------------------+----------+-----------+----------------------+--------------------------------+
492+
| IndexLookUp_7 | 10000.00 | root | | |
493+
| ├─LocalIndexLookUp(Build) | 10000.00 | cop[tikv] | | index handle offsets:[1] |
494+
| │ ├─IndexFullScan_5(Build) | 10000.00 | cop[tikv] | table:t1, index:a(a) | keep order:false, stats:pseudo |
495+
| │ └─TableRowIDScan_8(Probe) | 10000.00 | cop[tikv] | table:t1 | keep order:false, stats:pseudo |
496+
| └─TableRowIDScan_6(Probe) | 0.00 | cop[tikv] | table:t1 | keep order:false, stats:pseudo |
497+
+-----------------------------+----------+-----------+----------------------+--------------------------------+
498+
```
499+
500+
When you use the `INDEX_LOOKUP_PUSHDOWN` hint, the outermost Build operator on the TiDB side in the original execution plan is replaced with `LocalIndexLookUp` and pushed down to TiKV for execution. While scanning the index, TiKV attempts to perform a table lookup locally to read the corresponding row data. Because the index and row data might be distributed across different Regions, requests pushed down to TiKV might not cover all target rows. As a result, the execution plan still retains the `TableRowIDScan` operator on the TiDB side to fetch rows that are not hit on the TiKV side.
501+
502+
The `INDEX_LOOKUP_PUSHDOWN` hint currently has the following limitations:
503+
504+
- Cached tables and temporary tables are not supported.
505+
- Queries using [global indexes](/global-indexes.md) are not supported.
506+
- Queries using [multi-valued indexes](/choose-index.md#use-multi-valued-indexes) are not supported.
507+
- Isolation levels other than `REPEATABLE-READ` are not supported.
508+
- [Follower Read](/follower-read.md) is not supported.
509+
- [Stale Read](/stale-read.md) and [reading historical data using `tidb_snapshot`](/read-historical-data.md) are not supported.
510+
- The pushed-down `LocalIndexLookUp` operator does not support `keep order`. If the execution plan includes an `ORDER BY` based on index columns, the query falls back to a regular `IndexLookUp`.
511+
- The pushed-down `LocalIndexLookUp` operator does not support sending Coprocessor requests in paging mode.
512+
- The pushed-down `LocalIndexLookUp` operator does not support [Coprocessor Cache](/coprocessor-cache.md).
513+
514+
### NO_INDEX_LOOKUP_PUSHDOWN(t1_name) <span class="version-mark">New in v8.5.5 and v9.0.0</span>
515+
516+
The `NO_INDEX_LOOKUP_PUSHDOWN(t1_name)` hint explicitly disables the `IndexLookUp` pushdown for a specified table. This hint is typically used with the [`tidb_index_lookup_pushdown_policy`](/system-variables.md#tidb_index_lookup_pushdown_policy-new-in-v855-and-v900) system variable. When the value of this variable is `force` or `affinity-force`, you can use this hint to prevent `IndexLookUp` pushdown for specific tables.
517+
518+
The following example sets the `tidb_index_lookup_pushdown_policy` variable to `force`, which automatically enables pushdown for all `IndexLookUp` operators in the current session. If you specify the `NO_INDEX_LOOKUP_PUSHDOWN` hint in a query, `IndexLookUp` is not pushed down for the corresponding table:
519+
520+
```sql
521+
SET @@tidb_index_lookup_pushdown_policy = 'force';
522+
523+
-- The IndexLookUp operator will not be pushed down.
524+
SELECT /*+ NO_INDEX_LOOKUP_PUSHDOWN(t) */ * FROM t WHERE a > 1;
525+
```
526+
527+
> **Note:**
528+
>
529+
> `NO_INDEX_LOOKUP_PUSHDOWN` takes precedence over [`INDEX_LOOKUP_PUSHDOWN`](#index_lookup_pushdownt1_name-idx1_name--idx2_name--new-in-v855-and-v900). When you specify both hints in the same query, `NO_INDEX_LOOKUP_PUSHDOWN` takes effect.
530+
477531
### AGG_TO_COP()
478532

479533
The `AGG_TO_COP()` hint tells the optimizer to push down the aggregate operation in the specified query block to the coprocessor. If the optimizer does not push down some aggregate function that is suitable for pushdown, then it is recommended to use this hint. For example:

system-variables.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3533,6 +3533,19 @@ For a system upgraded to v5.0 from an earlier version, if you have not modified
35333533
- This variable is used to set the concurrency of the `index lookup join` algorithm.
35343534
- A value of `-1` means that the value of `tidb_executor_concurrency` will be used instead.
35353535

3536+
### tidb_index_lookup_pushdown_policy <span class="version-mark">New in v8.5.5 and v9.0.0</span>
3537+
3538+
- Scope: SESSION | GLOBAL
3539+
- Persists to cluster: Yes
3540+
- Applies to hint [SET_VAR](/optimizer-hints.md#set_varvar_namevar_value): Yes
3541+
- Type: Enumeration
3542+
- Default value: `hint-only`
3543+
- Value options: `hint-only`, `affinity-force`, `force`
3544+
- This variable controls whether and when TiDB pushes the `IndexLookUp` operator down to TiKV. The value options are as follows:
3545+
- `hint-only` (default): TiDB pushes the `IndexLookUp` operator down to TiKV only when the [`INDEX_LOOKUP_PUSHDOWN`](/optimizer-hints.md#index_lookup_pushdownt1_name-idx1_name--idx2_name--new-in-v855-and-v900) hint is explicitly specified in the SQL statement.
3546+
- `affinity-force`: TiDB automatically enables pushdown only for tables that are configured with the `AFFINITY` option.
3547+
- `force`: TiDB enables `IndexLookUp` pushdown for all tables.
3548+
35363549
### tidb_index_merge_intersection_concurrency <span class="version-mark">New in v6.5.0</span>
35373550

35383551
- Scope: SESSION | GLOBAL

0 commit comments

Comments
 (0)