mysql如果通過超長的字段進行on關聯,會導致效率很低,7k關聯1.4k,結果為30+W的數據量,執行時間高達50秒。
將這個字段進行md5,然后再通過md5后的值進行關聯,執行效率會大大優化,同樣的SQL,可在5秒內完成。
select count(1) as cnt from ( select md5(activity.target_building_ids) as md5 from aaaaaaaaaaaaaaaaaaaaa enroll -- 5472 ) t1 left join ( select md5(t21.target_building_ids) as md5 ,t22.name from (select distinct bbbbbbbbbbbbbbbbbbbbb from h_activity where ifnull(target_building_ids,'') <> '') t21 join ( select bld.id ,proj.name from ccccccccccccccccccc bld -- 6277 left join dddddddddddddddddddddd proj -- 575 on proj.id =bld.proj_id and proj.is_deleted=0 where bld.is_deleted =0 ) t22 on instr(t21.target_building_ids,t22.id)>0 ) t2 -- 1305 on t1.md5 = t2.md5 ;
