I have a small database on an embedded system with some tables which I want to limit to 1000 rows. To do this, I have set up a trigger which is executed after every new row insertion. Trigger's code is:
CREATE TRIGGER after_insertion_trigger
AFTER INSERT ON table
BEGIN
DELETE FROM table
WHERE uuid IN (
SELECT uuid FROM table ORDER BY sent ASC, timestamp DESC LIMIT -1 OFFSET 1000
);
END;
Problem is, that insertions into a full table when using triggers take more than 3 minutes each instead of 0.4 seconds, which is the time needed to complete a normal transaction of insert and delete sentences.
The time needed to complete an insertion when using triggers seems to increase as database rows increase too.
I have found many different threads in Stack Overflow and other webs about triggers, but none of them helped me to solve the problem. I am using SQLite 3.7.9.
Does anybody know why am I getting this poor performance? I don't need a huge performance because insertions would not occur very often, but 3 minutes for an insertion is inadmissible.