pingcap / pingcap/tidb

TopN + RangeScan has a better plan than Limit + IndexFullScan

Open
#60,944 3 comments 0 reactions 1 assignee Claimed by @qw4990 View on GitHub
severity/moderate sig/planner type/bug
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)

if you need the replayer, PLZ ping me in email or feishu/lark.
After testing, we can make sure idx_parentId_sitecode is better performance.
And, I see there's similar issue : https://github.com/pingcap/tidb/issues/39187
```
mysql> PLAN REPLAYER LOAD 'replayer_b-IxPsisIP6mpJsjNfsEOg==_1745987435719884935.zip';
Query OK, 0 rows affected (1.28 sec)
```

### 2. What did you expect to see? (Required)

```
mysql> explain format=verbose SELECT a.site_code, a.useridx FROM dh_account_basic a force index(idx_parentId_sitecode) WHERE a.account_type in (2, 5, 9, 10, 11) AND a.site_code = '006' AND a.parent_id in (393571418) GROUP BY a.site_code, a.useridx ORDER BY a.register_time desc, a.username desc LIMIT 100;
+------------------------------------+-----------+--------------+-----------+------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------+
| id | estRows | estCost | task | access object | operator info |
+------------------------------------+-----------+--------------+-----------+------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------+
| Projection_10 | 100.00 | 276207512.48 | root | | dh_app_006.dh_account_basic.site_code, dh_app_006.dh_account_basic.useridx |
| └─TopN_12 | 100.00 | 276207492.52 | root | | dh_app_006.dh_account_basic.register_time:desc, dh_app_006.dh_account_basic.username:desc, offset:0, count:100 |
| └─IndexLookUp_20 | 100.00 | 276171819.68 | root | | |
| ├─IndexRangeScan_16(Build) | 140525.00 | 30840645.02 | cop[tikv] | table:a, index:idx_parentId_sitecode(parent_id, site_code) | range:[393571418 "006",393571418 "006"], keep order:false |
| └─TopN_19(Probe) | 100.00 | 111543239.70 | cop[tikv] | | dh_app_006.dh_account_basic.register_time:desc, dh_app_006.dh_account_basic.username:desc, offset:0, count:100 |
| └─Selection_18 | 140119.46 | 65087135.06 | cop[tikv] | | in(dh_app_006.dh_account_basic.account_type, 2, 5, 9, 10, 11) |
| └─TableRowIDScan_17 | 140525.00 | 58074937.56 | cop[tikv] | table:a | keep order:false |
+------------------------------------+-----------+--------------+-----------+------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------+
7 rows in set (0.00 sec)
```

### 3. What did you see instead (Required)

```
mysql> explain format=verbose SELECT a.site_code, a.useridx FROM dh_account_basic a WHERE a.account_type in (2, 5, 9, 10, 11) AND a.site_code = '006' AND a.parent_id in (393571418) GROUP BY a.site_code, a.useridx ORDER BY a.register_time desc, a.username desc LIMIT 100;
+----------------------------------+----------+-------------+-----------+----------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id | estRows | estCost | task | access object | operator info |
+----------------------------------+----------+-------------+-----------+----------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Projection_10 | 100.00 | 90207197.03 | root | | dh_app_006.dh_account_basic.site_code, dh_app_006.dh_account_basic.useridx |
| └─Limit_15 | 100.00 | 90207177.07 | root | | offset:0, count:100 |
| └─Projection_51 | 100.00 | 90207177.07 | root | | dh_app_006.dh_account_basic.site_code, dh_app_006.dh_account_basic.register_time, dh_app_006.dh_account_basic.username, dh_app_006.dh_account_basic.useridx, dh_app_006.dh_account_basic.account_type, dh_app_006.dh_account_basic.parent_id |
| └─IndexLookUp_50 | 100.00 | 90207117.19 | root | | |
| ├─Selection_48(Build) | 75835.21 | 33190454.97 | cop[tikv] | | in(dh_app_006.dh_account_basic.account_type, 2, 5, 9, 10, 11) |
| │ └─IndexFullScan_46 | 76054.69 | 29395325.94 | cop[tikv] | table:a, index:idx_acco......gistertime_username_accounttype_useridx(register_time, username, account_type, useridx) | keep order:true, desc |
| └─Selection_49(Probe) | 100.00 | 38908861.24 | cop[tikv] | | eq(dh_app_006.dh_account_basic.parent_id, 393571418), eq(dh_app_006.dh_account_basic.site_code, "006") |
| └─TableRowIDScan_47 | 75835.21 | 31340507.70 | cop[tikv] | table:a | keep order:false |
+----------------------------------+----------+-------------+-----------+----------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
8 rows in set, 4 warnings (0.00 sec)

mysql> show warnings;
+-------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note | 1105 | [a,a(tiflash),idx_dh_account......register_time,idx_account_type_register_time_useridx_username,idx_dh_account_type_register_device_id_register_time,idx_account_basic_accounttype,idx_parentId_sitecode,idx_tmp] remain after pruning paths for a given Prop{SortItems: [], TaskTp: copSingleReadTask} |
| Note | 1105 | [a,a(tiflash),idx_dh_account_......_ip_register_time,idx_account_type_register_time_useridx_username,idx_dh_account_type_register_device_id_register_time,idx_account_basic_accounttype,idx_parentId_sitecode,idx_tmp] remain after pruning paths for a given Prop{SortItems: [], TaskTp: copMultiReadTask} |
| Note | 1105 | [a(tiflash)] remain after pruning paths for a given Prop{SortItems: [], TaskTp: mppTask} |
| Note | 1105 | [a,a(tiflash),idx_acco......gistertime_username_accounttype_useridx,idx_dh_account_type_register_ip_register_time,idx_account_type_register_time_useridx_username,idx_dh_account_type_register_device_id_register_time,idx_account_basic_accounttype,idx_parentId_sitecode,idx_tmp] remain after pruning paths for a given Prop{SortItems: [{dh_app_006.dh_account_basic.register_time desc} {dh_app_006.dh_account_basic.username desc}], TaskTp: copMultiReadTask} |
+-------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
4 rows in set (0.00 sec)
```

### 4. What is your TiDB version? (Required)

mysql> select version();
+--------------------+
| version() |
+--------------------+
| 8.0.11-TiDB-v7.5.6 |
+--------------------+

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.