MySQL triggers are simple. Most of them are used to update third-party tables. Today I will demonstrate the functions of MySQL triggers in Data creation.
The result of the base table is as follows:
- CREATETABLE 'tb1 '(
- 'Id'varchar (255) NOTNULL,
- 'Log _ date' dateDEFAULTNULL,
- PRIMARYKEY ('id ')
- ) ENGINE = InnoDB default charset = utf8 ROW_FORMAT = FIXED
This is the batch insert Stored Procedure for the base table:
- Create definer = 'root' @ 'localhost'
- PROCEDURE db_myisam.sp_generate_tb1_data (IN cur_1 INT,
- IN f_input INT,
- IN f_commit_num INT
- -- Stands for which month's date.
- )
- BEGIN
- DECLARE t_count INT;
- DECLARE v_log_date DATE;
- DECLARE c_date DATEDEFAULT '2017-07-01 ';
- SET t_count = cur_1 + (f_input-1 );
- SET @ autocommit = 0;
- WHILE cur_1 <= t_count
- DO
- IF mod (cur_1, f_commit_num) = 1 THEN
- Start transaction;
- End if;
- SET v_log_date = date_add (c_date, INTERVAL ceil (rand () * 3) * (ceil (rand () * 15) DAY );
- INSERTINTO tb1 (id, log_date) VALUES (cur_1, v_log_date );
- IF mod (cur_1, f_commit_num) = 0 THEN
- COMMIT;
- End if;
- SET cur_1 = cur_1 + 1;
- End while;
- COMMIT;
- END
Single-threaded Data creation:
- Mysql> call sp_generate_tb1_data (200 );
- Query OK, 0 rows affected (8 minutes 20.00 sec)
It takes more than 8 minutes to record 1 kW rows, that is, a thread inserts 2 million records per second.
- Mysql> selectcount (*) from tb1;
- + ---------- +
- | Count (*) |
- + ---------- +
- | 1, 10000000 |
- + ---------- +
- 1 row inset (34.35 sec)
Create a copy table:
- Createtable tb2 like tb1;
- Createtable fig like tb1;
Here, it is quite disgusting that a table of is created, because MySQL triggers do not support self-insertion. This is a post-insert Trigger Based on table:
- CREATE
- DEFINER = 'root' @ 'localhost'
- TRIGGER db_myisam.ti_tb3_after
- AFTERINSERT
- ON db_myisam.78
- FOR EACH ROW
- BEGIN
- DECLARE v_cur_1 INTDEFAULT 1;
- DECLARE v_log_date DATE;
- DECLARE c_date DATEDEFAULT '2017-07-01 ';
- WHILE v_cur_1 <= 10000000
- DO
- SET v_log_date = date_add (c_date, INTERVAL ceil (rand () * 3) * (ceil (rand () * 15) DAY );
- INSERTINTO tb2 (id, log_date) VALUES (v_cur_1, v_log_date );
- SET v_cur_1 = v_cur_1 + 1;
- End while;
- END
It takes less than 6 minutes for a 1 kW record, that is, a thread inserts more than records per second.
- Mysql> insertinto fig (2, current_date ());
- Query OK, 1 row affected (5 minutes 14.07 sec)
As you can see, the speed is about 60% higher than that of the stored procedure. How are you doing? HAPPY?