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 --
) 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 --
left join dddddddddddddddddddddd proj --
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 --
on t1.md5 = t2.md5
;