TiDB optimizer doesn't provide the clustered index information in the query plan
- 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)
a) Table DDL for CLUSTERED Primary Key:
CREATE TABLE `table1` (
`id` bigint(20) NOT NULL,
`src` int(11) NOT NULL,
`src_id` varchar(254) NOT NULL,
`uuid` varchar(250) NOT NULL,
`f_name` varchar(250) DEFAULT NULL,
`l_name` varchar(250) DEFAULT NULL,
PRIMARY KEY (`id`,`src`,`uuid`,`src_id`) /*T![clustered_index] CLUSTERED */,
KEY `idx_all` (`id`,`uuid`,`src_id`),
KEY `idx_temp` (`id`,`src`,`src_id`,`uuid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
b) Table DDL for NONCLUSTERED Primary Key:
CREATE TABLE `table2` (
`id` bigint(20) NOT NULL,
`src` int(11) NOT NULL,
`src_id` varchar(254) NOT NULL,
`uuid` varchar(250) NOT NULL,
`f_name` varchar(250) DEFAULT NULL,
`l_name` varchar(250) DEFAULT NULL,
PRIMARY KEY (`id`,`src`,`uuid`,`src_id`) NONCLUSTERED,
KEY `idx_all` (`id`,`uuid`,`src_id`),
KEY `idx_temp` (`id`,`src`,`src_id`,`uuid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
In my test environment, both these tables have the same data. The initial data in table1 is populated using the query:
insert into table1 values (FLOOR(RAND() * 400) + 700000,FLOOR(RAND() * 400) + 700000,'asasasasasas23232423314$%#$#$#$%#$#$##$asasa','asasasasasas23232423314$%#$#$#$%#$#$##$asasa','asasasasasas23232423314$%#$#$#$%#$#$##$asasa','asasasasasas23232423314$%#$#$#$%#$#$##$asasa');
The data in the table: table2 is copied from the table1 using:
insert into table2 select * from table1;
### 2. What did you expect to see? (Required)
While checking the query plan on the tables for the following query:
a) explain analyze SELECT `src`,`src_id`,`f_name`,`l_name` FROM `table1` WHERE ((`id`=175) AND (`src`=100)) ORDER BY `uuid` ASC,`src_id` ASC LIMIT 50 ;
+-------------------------------+---------+---------+-----------+-----------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------+-----------+------+
| id | estRows | actRows | task | access object | execution info | operator info | memory | disk |
+-------------------------------+---------+---------+-----------+-----------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------+-----------+------+
| Projection_8 | 3.67 | 1 | root | | time:724.1µs, loops:2, RU:0.288683, Concurrency:OFF | test.table1.src, test.table1.src_id, test.table1.f_name, test.table1.l_name | 3.23 KB | N/A |
| └─Limit_12 | 3.67 | 1 | root | | time:717.2µs, loops:2 | offset:0, count:50 | N/A | N/A |
| └─TableReader_19 | 3.67 | 1 | root | | time:714.4µs, loops:2, cop_task: {num: 1, max: 616.2µs, proc_keys: 1, tot_proc: 104.1µs, tot_wait: 46.6µs, rpc_num: 1, rpc_time: 585.6µs, copr_cache_hit_ratio: 0.00, build_task_duration: 11.9µs, max_distsql_concurrency: 1} | data:Limit_18 | 559 Bytes | N/A |
| └─Limit_18 | 3.67 | 1 | cop[tikv] | | tikv_task:{time:0s, loops:1}, scan_detail: {total_process_keys: 1, total_process_keys_size: 262, total_keys: 2, get_snapshot_time: 21.4µs, rocksdb: {key_skipped_count: 1, block: {cache_hit_count: 4}}} | offset:0, count:50 | N/A | N/A |
| └─TableRangeScan_17 | 3.67 | 1 | cop[tikv] | table:table1 | tikv_task:{time:0s, loops:1} | range:[175 100,175 100], keep order:true | N/A | N/A |
+-------------------------------+---------+---------+-----------+-----------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------+-----------+------+
b) explain analyze SELECT `src`,`src_id`,`f_name`,`l_name` FROM `table2` WHERE ((`id`=175) AND (`src`=100)) ORDER BY `uuid` ASC,`src_id` ASC LIMIT 50 ;
+------------------------------------+---------+---------+-----------+-------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+------+
| id | estRows | actRows | task | access object | execution info | operator info | memory | disk |
+------------------------------------+---------+---------+-----------+-------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+------+
| Projection_8 | 7.33 | 1 | root | | time:2.83ms, loops:2, RU:0.826477, Concurrency:OFF | test.table2.src, test.table2.src_id, test.table2.f_name, test.table2.l_name | 20.3 KB | N/A |
| └─Projection_28 | 7.33 | 1 | root | | time:2.82ms, loops:2, Concurrency:OFF | test.table2.id, test.table2.src, test.table2.src_id, test.table2.uuid, test.table2.f_name, test.table2.l_name | 4.22 KB | N/A |
| └─IndexLookUp_27 | 7.33 | 1 | root | | time:2.82ms, loops:2, index_task: {total_time: 1.42ms, fetch_handle: 1.41ms, build: 1.88µs, wait: 4.69µs}, table_task: {total_time: 1.32ms, num: 1, concurrency: 5}, next: {wait_index: 1.49ms, wait_table_lookup_build: 50.2µs, wait_table_lookup_resp: 1.26ms} | limit embedded(offset:0, count:50) | 21.2 KB | N/A |
| ├─Limit_26(Build) | 7.33 | 1 | cop[tikv] | | time:1.39ms, loops:3, cop_task: {num: 1, max: 1.34ms, proc_keys: 1, tot_proc: 144.1µs, tot_wait: 373.1µs, rpc_num: 1, rpc_time: 1.33ms, copr_cache_hit_ratio: 0.00, build_task_duration: 15.5µs, max_distsql_concurrency: 1}, tikv_task:{time:0s, loops:1}, scan_detail: {total_process_keys: 1, total_process_keys_size: 179, total_keys: 2, get_snapshot_time: 350.2µs, rocksdb: {key_skipped_count: 1, block: {cache_hit_count: 6, read_count: 1, read_byte: 31.9 KB, read_time: 19.1µs}}} | offset:0, count:50 | N/A | N/A |
| │ └─IndexRangeScan_24 | 7.33 | 1 | cop[tikv] | table:table2, index:PRIMARY(id, src, uuid, src_id) | tikv_task:{time:0s, loops:1} | range:[175 100,175 100], keep order:true | N/A | N/A |
| └─TableRowIDScan_25(Probe) | 7.33 | 1 | cop[tikv] | table:table2 | time:1.24ms, loops:2, cop_task: {num: 1, max: 1.2ms, proc_keys: 1, tot_proc: 816.6µs, tot_wait: 31.2µs, rpc_num: 1, rpc_time: 1.18ms, copr_cache_hit_ratio: 0.00, build_task_duration: 24.5µs, max_distsql_concurrency: 1, max_extra_concurrency: 1}, tikv_task:{time:10ms, loops:1}, scan_detail: {total_process_keys: 1, total_process_keys_size: 230, total_keys: 1, get_snapshot_time: 13.7µs, rocksdb: {block: {cache_hit_count: 4}}} | keep order:false | N/A | N/A |
+------------------------------------+---------+---------+-----------+-------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------+------+
c) > explain analyze SELECT /*+ IGNORE_INDEX(table2,PRIMARY,idx_temp,idx_all)*/ `src`,`src_id`,`f_name`,`l_name` FROM `table2` WHERE ((`id`=175) AND (`src`=100)) ORDER BY `uuid` ASC,`src_id` ASC LIMIT 50 ;
+--------------------------------+----------+---------+-----------+-----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------+-----------+------+
| id | estRows | actRows | task | access object | execution info | operator info | memory | disk |
+--------------------------------+----------+---------+-----------+-----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------+-----------+------+
| Projection_8 | 3.17 | 1 | root | | time:34.6ms, loops:2, RU:217.618192, Concurrency:OFF | test.table2.src, test.table2.src_id, test.table2.f_name, test.table2.l_name | 3.23 KB | N/A |
| └─TopN_9 | 3.17 | 1 | root | | time:34.6ms, loops:2 | test.table2.uuid, test.table2.src_id, offset:0, count:50 | 34.5 KB | N/A |
| └─TableReader_17 | 3.17 | 1 | root | | time:34.5ms, loops:3, cop_task: {num: 2, max: 34.4ms, min: 27.2ms, avg: 30.8ms, p95: 34.4ms, max_proc_keys: 35580, p95_proc_keys: 35580, tot_proc: 60.3ms, tot_wait: 98.2µs, rpc_num: 2, rpc_time: 61.6ms, copr_cache_hit_ratio: 0.00, build_task_duration: 15.1µs, max_distsql_concurrency: 2} | data:TopN_16 | 643 Bytes | N/A |
| └─TopN_16 | 3.17 | 1 | cop[tikv] | | tikv_task:{proc max:40ms, min:30ms, avg: 35ms, p80:40ms, p95:40ms, iters:58, tasks:2}, scan_detail: {total_process_keys: 58659, total_process_keys_size: 12910812, total_keys: 58661, get_snapshot_time: 36.8µs, rocksdb: {key_skipped_count: 58659, block: {cache_hit_count: 10}}} | test.table2.uuid, test.table2.src_id, offset:0, count:50 | N/A | N/A |
| └─Selection_15 | 3.17 | 1 | cop[tikv] | | tikv_task:{proc max:40ms, min:30ms, avg: 35ms, p80:40ms, p95:40ms, iters:58, tasks:2} | eq(test.table2.src, 100), eq(test.table2.id, 175) | N/A | N/A |
| └─TableFullScan_14 | 58659.00 | 58659 | cop[tikv] | table:table2 | tikv_task:{proc max:40ms, min:30ms, avg: 35ms, p80:40ms, p95:40ms, iters:58, tasks:2} | keep order:false | N/A | N/A |
+--------------------------------+----------+---------+-----------+-----------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------------------+-----------+------+
### 3. What did you see instead (Required)
The table making use of CLUSTERED primary key doesn't show the index information in the EXPLAIN analyze, although it is making use of the index.
If the query was scanning the whole table it would have resulted in the TableFullScan_14 shown in "C".
### 4. What is your TiDB version? (Required)
SELECT tidb_version() ;
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version() |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v7.1.1
Edition: Community
Git Commit Hash: cf441574864be63938524e7dfcf7cc659edc3dd8
Git Branch: heads/refs/tags/v7.1.1
UTC Build Time: 2023-07-19 10:20:53
GoVersion: go1.20.6
Race Enabled: false
TiKV Min Version: 6.2.0-alpha
Check Table Before Drop: false
Store: tikv |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Contributor guide
Assessment
This issue has not been assessed yet.