-- Calculamos todos los volumenes de todos los tickets una sola vez CREATE OR REPLACE TEMPORARY TABLE tmp.tTicketVolume (PRIMARY KEY (id)) ENGINE = MEMORY SELECT t.id, SUM(s.quantity * ic.cm3delivery / 1000000) volume FROM vn.sale s JOIN vn.ticket t ON t.id = s.ticketFk JOIN vn.itemCost ic ON ic.itemFk = s.itemFk AND ic.warehouseFk = t.warehouseFk GROUP BY t.id; UPDATE vn.ticket t JOIN tmp.tTicketVolume tv ON tv.id = t.id SET t.volume = tv.volume; DROP TEMPORARY TABLE tmp.tTicketVolume;