简体   繁体   中英

faster way to write INNER JOIN function

MySQL: I'm trying to add some data to my table from another table, where their id is the same.

update sf_leads t1
inner join temp_geninfo t2 
on t1.application_id = t2.application_id
set t1.org_city = t2.org_city;

However, each table has 92,000 values, and this query has been taking nearly an hour to finish. Is there a faster way to do this, or do I just need to wait?

For this query:

update sf_leads t1 inner join
       temp_geninfo t2 
       on t1.application_id = t2.application_id
    set t1.org_city = t2.org_city;

You want an index on temp_geninfo(application_id, org_city) :

create index idx_temp_geninfo_application_id_org_city on temp_geninfo(application_id, org_city);

You also might want to check that you don't have multiple updates on the same row, by running:

select application_id, count(*)
from temp_geninfo
group by application_id
having count(*) > 1;

If this returns any rows, then you potentially have the problem of the join multiplying the number of rows being updated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM