1. 添加索引:在product_distribution表的id和product_id字段上添加索引,可以提高查询效率。
  2. 避免使用函数:在对p.has_price_modified和pd.updated_date字段赋值时,使用now()函数会导致每次查询都要计算当前时间,可以考虑在程序中获取当前时间,然后直接赋值。
  3. 删除无用的注释:在where条件中注释掉的条件可以删除,避免影响查询效率。
  4. 使用EXPLAIN分析:使用EXPLAIN命令分析查询的执行计划,找出可能存在的性能问题。

优化后的SQL语句如下:

ALTER TABLE product_distribution ADD INDEX index_pd_id(id); ALTER TABLE product_distribution ADD INDEX index_pd_product_id(product_id);

UPDATE product_distribution pd JOIN product p ON p.id=pd.product_id SET p.has_price_modified = '2022-07-22 10:00:00', pd.updated = 597, pd.updated_date = '2022-07-22 10:00:00', pd.dispatch_time='', pd.dispatch_priority = 2 WHERE pd.id IN (437573);

EXPLAIN SELECT * FROM product_distribution WHERE id = 437573

update product_distribution pdjoin product p on pid=pdproduct_idset phas_price_modified = now pdupdated = 597 pdupdated_date = now pddispatch_time=pddispatch_priority = 2where -- pdstatus=1009002 and