MySql Delete不走索引问题

时间:2022-07-03 23:37:02

如果delete语句带有查询,写法不对会导致不走索引。

简单粗暴的办法:拆两条sql,一条查询,一条delete

=======================

【不走索引的写法】

DELETE FROM goods_item_combo_group_item_history
WHERE group_id IN (SELECT id
FROM goods_item_combo_group_history
WHERE lease_code = 'schjyzzh'
AND item_code = 'YL180606110506793'
AND gmt_modified = '2018-06-07 23:18:51')

【走索引的写法】

DELETE goods_item_combo_group_item_history
FROM goods_item_combo_group_item_history, goods_item_combo_group_history
WHERE goods_item_combo_group_item_history.group_id = goods_item_combo_group_history.id
AND goods_item_combo_group_history.lease_code = #{leaseCode}
AND goods_item_combo_group_history.item_code = #{itemCode}
AND goods_item_combo_group_history.gmt_modified = #{gmtModified,jdbcType=TIMESTAMP}