原语句
sql
UPDATE tbl_mix_trace
SET mix_plan_id = ( SELECT p.id FROM tbl_mix_plan p WHERE p.lot_id_out= lot_id_in ),
pro_date_in = (SELECT p.pro_date_out FROM tbl_mix_plan p WHERE p.lot_id_out = lot_id_in),
shift_id_in = (SELECT p.shift_id_out FROM tbl_mix_plan p WHERE p.lot_id_out = lot_id_in),
equip_id_in = (SELECT p.equip_id_out FROM tbl_mix_plan p WHERE p.lot_id_out = lot_id_in)
where <![CDATA[
( lot_id_in <> '' and lot_id_in is not NULL)
]]>
and pro_date_out = #{proDateOut}
and shift_id_out = #{shiftIdOut}
原因
- 使用了过多的
子查询
,查询的还是同一个表,效率低- 使用
join
可以避免这个问题(不同的数据库写法可能不同)
更改后的语句
sql
UPDATE t
SET
t.mix_plan_id = p.id,
t.pro_date_in = p.pro_date_out,
t.shift_id_in = p.shift_id_out,
t.equip_id_in = p.equip_id_out
FROM
tbl_mix_trace t
JOIN
tbl_mix_plan p ON p.lot_id_out = t.lot_id_in
WHERE
t.lot_id_in <> '' AND t.lot_id_in IS NOT NULL
AND t.pro_date_out = '2024-08-14'
AND t.shift_id_out = 1;