Implementing a Circular Buffer (Sliding window) in MySQL
Question
I intend to store some Java objects in a MySQL database, accompanied by a timestamp. These objects should be kept in a Sliding Window fashion (also known as Circular Buffer), meaning that only the last N
items from a specific type should be kept.
My general idea is a an INSERT
trigger:
DELETE FROM pixels WHERE type="type_of_new_pixel"
AND id NOT IN
(SELECT id FROM pixels
WHERE type="type_of_new_pixel"
ORDER BY timestamp DESC LIMIT N);
- Is triggger the right way to go? How do I add the trigger to the table?
- In order to increase performance, I would like to activate the trigger every M
INSERT
s. I will have some extra items in my list, but that's OK. How do I implement this? Will aid % 100 == 0
check do?
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange