why don't you simply update from a join over mail_message and mail_mail? The way you do it currently is very expensive I think and can really cause trouble for a lot of mails/messages