Mysql, triggers, vistas, eventos. Hoy he aprendido que los eventos son una buena opción para liberar espacio periodicamente de una tabla de mysql

Hoy he aprendido que los eventos son una buena opción para liberar espacio cada cierto tiempo de una tabla de mysql

Vía how to remove 6 months old data automatically from MYSQL innodb table


The Event Scheduler allows to execute regular events according to a schedule. There is detailed example in my post on the Stack Overflow, you just need to change time interval value from 24 hours to 6 months.

Firstly, make sure the Event Scheduler is enabled. To enable it use

SET GLOBAL event_scheduler = ON;

After that you could crate event that will check rows creation time and delete old records. For example

CREATE EVENT cleaning ON SCHEDULE EVERY 1 MONTH ENABLE
  DO 
  DELETE FROM MyTable
  WHERE `timestamp_column` < CURRENT_TIMESTAMP - INTERVAL 6 MONTH;

Si quisiera vaciar la tabla entera podría usar truncate

chevron_left
chevron_right

Dejar un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Comentario
Nombre
Correo electrónico
Web

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.