简体   繁体   中英

MySQL update in join query executes very slow

I got two tables which are xp_pn_resale and xp_guru_properties . I want to update the column postal_code from xp_pn_resale but the data comes from the xp_guru_properties and it is in a json object so I need to extract. I tried to write an query but it does not respond or not taking any output. Does anyone know whats the problem? Thanks

Here is my select query for reference and test

select r.block, r.street_name, 
r.postal_code, p.property_name,
r.property_name as resale_property_name,
JSON_UNQUOTE(JSON_EXTRACT(p.json, '$.postcode') )as postcode
from xp_pn_resale AS r 
JOIN xp_guru_properties AS p 
ON r.property_name = p.property_name where r.postal_code is null 
limit 10

Result在此处输入图片说明

I just want the postcode to be the value of postal_code from the query, I just set the limit to 10 because it takes too much time when i dont do limit

Also here is my update statement

UPDATE xp_pn_resale AS r
JOIN xp_guru_properties AS p ON r.property_name = p.property_name
SET r.postal_code = JSON_UNQUOTE(JSON_EXTRACT(p.json, '$.postcode'))
where r.property_name = p.property_name

The query above does not do anything, it keeps executing. I dont know whats wrong with the query i wrote

I got 800k data in xp_pn_resale and 30k in xp_guru_properties

First, your query does not need the WHERE clause, so:

UPDATE xp_pn_resale r JOIN
       xp_guru_properties p
       ON r.property_name = p.property_name
    SET r.postal_code = JSON_UNQUOTE(JSON_EXTRACT(p.json, '$.postcode'));

I might suspect one of two things. The first is an index on xp_guru_properties(property_name) . That is important for performance.

The second is that there are multiple matches in the properties table for a single property. That will really slow things down.

Try this query

UPDATE xp_pn_resale
INNER JOIN xp_guru_properties
ON xp_pn_resale.property_name = xp_guru_properties.property_name
SET xp_pn_resale.postal_code = JSON_UNQUOTE(JSON_EXTRACT(xp_guru_properties.json, '$.postcode'))
WHERE xp_pn_resale.postal_code is NULL

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