For each user_id (group) in a table I want to delete all records except the records with the highest ID in each group. Something similar to this: Delete all records except the most recent one?
In my case it would look like:
DELETE FROM logins
WHERE user_id IS NOT NULL AND id NOT IN (SELECT MAX(id) FROM logins WHERE user_id IS NOT NULL GROUP BY user_id)
The problem is, the table holds 100+ millon records, so doing this in a single query like that would be impossible performance wise.
What other ways could I do this, e.g. in some batches?