PHP Mysql multiple inserts (big dataset) -
i have been running relatively simple script, tends lock database.
each time lookup value inserted, checked against make sure isnt inserted again.
this works on small data sets (<50k), has issues large data sets (>2m). appreciated.
$insertcounter = 20; $matchcounter = 200; $insertindex = 0; $sqlinsert = 'insert `database` (`lookup_value`, `timestamp`, `source`) values '; $matchindex = 0; $resetcount = 0; $indexcounter = 0; foreach ($matches $lookup) { $sqlselect = 'select `id` `database` `lookup_value` = \'' . $lookup . '\''; $qexisting = executesql($sqlselect); if (mysql_num_rows($qexisting) == 0) { $insertindex += 1; $sqlinsert .= '(\'' . strtolower($lookup) . '\', \'' . date('y-m-d h:i:s') . '\', \'database\'), '; if ($insertindex >= $insertcounter) { $sqlinsert = substr($sqlinsert, 0, strlen($sqlinsert) - 2); executesqlnoresult($sqlinsert); echo '<p><strong>' . date('y-m-d h:i:s') . '</strong><br />' . $sqlinsert . '</p>'; $sqlinsert = 'insert `database` (`lookup_value`, `timestamp`, `source`) values '; $insertindex = 0; } } mysql_free_result($qexisting); $matchindex += 1; $indexcounter += 1; if ($matchindex > $matchcounter) { $resetcount += 1; $matchindex = 0; echo '<p>(' . $indexcounter . ', reset no.' . $resetcount . ') counter reached, resetting.</p>'; } }
how adding unique index lookup_value field , using insert ignore statement?
Comments
Post a Comment