我一直在运行一个相对简单的脚本,但它往往会锁定数据库。
每次插入查找值时,都会检查它以确保它不会再次插入。
这在小数据集(< 50k)上非常有效,但是它存在大数据集(> 2m)的问题。任何帮助将不胜感激。
$insertCounter = 20;
$matchCounter = 200;
$insertIndex = 0;
$sqlInsert = 'INSERT INTO `database` (`lookup_value`, `timestamp`, `source`) VALUES ';
$matchIndex = 0;
$resetCount = 0;
$indexCounter = 0;
foreach ($matches as $lookup) {
$sqlSelect = 'SELECT `id` FROM `database` WHERE `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 INTO `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>';
}
}
答案 0 :(得分:0)
如何使用lookup_value字段添加UNIQUE索引然后使用INSERT IGNORE语句?