簡體   English   中英

PHP Mysql多次插入(大數據集)

[英]PHP Mysql multiple inserts (big dataset)

我一直在運行一個相對簡單的腳本,但是它傾向於鎖定數據庫。

每次插入查找值時,也會對它進行檢查,以確保不再插入它。

這在小數據集(<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>';
    }
}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM