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

About the layout method of content overflow in table

What is content overflow? In fact, when there is ...

Implementation of IP address configuration in Centos7.5

1. Before configuring the IP address, first use i...

Thumbnail hover effect implemented with CSS3

Achieve resultsImplementation Code html <heade...

Super detailed steps to install zabbix3.0 on centos7

Preface Recently, part of the company's busin...

mysql server is running with the --skip-grant-tables option

The MySQL server is running with the --skip-grant...

The best way to start a jar package project under Centos7 server

Preface Everyone knows how to run a jar package o...

HTTP and HTTP Collaboration Web Server Access Flow Diagram

A web server can build multiple web sites with in...

A brief summary of vue keep-alive

1. Function Mainly used to preserve component sta...

Detailed explanation of various loop speed tests in JS that you don’t know

Table of contents Preface 1. for loop 2. while lo...

JavaScript implements the protocol example in which the user must check the box

In js, set the user to read a certain agreement b...

Dissecting the advantages of class over id when annotating HTML elements

There are very complex HTML structures in web pag...

Introduction to JavaScript conditional access attributes and arrow functions

Table of contents 1. Conditional access attribute...

Vue code highlighting plug-in comprehensive comparison and evaluation

Table of contents Comprehensive comparison From t...

Two-hour introductory Docker tutorial

Table of contents 1.0 Introduction 2.0 Docker Ins...

Why MySQL should avoid large transactions and how to solve them

What is a big deal? Transactions that run for a l...