davewx Posted November 18, 2017 Share Posted November 18, 2017 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 Quote Link to comment https://forums.phpfreaks.com/topic/305688-efficient-mysql-join-or-update/ Share on other sites More sharing options...
Barand Posted November 18, 2017 Share Posted November 18, 2017 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 Quote Link to comment https://forums.phpfreaks.com/topic/305688-efficient-mysql-join-or-update/#findComment-1553930 Share on other sites More sharing options...
davewx Posted November 18, 2017 Author Share Posted November 18, 2017 Wow thank you very much. The indexing was the issue. Query is basically instantaneous now. Quote Link to comment https://forums.phpfreaks.com/topic/305688-efficient-mysql-join-or-update/#findComment-1553934 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.