MySQL 忽略删除查询的强制索引,但不是等效的 Select 查询

MySQL ignoring Force Index for Delete Query, But Not For Equivalent Select Query

我在这里非常困惑和沮丧-

我有一个巨大的 table (table1)。

我正在尝试基于另一个 table 执行删除查询。查询优化器没有使用适当的索引,所以我试图强制它

delete table1 
FROM table1
FORCE INDEX FOR JOIN (index1)  
inner JOIN table2  
where
table1.field1=table2.field1
and
table1.field2=table2.field2
and
date(table1.startdatetime)=table2.reportdate;

等效的 select 如下所示:

SELECT * 
FROM table1
FORCE INDEX FOR JOIN (index1)  
inner JOIN table2  
where
table1.field1=table2.field1
and
table1.field2=table2.field2
and
date(table1.startdatetime)=table2.reportdate;

我不明白的是删除查询不使用索引,但是select查询使用

select 中的 "explain" 是:

1   SIMPLE  table2 index    idx1    idx1    55      1456    Using where; Using index
1   SIMPLE  table1 ref  index1 index1 51    table2.field1,table2.field2 508 **Using index condition**

但删除的解释如下:

# id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
1, SIMPLE, table2, index, idx1, idx1, 55, , 1456, Using where; Using index
1, SIMPLE, table1, ref, index1, index1, 51, table2.field1,table2.field2, 508, Using where

为什么select语句有Using index condition而等价的delete有using where?

如何强制删除也使用索引?

谢谢!

Index hints apply only to SELECT statements. (They are accepted by the parser for UPDATE statements but are ignored and have no effect.)

来自 the official docs

即使没有特别指出,我假设 DELETE 属于 UPDATE 的处理方式,而不是 SELECT。

效率低下可能是

date(table1.startdatetime)=table2.reportdate

无法使用 startdatetime 上的索引,因为它隐藏在函数调用中。通常更好的做法是:

    table1.startdatetime >= table2.reportdate
AND table1.startdatetime  < table2.reportdate + INTERVAL 1 DAY