MySQL quickly generates millions test data

Source: Internet
Author: User
Tags rand

Ext. 54691590

1. Generating ideas
Using MySQL memory table insertion speed features, first use functions and stored procedures in the memory table to generate data, and then inserted from the memory table in the normal table
2. Create memory table and normal table

CREATE TABLE' vote_record_memory ' (' ID ')INT( One) not NULLauto_increment, 'user_id`VARCHAR( -) not NULL, ' vote_id 'INT( One) not NULL, ' group_id 'INT( One) not NULL, ' Create_time 'datetime  not NULL,    PRIMARY KEY(' id '),KEY' Index_id ' ('user_id`) USING HASH) ENGINE=MEMORY auto_increment= 1 DEFAULTCHARSET=Utf8

CREATE TABLE' Vote_record ' (' ID ')INT( One) not NULLauto_increment, 'user_id`VARCHAR( -) not NULL, ' vote_id 'INT( One) not NULL, ' group_id 'INT( One) not NULL, ' Create_time 'datetime  not NULL,    PRIMARY KEY(' id '),KEY' Index_user_id ' ('user_id`) USING HASH) ENGINE=INNODB auto_increment= 1 DEFAULTCHARSET=Utf8

3. Creating functions and stored procedures

CREATE FUNCTION' Rand_string ' (nINT)RETURNS varchar(255) CHARSET latin1BEGIN DECLAREChars_strvarchar( -)DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; DECLAREReturn_strvarchar(255)DEFAULT "' ;DECLAREIINT DEFAULT 0;  whileI<N doSETReturn_str=Concat (RETURN_STR,substring(Chars_str, Floor(1 + RAND()* +),1)); SETI=I+1; END  while; RETURNReturn_str;END

CREATE  PROCEDURE' Add_vote_memory ' (inchNint)BEGIN    DECLAREIINT DEFAULT 1;  while(I<=N) doINSERT  intoVote_record_memory (user_id, Vote_id,group_id,create_time)VALUEs(Rand_string ( -), Floor(RAND()*  +), Floor(RAND()*  -), now ()); SetI=I+1; END  while;END

4. Call the stored procedure

CALL add_vote_memory(1000000)

According to the computer performance can not take the same time, about time in the hour level, if the error memory is full, only modify max_heap_table_size parameters can be, Win7 modify the location as follows, Linux, Modify the my.cnf file to restart MySQL after the change, the memory table data will be lost after reboot

5, insert the ordinary table

INSERT into vote_record SELECT * from vote_record_memory
    • 1

6. Results

MySQL quickly generates millions test data

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.