在数据库插入之前检查记录是否存在

时间:2020-05-16 21:28:49

标签: php mysql pdo

我有一个数据库,其中包含超过640,000条记录,我每周都会使用JSON文件中的数据进行更新。我要做的只是将记录加载到当前不存在的数据库中。我的以下脚本可处理少量数据,但是当我尝试加载大文件时,它会超时(我收到500 Internal Server Error)。有更好的方法吗?

{{1}}

2 个答案:

答案 0 :(得分:-1)

我无法评论,我写这是答案,因为我的分数还不够。您可以像这样更改并尝试吗?

 $query = $conn->prepare("SELECT id FROM oer_search WHERE title=:title AND author=:author AND source=:source limit 1");

<?php
if(!session_id()) session_start();
ini_set('memory_limit', '2000M');

$url = 'json/OERrecordstest.json';
$contents = file_get_contents($url);
$records = json_decode($contents, true);
include("../config.php");

echo "<div class='card card-body'>";

if (!$_SESSION["records"]) {
    foreach ($records as $record) {
        $_SESSION["records"][$record["id"]] = $records;
    }
}
$i = 0;
foreach ($_SESSION["records"] as $record) {
    $i++;
    if ($i > 1000) break;

    $type = $record['type'];
    $name = $record['title'];
    $title = addslashes($name);
    $creator = $record['author'];
    $author = addslashes($creator);
    $link = addslashes($record['link']);
    $origin = $record['source'];
    $source = addslashes($origin);
    $description = addslashes($record['description']);
    $base_url = $record['base_url'];
    $isbn_number = $record['isbn_number'];
    $e_isbn_number = $record['e_isbn_number'];
    $publication_date = $record['publication_date'];
    $license = $record['license'];
    $subject = addslashes($record['subject']);
    $image_url = $record['image_url'];
    $review = $record['review'];
    $language = $record['language'];
    $license_url = $record['license_url'];
    $publisher = addslashes($record['publisher']);
    $publisher_url = $record['publisher_url'];

    $query = $conn->prepare("SELECT id FROM oer_search WHERE title=:title AND author=:author AND source=:source limit 1");
    $query->bindParam(":title", $name);
    $query->bindParam(":author", $creator);
    $query->bindParam(":source", $origin);
    $query->execute();

    if ($query->rowCount() == 0) {
        $insert = $conn->prepare("INSERT INTO oer_search (type, title, author, link, source, description, base_url, isbn_number, e_isbn_number, publication_date, license, subject, image_url, review, language, license_url, publisher, publisher_url) VALUES ('$type', '$title', '$author', '$link', '$source', '$description', '$base_url', '$isbn_number', '$e_isbn_number', '$publication_date', '$license', '$subject', '$image_url', '$review', '$language', '$license_url', '$publisher', '$publisher_url')");
        $insert->execute();

        unset($_SESSION["records"][$record["id"]]);
    }

}

print "remaining data :". count($_SESSION["records"]);
?>

答案 1 :(得分:-1)

提示以加快批量进口:

  • 将您的SQL准备工作移出循环(只需执行一次)
  • 收集数据以将其插入到1000个批次中(例如,。通常可能更多)
  • 在插入过程中使用事务/禁用索引计算
  • 使用查找数组从现有数据中查找重复项(不要在数据库中查询导入的每一行)
  • 通常:避免在循环中使用SQL查询

希望有所帮助