You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### `tidb_opt_partial_ordered_index_for_topn` <span class="version-mark">New in v8.5.6 and v9.0.0</span>
4793
+
4794
+
- Scope: SESSION | GLOBAL
4795
+
- Persists to cluster: Yes
4796
+
- Applies to hint [SET_VAR](/optimizer-hints.md#set_varvar_namevar_value): Yes
4797
+
- Type: Enum
4798
+
- Default value: `DISABLE`
4799
+
- Possible values: `DISABLE`, `COST`
4800
+
- Controls whether the optimizer can leverage the partial order of an index to optimize TopN computation when a query contains `ORDER BY ... LIMIT`. When the sort column matches the index order (for example, the sort column is an index column or has a prefix index), the data returned by the index scan is already partially ordered on that column. In this case, the optimizer can incrementally build the TopN result during the scan and stop early once the `LIMIT` is satisfied, thereby reducing sorting overhead.
4801
+
- Usage scenarios: When the sort column in an `ORDER BY ... LIMIT` clause is a long string with only a prefix index, to reduce the TopN sorting overhead, you can set this variable to `COST`and specify a `USE INDEX`or`FORCE INDEX` hint in the query to enable the partial order TopN optimization.
4802
+
4803
+
- The default value is `DISABLE`, which means the partial order TopN optimization is disabled. In this case, the optimizer uses the standard global sorting approach for TopN.
4804
+
- To force the use of the partial order TopN optimization, set this variable to `COST`and specify a qualifying index in the query using `USE INDEX`or`FORCE INDEX`. If the specified index does not meet the prerequisites for this optimization (for example, the `ORDER BY` clause does not match the index prefix, or the query contains unsupported ordering patterns), the optimization might not be applied even when the variable is set to `COST`, and the execution plan falls back to the standard TopN approach.
4805
+
4806
+
>**Note:**
4807
+
>
4808
+
> Currently, the optimizer does not support dynamically deciding whether to apply the partial order TopN optimization based on the cost model. If you only set this variable to `COST` without specifying `USE INDEX`or`FORCE INDEX`, the optimizer might not apply this optimization. To ensure that the optimization is applied, use it together with `USE INDEX`or`FORCE INDEX`.
4809
+
4810
+
<details>
4811
+
<summary>View examples of partial order TopN optimization</summary>
4812
+
4813
+
Create a table `t_varchar`and define a prefix index `idx_name_prefix(name(10))`on the string column `name`:
4814
+
4815
+
```sql
4816
+
CREATE TABLE t_varchar (
4817
+
id INT PRIMARY KEY,
4818
+
name VARCHAR(255),
4819
+
INDEX idx_name_prefix(name(10))
4820
+
);
4821
+
```
4822
+
4823
+
- Force the partial order TopN optimization (`COST`+`USE INDEX`):
4824
+
4825
+
```sql
4826
+
> SET SESSION tidb_opt_partial_ordered_index_for_topn = 'COST';
0 commit comments