update与select关联执行效率问题

UPDATE fl_user_space u
SET u.`course_count` = (SELECT COUNT(*) FROM fl_course c WHERE c.uid = u.uid)
WHERE 1 = 1 
AND u.school_id = #${schoolId}#;

  这种执行效率执行时间在秒级以上,几秒到几十秒,数据量大时甚至更长

 

UPDATE fl_user_space u,
(SELECT c.uid ,COUNT(course_id) AS course_count FROM fl_course c WHERE c.school_id = #${schoolId}# GROUP BY c.`uid`) b
SET u.`course_count` = b.course_count
WHERE u.`uid` = b.uid 
AND u.school_id = #${schoolId}#;

  这种执行效率执行时间在毫秒级,1~2百毫秒左右

posted @ 2018-11-13 18:49  水木神舟10  阅读(880)  评论(0编辑  收藏  举报