Mysql implements regular clearing of old data in a table and retaining several pieces of data (recommended)

Mysql implements regular clearing of old data in a table and retaining several pieces of data (recommended)

To achieve the following goals:

Mysql database will make a regular judgment on a table in a library at regular intervals (it can be 2 hours or a day, which can be customized). If the data in this table exceeds 20 records (this data is also customized, it can also be 200 records), the latest 10 records will be retained (this data can also be customized, but it must be less than or equal to the previous number of data records).

Let me briefly talk about the solution (derived from back to front):

1. Start a timer, which does two things:

⑴Set the time interval

⑵Call a stored procedure

2. Write a stored procedure that does two things:

⑴ Determine whether the number of data entries in the table exceeds 20. If it exceeds 20, perform the following steps.

⑵ Keep the latest 10 data and delete the rest of the old data. This requires that the table must have an increasing primary key id, so that the value of the latest data id is larger. Just find the largest id in the current table and subtract 10 to get a 'delete node', and then write where id <'delete node' in the delete statement. Although the result may not be accurate, it can roughly achieve the effect.

Suppose there is a datas table, which has a primary key id that is incremented. The data in this table will continue to increase. Now we need to keep the latest 10 data in the datas table every 5 seconds and delete the others.

The code process is as follows:

1. First, define a stored procedure named pro_clear_data. Note that the vertical bar (“|”) must not be missing.

DELIMITER |
 DROP PROCEDURE IF EXISTS pro_clear_data |
 CREATE PROCEDURE pro_clear_data()
  BEGIN 
  
   SET @datas_count=(SELECTCOUNT(id) FROM datas);
	   IF(@datas_count>20) THEN
	  
   SET @max_id=(SELECT MAX(id) FROM datas);  
	 SET @max_id = @max_id - 10;
    DELETE FROM `datas` WHERE id<@max_id;   
  END IF ;
  
  END
 |

2. Create a timer named event_time_clear_data

SET GLOBAL event_scheduler = 1; 
 CREATE EVENT IF NOT EXISTS event_time_clear_data
 
 ON SCHEDULE EVERY 5 SECOND
 
 ON COMPLETION PRESERVE 
 
DO CALL pro_clear_data();

3. This is the simplest but also the most important. We have to start the timer manually, otherwise it will not work.

ALTER EVENT event_time_clear_data ON 
 
COMPLETION PRESERVE ENABLE;

Create stored procedures and create timer codes separately.

The data will be automatically cleared every 5 seconds, and the latest 10 records will be retained.

In addition, the code to turn off the timer is:

ALTER EVENT event_time_clear_data ON 
 
 COMPLETION PRESERVE DISABLE;

The code to delete the stored procedure is:

DROP PROCEDURE pro_clear_data;

About Event:

MySQL version 5.1 began to introduce the concept of event. Event is a "time trigger", which is different from the event trigger of triggers. Event is similar to the Linux crontab scheduled task and is used for time triggering. By using it alone or calling a stored procedure, the related SQL statement or stored procedure is triggered at a specific point in time.

Delete Event:

DROP EVENT IF EXISTS event_time_clear_data1

This is the end of this article about how to implement MySQL to periodically clear old data in a table and retain several pieces of data. For more relevant MySQL periodic data clearing content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Example and analysis of MySQL clearing data tables
  • MySQL query duplicate data (delete duplicate data and keep the one with the smallest id as the only data)
  • Mysql delete duplicate data to keep the smallest id solution
  • Detailed explanation of how to find and delete duplicate data in MySQL and keep only one example

<<:  Some parameter descriptions of text input boxes in web design

>>:  A brief discussion on the understanding of TypeScript index signatures

Recommend

Free tool to verify that HTML, CSS and RSS feeds are correct

One trick for dealing with this type of error is t...

5 super useful open source Docker tools highly recommended

Introduction The Docker community has created man...

VS2019 connects to mysql8.0 database tutorial with pictures and text

1. First, prepare VS2019 and MySQL database. Both...

Layui implements sample code for multi-condition query

I recently made a file system and found that ther...

Let's talk about the difference between MyISAM and InnoDB

The main differences are as follows: 1. MySQL use...

How to delete an image in Docker

The command to delete images in docker is docker ...

Pagination Examples and Good Practices

<br />Structure and hierarchy reduce complex...

How to add Nginx to system services in CentOS7

Introduction After compiling, installing and solv...

Steps to repair grub.cfg file corruption in Linux system

Table of contents 1. Introduction to grub.cfg fil...

How to import/save/load/delete images locally in Docker

1. Docker imports local images Sometimes we copy ...

How to use JavaScript to determine several common browsers through userAgent

Preface Usually when making h5 pages, you need to...