快速生成mysql上百萬條測試數據

方案:編寫一個存儲過程循環添加數據sql

1. 建立表index_testspa

DROP TABLE IF EXISTS index_test;

CREATE TABLE index_test(

id BIGINT(20) PRIMARY KEY NOT NULL AUTO_INCREMENT,

USER VARCHAR(16) DEFAULT NULL,

psd varchar(64) default null

/*psd mediumint DEFAULT 0 存儲隨機數據*/

)ENGINE=MyISAM DEFAULT CHARSET=utf8;

2.建立存儲過程code

DELIMITER $$
DROP PROCEDURE IF EXISTS `insert_data`$$
CREATE PROCEDURE `insert_data`(IN tableName varchar(500),IN num INT)
BEGIN
  DECLARE n INT DEFAULT 1;/*定義一個變量,存儲當前執行的次數*/
    WHILE n <= num DO
        SET @sqlStr = CONCAT("INSERT INTO ",tableName,"(USER,psd) VALUES(CONCAT(",n,",'用戶'),password(",n,"))");
        PREPARE stmt FROM @sqlStr;
        EXECUTE stmt;
        set n=n+1;
  end while;
END $$

3.執行存儲過程插入數據:blog

call insert_data('index_test',1000000);table

所花時間class

相關文章
相關標籤/搜索