I have a query, that finds the newest item of each group based on the ID returned by a subquery. The problem is, that the table has a bit over 5000 entries and even with only this amount of entries the query takes almost 5 minutes to complete. I believe it should be possible to do this more efficiently. Can anyone help me do some optimization?
Here is the query in question.
SELECT *
FROM ast a
WHERE a.id = ( SELECT id
FROM ast
WHERE a.tid = tid
ORDER BY timestamp DESC LIMIT 1)
The timestamp
column is taken from the subquery table. I didn't assign an alias to it.
I am using MariaDB 10.3.39.