A
A
ALEXANDR PETRIN2021-06-11 14:49:47
PostgreSQL
ALEXANDR PETRIN, 2021-06-11 14:49:47

How to delete an entry in a table after 7 days?

There are records in the Users table.

Each entry can be added at different times.
Each entry can be kept for a maximum of 7 days.

Can I make it so that the record is deleted after this period? (Roughly speaking, assign each entry a field with a timer). I read that you need to use moment, but I can’t figure out how to do it.

Another small question. If it is still possible to do this, will it heavily load the database, let's say, approximately, with 10'000 records?

I am using Sequelize 6.6.2 / PostgreSQL 8.6.0

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
WinPooh32, 2021-06-11
@dataarray

Store the creation date of the record and simply exclude rows that are older than 7 days relative to the query time when fetching.

Another small question. If it is still possible to do this, will it heavily load the database, let's say, approximately, with 10'000 records?

10k is a very small number of records. At this volume it will not be noticeable.
But if you have hundreds of thousands of insertions and deletions, then friezes are guaranteed.

R
Rag'n' Code Man, 2021-06-12
@iDmitriyWinX

I have never worked with SQL databases, only with MongoDB, and there is such a thing as Time To Lifeindex, that is, we mark the field with the creation date (UTC format) of this record with the TTL index and set the ExpiresAfrerSeconds parameter (in the week 60 * 60 * 24 * 7 seconds) and as a result, the database will delete this record for us in a week.
I don't know about postgre, but maybe you should read about TTL in your database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question