I am trying to develop a query that will delete all but the most recently added row in a database. This is based on a Timestamp field, that is stored as a string and a User ID field that is stored as a string..
table.Timestamp -> text field
table.Retrieving_User -> text field
This is the query I have developed. We have around 50K records in this database and it runs very slowly. I hope its not because of the string to date conversion that I'm doing, because this needs to be done.
DELETE
FROM `table` main
WHERE (main.Retrieving_User, STR_To_DATE( main.Timestamp , '%a %b %d %H:%i:%s CST %Y' )) NOT IN
(SELECT sub.Retrieving_User, MAX( STR_To_DATE( sub.Timestamp , '%a %b %d %H:%i:%s CST %Y' ))
FROM `table` sub
WHERE sub.Retrieving_User = 'userID'
GROUP BY sub.Retrieving_User )
AND main.Retrieving_User = 'userID'
Does anyone know of a more efficient way of doing what I'm trying to do?