pingcap / pingcap/tidb

The delete stmt could not be column pruning and choose the table full scan

Open
#48,709 7 comments 0 reactions 0 assignees View on GitHub
sig/planner type/enhancement
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

We can not do column pruning in delete stmt.
Although the where column has index, we cannot choose index directly rather than full scan.

### 1. Minimal reproduce step (Required)

1. create table with multi column
```
CREATE TABLE `dh_message_logs` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`messageid` int(11) NOT NULL ,
`username` bigint(20) DEFAULT NULL,
`createtime` int(11) DEFAULT NULL ,
`status` int(1) DEFAULT NULL,
PRIMARY KEY (`id`) /*T![clustered_index] NONCLUSTERED */,
KEY `idx_message_logs_messageid_username` (`messageid`,`username`),
KEY `idx_message_logs_username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
```

2. delete stmt with high selectivity

```
mysql> explain delete from dh_message_logs where messageid=1830030;
+---------------------------+------------+-----------+-----------------------+---------------------------------------------------+
| id | estRows | task | access object | operator info |
+---------------------------+------------+-----------+-----------------------+---------------------------------------------------+
| Delete_4 | N/A | root | | N/A |
| └─TableReader_8 | 26618.55 | root | | data:Selection_7 |
| └─Selection_7 | 26618.55 | cop[tikv] | | eq(dh_app_398.dh_message_logs.messageid, 1830030) |
| └─TableFullScan_6 | 1308288.00 | cop[tikv] | table:dh_message_logs | keep order:false |
+---------------------------+------------+-----------+-----------------------+---------------------------------------------------+

```

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

Use index scan instead of full scan

```
mysql> explain delete from dh_message_logs where messageid=1830030;
+----------------------------------+---------+-----------+---------------------------------------------------------------------------------------+-------------------------------------------+
| id | estRows | task | access object | operator info |
+----------------------------------+---------+-----------+---------------------------------------------------------------------------------------+-------------------------------------------+
| Delete_4 | N/A | root | | N/A |
| └─IndexRangeScan | 70.20 | root | | |
+----------------------------------+---------+-----------+---------------------------------------------------------------------------------------+-------------------------------------------+
```

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

Use full scan because of double read cost more than full scan cost
```
mysql> explain delete from dh_message_logs where messageid=1830030;
+---------------------------+------------+-----------+-----------------------+---------------------------------------------------+
| id | estRows | task | access object | operator info |
+---------------------------+------------+-----------+-----------------------+---------------------------------------------------+
| Delete_4 | N/A | root | | N/A |
| └─TableReader_8 | 26618.55 | root | | data:Selection_7 |
| └─Selection_7 | 26618.55 | cop[tikv] | | eq(dh_app_398.dh_message_logs.messageid, 1830030) |
| └─TableFullScan_6 | 1308288.00 | cop[tikv] | table:dh_message_logs | keep order:false |
+---------------------------+------------+-----------+-----------------------+---------------------------------------------------+
```

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

v6.5

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.