简体   繁体   English

PHP Mysql多次插入(大数据集)

[英]PHP Mysql multiple inserts (big dataset)

I have been running a relatively simple script, however it tends to lock up the database. 我一直在运行一个相对简单的脚本,但是它倾向于锁定数据库。

Each time a lookup value is inserted, it is also checked against to make sure it isnt inserted again. 每次插入查找值时,也会对它进行检查,以确保不再插入它。

This works very well on small data sets (<50k), however it has issues with large data sets (>2m). 这在小数据集(<50k)上效果很好,但是在大数据集(> 2m)上存在问题。 Any help would be appreciated. 任何帮助,将不胜感激。

$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>';
    }
}

如何添加带有lookup_value字段的UNIQUE索引,然后使用INSERT IGNORE语句?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM