Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quickest way to Insert mass data Into Mysql database [duplicate]

Tags:

php

mysql

I actually have a list of 100,000 records which I'd like to insert into the MySQL database.

I have tried to insert them with foreach and simple INSERT INTO however it took a lot of time to insert even 100 row. Like 1 second / row.

IS there any method to insert these rows much faster?

like image 848
Taboo Loco Avatar asked Mar 18 '13 10:03

Taboo Loco


People also ask

How can insert 1000 records at a time in MySQL?

How can insert 1000 records at a time in MySQL? MySQL INSERT multiple rows statement In this syntax: First, specify the name of table that you want to insert after the INSERT INTO keywords. Second, specify a comma-separated column list inside parentheses after the table name.

How do you make MySQL insert faster?

You can use the following methods to speed up inserts: If you are inserting many rows from the same client at the same time, use INSERT statements with multiple VALUES lists to insert several rows at a time. This is considerably faster (many times faster in some cases) than using separate single-row INSERT statements.

How do I insert multiple records at a time in MySQL?

Insert multiple rows in MySQL with the help of “values”. You can enclose the values with parentheses set with comma separation.

Which is more efficient to insert data into a table in MySQL?

Using INSERT to add multiple rows at onceInserting records one statement at a time is more time consuming and less efficient than inserting multiple rows at once. MySQL allows you to specify multiple rows to add to the same table.


2 Answers

Using one INSERT statement with multiple rows is faster than one INSERT statement per row. This will reduce calls to the database.

Example:

 INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
like image 139
weelee Avatar answered Nov 03 '22 08:11

weelee


Make packages:

INSERT INTO `table_name`(`column`) VALUES('value'),VALUES('value1'),VALUES('value2'), ..., VALUES('valuen');

documentation for insert

or export data to csv or other text format and use LOAD DATA, look here: load data by mysql client

like image 29
mkjasinski Avatar answered Nov 03 '22 08:11

mkjasinski