我有一个非常包含LEFT JOIN
子查询的查询。完全加载需要20分钟。
这是我的问题:
UPDATE orders AS o
LEFT JOIN (
SELECT obe_order_master_id, COUNT(id) AS count_files, id, added
FROM customer_instalments
GROUP BY obe_order_master_id
) AS oci ON oci.obe_order_master_id = SUBSTRING(o.order_id, 4)
SET o.final_customer_file_id = oci.id,
o.client_work_delivered = oci.added
WHERE oci.count_files = 1
有什么方法可以让这个查询运行得更快?
答案 0 :(得分:1)
在Temp Table中移动Where
条件并将WHERE
替换为HAVING
子句,这将从临时表中删除不必要的行,因此减少过滤并可能有助于提高性能
UPDATE orders AS o
LEFT JOIN (
SELECT obe_order_master_id, id, added
FROM customer_instalments
GROUP BY obe_order_master_id
HAVING COUNT(id) = 1
) AS oci ON oci.obe_order_master_id = SUBSTRING(o.order_id, 4)
SET o.final_customer_file_id = oci.id,
o.client_work_delivered = oci.added
答案 1 :(得分:0)
我建议为Order_id substring创建单独的列并在其上创建索引。然后在WHERE中使用此列。