Can you EMPTY a table (truncate) every X Minutes?

1

I have a little doubt, in which I have not found much information.

Could something be done, for each "5 minutes" (for example) to empty all the contents of a table?

The truth is that I have been investigating, and I have not found any useful information.

Thank you very much everyone in advance.

    
asked by Javier Avila Fernandez 04.09.2018 в 22:26
source

2 answers

1

An option to do this from the same database is to create a scheduled event (schedule event) which you can do it in the following way

To know if the scheduler is activated you use this query:

SELECT @@event_scheduler;

If it were not activated, we activate it with the following query:

SET GLOBAL event_scheduler = ON;

And finally we generated the scheduled event:

CREATE 
EVENT 'truncateTable'
ON SCHEDULE EVERY 5 MINUTE STARTS '2016-03-23 00:00:00'
ON COMPLETION NOT PRESERVE
ENABLE
DO
TRUNCATE TABLE tuTabla;
    
answered by 04.09.2018 / 22:57
source
0

Javier

The option to empty a table every 5 min, is that you create a task that runs every 5 min at the operating system level, for example if you are in Linux you create a crontab that runs every 5 min where you give the command

run on a terminal as root user crontab -e and will open the cron editor where you could something like this:

* / 5 * * * * psql dbname -c "TRUNCATE TABLE table_name;" -p 5432 -U postgres

in case of using Postgresql

if mysql would be like this:

* / 5 * * * * mysql dbname -e "TRUNCATE TABLE table_name;" -P3306 -u root -p your_password

with this you would erase the table every 5 min

although if you do not have to reset the id autoincrementales or serial instead of truncate would use the delete statement

    
answered by 04.09.2018 в 22:51