[Original] High-Performance Data creation using MySQL triggers

Source: Internet
Author: User

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:
 
 
  1. CREATE TABLE `tb1` ( 
  2.   `id` varchar(255) NOT NULL, 
  3.   `log_date` date DEFAULT NULL, 
  4.   PRIMARY KEY (`id`) 
  5. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED 
This is the batch insert Stored Procedure for the base table:
 
 
  1. CREATE DEFINER = 'root'@'localhost' 
  2. PROCEDURE db_myisam.sp_generate_tb1_data(IN cur_1        INT, 
  3.                                          IN f_input      INT, 
  4.                                          IN f_commit_num INT 
  5.                                          -- Stands for which month's date. 
  6. BEGIN 
  7.   DECLARE t_count     INT; 
  8.   DECLARE v_log_date DATE; 
  9.   DECLARE c_date  DATE DEFAULT '2012-07-01'; 
  10.  
  11.   SET t_count = cur_1 + (f_input - 1); 
  12.   SET @@autocommit = 0; 
  13.  
  14.   WHILE cur_1 <= t_count 
  15.   DO 
  16.     IF mod(cur_1, f_commit_num) = 1 THEN 
  17.       START TRANSACTION; 
  18.     END IF; 
  19.     SET v_log_date = date_add(c_date, INTERVAL ceil(rand() * 3) * (ceil(rand() * 15)) DAY); 
  20.     INSERT INTO tb1 (id, log_date) VALUES (cur_1, v_log_date); 
  21.     IF mod(cur_1, f_commit_num) = 0 THEN 
  22.       COMMIT; 
  23.     END IF; 
  24.     SET cur_1 = cur_1 + 1; 
  25.   END WHILE; 
  26.   COMMIT; 
  27. END 
Single-threaded Data creation:
 
 
  1. mysql> call sp_generate_tb1_data(1,10000000,200); 
  2. Query OK, 0 rows affected (8 min 20.00 sec) 
It takes more than 8 minutes to record 1 kW rows, that is, a thread inserts 2 million records per second.
 
 
  1. mysql> select count(*) from tb1; 
  2. +----------+ 
  3. | count(*) | 
  4. +----------+ 
  5. | 10000000 | 
  6. +----------+ 
  7. 1 row in set (34.35 sec) 
Create a copy table:
 
 
  1. create table tb2 like tb1; 
  2. create table tb3 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:
 
 
  1. CREATE  
  2.     DEFINER = 'root'@'localhost' 
  3. TRIGGER db_myisam.ti_tb3_after 
  4.     AFTER INSERT 
  5.     ON db_myisam.tb3 
  6.     FOR EACH ROW 
  7. BEGIN 
  8.   DECLARE v_cur_1    INT DEFAULT 1; 
  9.   DECLARE v_log_date DATE; 
  10.   DECLARE c_date     DATE DEFAULT '2012-07-01'; 
  11.  
  12.   WHILE v_cur_1 <= 10000000 
  13.   DO 
  14.     SET v_log_date = date_add(c_date, INTERVAL ceil(rand() * 3) * (ceil(rand() * 15)) DAY); 
  15.     INSERT INTO tb2 (id, log_date) VALUES (v_cur_1, v_log_date); 
  16.     SET v_cur_1 = v_cur_1 + 1; 
  17.   END WHILE; 
  18. END 
It takes less than 6 minutes for a 1 kW record, that is, a thread inserts more than records per second.
 
 
  1. mysql> insert into tb3 values (2,current_date()); 
  2. Query OK, 1 row affected (5 min 14.07 sec) 
As you can see, the speed is about 60% higher than that of the stored procedure. How are you doing? HAPPY?

This article is from "god, let's see it !" Blog, please be sure to keep this source http://yueliangdao0608.blog.51cto.com/397025/1066993

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.