Skip to content
Advertisement

Update of many rows with join extremely slow

I have a table with five relevant fields – id, source, iid, track_hash, alias. I want to group all entries into groups with a common track_hash and then for each row save the id of the row with the lowest source (with ties broken in favor of the highest iid) entry from its group into the alias field. To do this I wrote the following query:

This takes a completely reasonable 2 seconds on 24k rows. Now, instead of simply seeing this id, I want to actually save it. For this, I used the following very similar query:

However, this one takes anywhere between 1 and 10 minutes, and I really don’t understand why. Doesn’t the engine have to match every row to its best.id/alias anyway, which is exactly what I’m doing with my update? Why is this happening and what am I doing wrong?

Query plan looks like this:

Advertisement

Answer

You don’t need the join to albums (again).
The UPDATE ... FROM syntax provides actually an implicit join of albums to best:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement