update temp_parent_rating_emails pre
set reminders_count = (select count(1) from vendor_rating_email vre where vre.parent_email_id = pre.email_id);
上面的查询在我的小型 mysql 数据库上运行良好,但在我们较大的临时数据库上挂起。有人知道为什么或如何更好地优化它吗?
最佳答案
不要使用子查询,而是尝试使用内联 View (如下所示),以便仅扫描 vendor_ rating_email
表一次。
update temp_parent_rating_emails pre
JOIN
(
select
parent_email_id,
count(*) cnt_email
from vendor_rating_email
group by parent_email_id
) vre ON vre.parent_email_id = pre.email_id
SET pre.reminders_count = vre.cnt_email;
此外,还可以考虑在 temp_parent_ rating_emails (parent_email_id)
和 vendor_ rating_email (email_id)
上创建索引。
引用:
关于mysql - 简单的 mysql 更新语句卡在大型数据库上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25085872/