I have an Oracle table which contains event log messages for an application. We don’t want this table to grow out of control and eat up too much space, so we’d like to set a cap on the number of rows that it can contain to some big number like one million.
So we’d like to tell Oracle that when the table grows too large, it should delete the oldest rows to make space for new rows.
Is there any way to do this? I imagine we could do this with a trigger or by making all inserts with a stored procedure, but is there anything simpler?
EDIT: A couple of answers have suggested solutions that involve partitions. We do not currently partition this table, although we have the ability to do so if necessary. However, from looking into the matter, it seems that even if we partition the table, we’d still need for some kind of a scheduled job to drop the old partitions, etc. So we’ve decided to forgo partitions in favor of a scheduled job to check the row count and delete old rows as necessary once per day.
Thanks for the help, everyone.
Depending on your definition of simpler…
My bias would be to schedule a job that runs periodically (say, nightly) in order to delete the oldest rows. If you expect that you could generate a million event logs in a single day, you may want the job to run more frequently, but nightly is generally sufficient for most people. This has the advantage of being asynchronous so that you’re not incurring the overhead of counting a million rows every time you do an insert to find out if you have to purge some data. On the other hand, it does potentially require some administration to set up the job.