PHP Max amount of inserts in one SQL query

Sten Van den Bergh picture Sten Van den Bergh · Apr 20, 2011 · Viewed 8.5k times · Source

I have a pretty simple question. I am inserting a lot of records at once in a MySQL table. It works for about 2000 records (actually a bit more). But say I want to insert 3000 records, than it doesn't do anything.

I'm working through AS3 sending an array containing all the records via AMFPHP to a simple PHP script to parse and insert the array.

Is this normal, or should I look into it?

Currently I'm slicing my array in parts of 2000 records, and sending a couple AMFPHP requests instead of just 1.

Answer

Marc B picture Marc B · Apr 20, 2011

PHP's queries are limited by the "max_allowed_packet" configuration option. It defines the absolute length limit, in characters, that a query string can be. Note that this isn't just the total size of the data being inserted, it's the entire query string. SQL commands, punctuation, spaces, etc...

Check how long your 3000 record version is vs. the 2000 one, and then get your server's packet length limit:

SHOW VARIABLES WHERE Variable_name LIKE  '%max_allowed_packet%'

If your 3000-record version is longer than this limit, the query will defnitely fail because it'll be chopped off somewhere part-way