I have a table for chat messages like:
id | fk_user_1_id | fk_user_2_id | message | timestamp
I only need to keep the 50 most recent messages around per friend pair. What's the simplest way to do this? An awful way is:
- Insert a new message.
- Select the 50 most recent messages, sorted in reverse chronological order.
- If the result has more than 50 records, grab the timestamp of the 50th one.
- Delete all records older than that timestamp for the friend pair.
Is there a simpler way to do this? I'm using mysql.
Thanks