Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting multiple rows in mysql

Is the database query faster if I insert multiple rows at once:

like

INSERT....  UNION  INSERT....  UNION 

(I need to insert like 2-3000 rows)

like image 991
Emma Avatar asked Jul 31 '11 11:07

Emma


People also ask

Can you insert multiple rows in SQL at once?

Answer. Yes, instead of inserting each row in a separate INSERT statement, you can actually insert multiple rows in a single statement. To do this, you can list the values for each row separated by commas, following the VALUES clause of the statement.

How can I insert more than 1000 rows in MySQL?

Or you can go to Edit -> Preferences -> SQL Editor -> SQL Execution and set the limit on Limit Rows Count.

How many rows can you insert at once MySQL?

You can put 65535 placeholders in one sql.So if you have two columns in one row,you can insert 32767 rows in one sql.


2 Answers

INSERT statements that use VALUES syntax can insert multiple rows. To do this, include multiple lists of column values, each enclosed within parentheses and separated by commas.

Example:

INSERT INTO tbl_name     (a,b,c) VALUES     (1,2,3),     (4,5,6),     (7,8,9); 

Source

like image 108
Nicola Cossu Avatar answered Oct 13 '22 05:10

Nicola Cossu


If you have your data in a text-file, you can use LOAD DATA INFILE.

When loading a table from a text file, use LOAD DATA INFILE. This is usually 20 times faster than using INSERT statements.

Optimizing INSERT Statements

You can find more tips on how to speed up your insert statements on the link above.

like image 40
Jacob Avatar answered Oct 13 '22 06:10

Jacob