Jump to content
davewx

Efficient MYSQL Join or Update

Recommended Posts

I have two tables of point data. The first main table has about 50,000 rows of data with values that represent the temperature for a grid point. I also have another table with about 25,000 rows of data. I'm trying to update the values from the second table into the first table for the closest point. Doing this using spatial functions in MYSQL would likely be slow, so I decided to do a spatial join in ArcGIS to give them relating ID's. As such, there are repeating id's from the second table in the first table as expected. This is because the grid spacing in the first (main) table is much smaller than the second table and there will be duplicate grid points with the same value...which is what I want. The only problem is, when I try to update the values by id = id, the query takes forever to complete. It seems as if its looping through each row for each time which seems very inefficient. 

 

Is there a better method for doing this?

UPDATE grid, hrdps_points SET grid.val=hrdps_points.value WHERE grid.hrdpsid = hrdps_points.id

Share this post


Link to post
Share on other sites

Two things to try

 

1) Ensure you have indexes set up on those id columns?

 

Then

 

2) Rewrite the query using explicit joins

UPDATE grid
    INNER JOIN 
    hrdps_points
       ON grid.hrdpsid = hrdps_points.id
SET
    grid.val = hrdps_points.value

Share this post


Link to post
Share on other sites

Wow thank you very much. The indexing was the issue. Query is basically instantaneous now. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.