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

Solution to data duplication when using limit+order by in MySql paging

Table of contents summary Problem Description Ana...

MySQL 8.0 DDL atomicity feature and implementation principle

1. Overview of DDL Atomicity Before 8.0, there wa...

MySQL slow log online problems and optimization solutions

MySQL slow log is a type of information that MySQ...

win10 docker-toolsbox tutorial on building a php development environment

Download image docker pull mysql:5.7 docker pull ...

Some things to note about varchar type in Mysql

Storage rules for varchar In versions below 4.0, ...

Ubuntu 19.04 installation tutorial (picture and text steps)

1. Preparation 1.1 Download and install VMware 15...

How to store false or true in MySQL

MySQL Boolean value, stores false or true In shor...

The actual process of encapsulating axios in the project

Table of contents Preface Benefits of axios encap...

Singleton design pattern in JavaScript

Table of contents 1. What is a design pattern? 2....

Detailed explanation of DOM DIFF algorithm in react application

Table of contents Preface What is VirtualDOM? Rea...

MySQL 8.0.24 installation and configuration method graphic tutorial

This article shares the installation tutorial of ...

Docker advanced method of rapid expansion

1. Command method Run the nginx service in the cr...

How to install babel using npm in vscode

Preface The previous article introduced the insta...

How to use native JS to implement touch sliding monitoring events

Preface I wrote a small demo today. There is a pa...