Triggers realize the separation of real-time data and historical data

MySQL trigger realizes the separation of real-time data and historical data

In database design, it is often necessary to separate real-time data from historical data, so as to ensure the timeliness of real-time data, while also retaining historical data for backtracking and analysis. This article describes how to use a MySQL trigger (trigger) to separate real-time data from historical data, and set the real-time table to retain only the latest data.

Design of real-time table and history table

When designing database tables, we usually store real-time data in one table and historical data in another. Doing so can reduce the data volume of a single table and improve query efficiency.

We take the user table as an example and design two tables: real-time table (user_realtime) and history table (user_history). Among them, the real-time table only keeps the latest user data, and the history table saves all user data and records the modification time.

The structure of the real-time table is as follows:

CREATE TABLE user_realtime (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(50) NOT NULL,
    age INT NOT NULL,
    PRIMARY KEY (id)
);

The structure of the history table is as follows:

CREATE TABLE user_history (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(50) NOT NULL,
    age INT NOT NULL,
    modified_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
);

Implementation of the trigger

We need to set an insert trigger on the real-time table. Whenever new data is inserted, the trigger will insert the data into the history table and delete the old data in the real-time table. This ensures that only the latest data is in the live table.

The code to insert the trigger is as follows:

CREATE TRIGGER insert_user_realtime AFTER INSERT ON user_realtime
FOR EACH ROW
BEGIN
    -- 插入历史表
    INSERT INTO user_history(name, age) VALUES (NEW.name, NEW.age);
    -- 删除老数据
    DELETE FROM user_realtime WHERE id <> NEW.id;
END;

achieve results

Now let's test the trigger implementation.

First, insert a piece of data into the real-time table:

INSERT INTO user_realtime(name, age) VALUES ('Tom', 20);

There is only one piece of data in the real-time table:

SELECT * FROM user_realtime;
+----+------+-----+
| id | name | age |
+----+------+-----+
|  1 | Tom  |  20 |
+----+------+-----+

There is also a piece of data in the history table:

SELECT * FROM user_history;
+----+------+-----+---------------------+
| id | name | age | modified_time       |
+----+------+-----+---------------------+
|  1 | Tom  |  20 | 2021-12-31 15:00:00 |
+----+------+-----+---------------------+

Now insert a new piece of data and modify the original data:

INSERT INTO user_realtime(name, age) VALUES ('Jerry', 25);
UPDATE user_realtime SET age = 22 WHERE id = 1;

There is only one piece of latest data in the real-time table:

SELECT * FROM user_realtime;
+----+-------+-----+
| id | name  | age |
+----+-------+-----+
|  2 | Jerry |  25 |
+----+-------+-----+

There are two pieces of data in the history table, one piece of original data and one piece of modified data:

SELECT * FROM user_history;
+----+------+-----+---------------------+
| id | name | age | modified_time       |
+----+------+-----+---------------------+
|  1 | Tom  |  20 | 2021-12-31 15:00:00 |
|  2 | Tom  |  22 | 2021-12-31 15:01:00 |
+----+------+-----+---------------------+

Summarize

This article describes how to use MySQL triggers to separate real-time data from historical data, and set the real-time table to retain only the latest data.

In practical applications, tables can be designed according to business needs, and triggers can be used to separate data and retain historical data, thereby improving the efficiency of data query and analysis.

Guess you like

Origin blog.csdn.net/sinat_35773915/article/details/131758701#comments_28139470