Detailed explanation of MySQL database Event scheduled execution tasks

Detailed explanation of MySQL database Event scheduled execution tasks

1. Background

As the project's business continues to move forward, it is inevitable that the number of database tables will become larger and larger, constantly occupying hard disk space. Even a larger space cannot support the growth of business, so it is necessary to delete unnecessary data regularly. In our project, due to the lack of data cleaning, the space occupied by a table reached as much as 4G. Just think how scary it is...

Here we introduce how to use MySQL to create a timer Event to regularly clear previous unnecessary events.

2. Content

#1. Create a stored procedure for events to call delimiter//
drop procedure if exists middle_proce//
create procedure middle_proce()
begin
DELETE FROM jg_bj_comit_log WHERE comit_time < SUBDATE(NOW(),INTERVAL 2 MONTH);
optimize table jg_bj_comit_log;
DELETE FROM jg_bj_order_create WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH);
optimize table jg_bj_order_create;
DELETE FROM jg_bj_order_match WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH);
optimize table jg_bj_order_match;
DELETE FROM jg_bj_order_cancel WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH);
optimize table jg_bj_order_cancel;
DELETE FROM jg_bj_operate_arrive WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH);
optimize table jg_bj_operate_arrive;
DELETE FROM jg_bj_operate_depart WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH);
optimize table jg_bj_operate_depart;
DELETE FROM jg_bj_operate_login WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH);
optimize table jg_bj_operate_login;
DELETE FROM jg_bj_operate_logout WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH);
optimize table jg_bj_operate_logout;
DELETE FROM jg_bj_operate_pay WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH);
optimize table jg_bj_operate_pay;
DELETE FROM jg_bj_position_driver WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH);
optimize table jg_bj_position_driver;
DELETE FROM jg_bj_position_vehicle WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH);
optimize table jg_bj_position_vehicle;
DELETE FROM jg_bj_rated_passenger WHERE created_on < SUBDATE(NOW(),INTERVAL 3 MONTH);
optimize table jg_bj_rated_passenger;
end//
delimiter;

#2. Enable event (for the timing to work, the MySQL constant GLOBAL event_scheduler must be on or 1)
show variables like 'event_scheduler'
set global event_scheduler='on'

#3、Create Evnet event drop event if exists middle_event;
create event middle_event
on schedule every 1 DAY STARTS '2017-12-05 00:00:01'
on completion preserve ENABLE
do call middle_proce();

#4、Open Event alter event middle_event on completion preserve enable;

#5. Close Event event alter event middle_event on completion preserve disable;

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to use PHP mysql_fetch_row to query and obtain a list of data rows
  • 5 MySQL GUI tools recommended to help you with database management
  • A brief introduction to the command line tool mycli for operating MySQL database
  • Why should the number of rows in a single MySQL table not exceed 5 million?
  • PHP uses mysqli and pdo extensions to test and compare the execution efficiency of mysql database. Complete example
  • Will MySQL execute the update statement again if it has the same data as the original one?
  • IDEA uses properties configuration file to connect to MySQL database
  • How to use binlog for data recovery in MySQL
  • Solve the Chinese garbled problem of mysql5.5 database command line under Windows 10
  • Java connects, queries and modifies MySQL database
  • Detailed explanation of MySQL data rows and row overflow mechanism

<<:  Detailed explanation of the use of Vue3 state management

>>:  How to install ionCube extension using pagoda

Recommend

Trash-Cli: Command-line Recycle Bin Tool on Linux

I believe everyone is familiar with the trashcan,...

MySQL 8.0 upgrade experience

Table of contents Preface 1. First completely uni...

Solution to ONLY_FULL_GROUP_BY error in Mysql5.7 and above

Recently, during the development process, the MyS...

Tutorial on building nextcloud personal network disk with Docker

Table of contents 1. Introduction 2. Deployment E...

Summary of 3 minor errors encountered during MySQL 8.0 installation

Preface In the past, the company used the 5.7 ser...

MySQL 8.0.11 Community Green Edition Installation Steps Diagram for Windows

In this tutorial, we use the latest MySQL communi...

Steps to build MHA architecture deployment in MySQL

Table of contents MAH 1. Introduction to MAH Arch...

JavaScript implements large file upload processing

Many times when we process file uploads, such as ...

How to install MySQL under Linux (yum and source code compilation)

Here are two ways to install MySQL under Linux: y...

Vue implements partial refresh of the page (router-view page refresh)

Using provide+inject combination in Vue First you...

Advantages and disadvantages of common MySQL storage engines

Table of contents View all storage engines InnoDB...

React-native sample code to implement the shopping cart sliding deletion effect

Basically all e-commerce projects have the functi...