Skip to content

Instantly share code, notes, and snippets.

@fredrike
Created December 4, 2012 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredrike/4201640 to your computer and use it in GitHub Desktop.
Save fredrike/4201640 to your computer and use it in GitHub Desktop.
Split huge array of rows to smaller chuncks
/*
* The idea is to test against the value $max_allowed_packet (in bytes)
* and make sure our sql don't get bigger than that.
* Before executing our query (the bigger INSERT the less overhead.
*/
// $rows is an array containing the rows to insert
while(count($rows)) {
$sql = "INSERT IGNORE INTO `table` VALUES ".
implode(',',array_splice($rows,0,25000)).
";".PHP_EOL;
if(!$db->query($sql))
die($db->error);
}
@fredrike
Copy link
Author

fredrike commented Dec 4, 2012

Perhaps something like this: http://stackoverflow.com/a/7942680/803002

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment