突发系统某定时任务一直未执行成功,一开始用几百条数据做测试发现无反应,后续改为1条数据发现仍无反应。
问题sql:
select * from fnd_items_itf fii
where fii.ID in
(
select max(f.ID) from fnd_items_itf f
where FLAG_MESSAGE = #{flagMessage}
and FINISHED_FLAG = 'N'
group by f.INVENTORY_ITEM_ID, f.organization_id
)
1条数据执行上述sql,运行时间大概10分钟左右。
优化后sql:
SELECT
*
FROM
fnd_items_itf fii
WHERE
fii.ID IN (
SELECT
s.*
FROM
(
SELECT
max( f.ID )
FROM
fnd_items_itf f
WHERE
f.FLAG_MESSAGE = #{flagMessage}
AND f.FINISHED_FLAG = 'N'
GROUP BY
f.INVENTORY_ITEM_ID,
f.organization_id
) s
)
group by和max函数均对fii.id 关联下推有 误导,导致mysql内核执行慢。