How to speed up MySQL Insertion of millions of rows?












0















I want to insert 10 million rows to MySQL table. But it completed in 10 h 12 m 13 s 29 ms. How to speed it up?



Here is my 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();









share|improve this question

























  • I don't know MySQL very well, but I'd see if smaller transactions help. Commit once in a while, for each 10000 rows perhaps?

    – jarlh
    Nov 23 '18 at 8:13











  • For my experience, insert multiple rows within one statement is very fast, and MySQL's dump are also in this way. But I don't know why.

    – Geno Chen
    Nov 23 '18 at 8:20













  • Also you can look at stackoverflow.com/questions/6889065.

    – Geno Chen
    Nov 23 '18 at 8:21











  • Possible duplicate of stackoverflow.com/questions/1793169.

    – Geno Chen
    Nov 23 '18 at 8:25











  • Remove all the indexes at first. Once you have completed the inserts then create index. Also you could just create one INDEX on school_id and student_id Having index while inserting introduces overhead.

    – AmeyaN99
    Nov 23 '18 at 10:11


















0















I want to insert 10 million rows to MySQL table. But it completed in 10 h 12 m 13 s 29 ms. How to speed it up?



Here is my 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();









share|improve this question

























  • I don't know MySQL very well, but I'd see if smaller transactions help. Commit once in a while, for each 10000 rows perhaps?

    – jarlh
    Nov 23 '18 at 8:13











  • For my experience, insert multiple rows within one statement is very fast, and MySQL's dump are also in this way. But I don't know why.

    – Geno Chen
    Nov 23 '18 at 8:20













  • Also you can look at stackoverflow.com/questions/6889065.

    – Geno Chen
    Nov 23 '18 at 8:21











  • Possible duplicate of stackoverflow.com/questions/1793169.

    – Geno Chen
    Nov 23 '18 at 8:25











  • Remove all the indexes at first. Once you have completed the inserts then create index. Also you could just create one INDEX on school_id and student_id Having index while inserting introduces overhead.

    – AmeyaN99
    Nov 23 '18 at 10:11
















0












0








0








I want to insert 10 million rows to MySQL table. But it completed in 10 h 12 m 13 s 29 ms. How to speed it up?



Here is my 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();









share|improve this question
















I want to insert 10 million rows to MySQL table. But it completed in 10 h 12 m 13 s 29 ms. How to speed it up?



Here is my 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();






mysql sql insert






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 1:33







BaiJiFeiLong

















asked Nov 23 '18 at 8:09









BaiJiFeiLongBaiJiFeiLong

590411




590411













  • I don't know MySQL very well, but I'd see if smaller transactions help. Commit once in a while, for each 10000 rows perhaps?

    – jarlh
    Nov 23 '18 at 8:13











  • For my experience, insert multiple rows within one statement is very fast, and MySQL's dump are also in this way. But I don't know why.

    – Geno Chen
    Nov 23 '18 at 8:20













  • Also you can look at stackoverflow.com/questions/6889065.

    – Geno Chen
    Nov 23 '18 at 8:21











  • Possible duplicate of stackoverflow.com/questions/1793169.

    – Geno Chen
    Nov 23 '18 at 8:25











  • Remove all the indexes at first. Once you have completed the inserts then create index. Also you could just create one INDEX on school_id and student_id Having index while inserting introduces overhead.

    – AmeyaN99
    Nov 23 '18 at 10:11





















  • I don't know MySQL very well, but I'd see if smaller transactions help. Commit once in a while, for each 10000 rows perhaps?

    – jarlh
    Nov 23 '18 at 8:13











  • For my experience, insert multiple rows within one statement is very fast, and MySQL's dump are also in this way. But I don't know why.

    – Geno Chen
    Nov 23 '18 at 8:20













  • Also you can look at stackoverflow.com/questions/6889065.

    – Geno Chen
    Nov 23 '18 at 8:21











  • Possible duplicate of stackoverflow.com/questions/1793169.

    – Geno Chen
    Nov 23 '18 at 8:25











  • Remove all the indexes at first. Once you have completed the inserts then create index. Also you could just create one INDEX on school_id and student_id Having index while inserting introduces overhead.

    – AmeyaN99
    Nov 23 '18 at 10:11



















I don't know MySQL very well, but I'd see if smaller transactions help. Commit once in a while, for each 10000 rows perhaps?

– jarlh
Nov 23 '18 at 8:13





I don't know MySQL very well, but I'd see if smaller transactions help. Commit once in a while, for each 10000 rows perhaps?

– jarlh
Nov 23 '18 at 8:13













For my experience, insert multiple rows within one statement is very fast, and MySQL's dump are also in this way. But I don't know why.

– Geno Chen
Nov 23 '18 at 8:20







For my experience, insert multiple rows within one statement is very fast, and MySQL's dump are also in this way. But I don't know why.

– Geno Chen
Nov 23 '18 at 8:20















Also you can look at stackoverflow.com/questions/6889065.

– Geno Chen
Nov 23 '18 at 8:21





Also you can look at stackoverflow.com/questions/6889065.

– Geno Chen
Nov 23 '18 at 8:21













Possible duplicate of stackoverflow.com/questions/1793169.

– Geno Chen
Nov 23 '18 at 8:25





Possible duplicate of stackoverflow.com/questions/1793169.

– Geno Chen
Nov 23 '18 at 8:25













Remove all the indexes at first. Once you have completed the inserts then create index. Also you could just create one INDEX on school_id and student_id Having index while inserting introduces overhead.

– AmeyaN99
Nov 23 '18 at 10:11







Remove all the indexes at first. Once you have completed the inserts then create index. Also you could just create one INDEX on school_id and student_id Having index while inserting introduces overhead.

– AmeyaN99
Nov 23 '18 at 10:11














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442800%2fhow-to-speed-up-mysql-insertion-of-millions-of-rows%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442800%2fhow-to-speed-up-mysql-insertion-of-millions-of-rows%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?