如何加快MySQL插入数百万行的速度?

时间:2018-11-23 08:09:45

标签: mysql sql insert

我要向MySQL表插入10 million行。但它在10 h 12 m 13 s 29 ms中完成。如何加快速度?

这是我的SQL

USE foo;
DROP TABLE IF EXISTS tmp;
CREATE TABLE tmp (
  id         INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  school_id  CHAR(4) NOT NULL,
  student_id CHAR(6) NOT NULL,
  INDEX school_id(school_id),
  INDEX student_id(student_id),
  INDEX school_id_and_student_id(school_id, student_id)
);
DROP PROCEDURE IF EXISTS tmpproc;
CREATE PROCEDURE tmpproc() BEGIN
  DECLARE i INT UNSIGNED DEFAULT 0;
  WHILE i < 10000000 DO
    INSERT INTO tmp (school_id, student_id)
    VALUES (SUBSTR(MD5(RAND()) FROM 1 FOR 4), SUBSTR(MD5(RAND()) FROM 1 FOR 6));
    SET i = i + 1;
  END WHILE;
END;
CALL tmpproc();

0 个答案:

没有答案