MySQL - how many rows can I insert in one single INSERT statement?
Asked Answered
F

8

108

Does it depend on the number of values sets? Does it depend on the number of bytes in the INSERT statement?

Fatling answered 21/8, 2010 at 3:7 Comment(0)
H
104

You can insert infinitely large number of records using INSERT ... SELECT pattern, provided you have those records, or part of, in other tables.

But if you are hard-coding the values using INSERT ... VALUES pattern, then there is a limit on how large/long your statement is: max_allowed_packet which limits the length of SQL statements sent by the client to the database server, and it affects any types of queries and not only for INSERT statement.

Houseleek answered 21/8, 2010 at 3:22 Comment(0)
T
62

Ideally, Mysql allow infinite number of rows creation in single insert (at once) but when a

MySQL client or the mysqld server receives a packet bigger than max_allowed_packet bytes, it issues a Packet too large error and closes the connection.

To view what the default value is for max_allowed_packet variable, execute the following command in in MySQL:

show variables like 'max_allowed_packet';

Standard MySQL installation has a default value of 1048576 bytes (1MB). This can be increased by setting it to a higher value for a session or connection.

This sets the value to 500MB for everyone (that's what GLOBAL means):

SET GLOBAL max_allowed_packet=524288000;

check your change in new terminal with new connection:

show variables like 'max_allowed_packet';

Now it should work without any error for infinite records insert. Thanks

Tap answered 9/1, 2015 at 6:53 Comment(3)
500 MB isn't infinite. It's way much larger than default value, but still, not infinite.Cammycamomile
A question: what exactly is this limit for? The query itself? What about the data, bind when executing statement? Can I be safe, until strlen($query_with_questionmarks) < $max_alloweed_packet?Vevay
If you were trying to set it to the largest possible value, that is 1GB which would look like this: SET GLOBAL max_allowed_packet=1073741824Membranophone
P
25

Query is limited by max_allowed_packet in general.

Preteritive answered 21/5, 2012 at 12:26 Comment(0)
S
14

You will hit the max_allowed_packet limit and

error: 1390 Prepared statement contains too many placeholders.

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.

Import of 50K+ Records in MySQL Gives General error: 1390 Prepared statement contains too many placeholders

Solly answered 22/9, 2017 at 9:36 Comment(0)
T
10

refer to http://forums.mysql.com/read.php?20,161869, it's related with your mysql's configuration: max_allowed_packet, bulk_insert_buffer_size, key_buffer_size.

Tympanum answered 8/6, 2013 at 9:19 Comment(0)
C
6

You can insert an infinite number of rows with one INSERT statement. For example, you could execute a stored procedure that has a loop executed a thousand times, each time running an INSERT query.

Or your INSERT could trip a trigger which itself performs an INSERT. Which trips another trigger. And so on.

No, it does not depend on the number of value sets. Nor does it depend on the number of bytes.

There is a limit to how deeply nested your parentheses may be, and a limit to how long your total statement is. Both of these are referenced, ironically, on thedailywtf.com . However, both of the means I mentioned above get around these limits.

Chinquapin answered 21/8, 2010 at 3:14 Comment(5)
Your examples are for running multiple INSERT, and it does not show about inserting multiple rows in one INSERT statement ..Houseleek
@Lukman: One INSERT query can result in multiple INSERTS hitting the database. It's just a matter of who's counting what.Chinquapin
true that, but your 1st example show about running query in a loop with each loop running one INSERT statement, and it says nothing about that one INSERT statement inserting multiple rows. it's just a matter of emphasis.Houseleek
Nobody can do infinite anything. There are always resource limits.Poach
Does this apply to SELECT ... FROM statement too? I mean if data has been inserted in the first place with max_allowed_packet, shouldn't this be ale to be selected after that too?. I don't get it why it's applied to all other statements like the other poster said when it first has to be inserted before anything else anyway.Neoterize
C
3

I believe there's no defined number of rows you're limited to inserting per INSERT, but there may be some sort of maximum size for queries in general.

Cleavable answered 21/8, 2010 at 3:13 Comment(0)
K
3

It is limited by max_allowed_packet.
You can specify by using: mysqld --max_allowed_packet=32M It is by default 16M.
You can also specify in my.cnf in /etc/mysql/

Kioto answered 27/3, 2013 at 5:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.