I hope you can help me. I searched a lot, but unfortunately didn't find anything. What's the problem? I've got big CSV files with 1 column, which contains e-mail addresses. There are about 50000 lines in single file. I'm creating administration panel, which allows to import these files to the server, using HTML form and PHP. Importing CSV to MySQL database through PHP is simple, but I need something more - check for every e-mail does it exists, and if yes - skip it. What's the problem? Table has about million+ records, checking one e-mail lasts +/- 3 seconds. 50000 records multiplied by 3... it's gonna take min. 44 hours! PHP script stops responding after less than 10 minutes... So it's impossible do it this way:
function doesExist($email) {
$sql = "SELECT count(*) as counter FROM mailing_subscribers WHERE subscriber_email LIKE :subscriber_email";
$sth = $this->db->prepare($sql);
$sth->execute(array(':subscriber_email' => $email));
$row = $sth->fetch();
$counter = $row->counter;
if ($counter > 0) {
return true;
} else {
return false;
}
}
function importCSV($file,$group) {
$fp = fopen($file['tmp_name'], "r");
$importsCounter = 0;
while($csv_line = fgetcsv($fp)) {
for ($i = 0, $j = count($csv_line); $i < $j; $i++) {
if (!$this->doesExist($csv_line[$i])) {
$sql = "INSERT INTO mailing_subscribers(subscriber_email,subscriber_group) VALUES('".$csv_line[$i]."','".$group."')";
$sth = $this->db->prepare($sql);
$sth->execute();
$importsCounter++;
}
}
}
$_SESSION["feedback_positive"][] = FEEDBACK_FILE_IMPORT_SUCCESSFUL . " Utworzonych wpisów: " . $importsCounter;
}
$file is a $_FILE array.
Is there any other and faster method to do it?
INSERT IGNORE. A proper index on the email column could help also.WHERE subscriber_email LIKE :subscriber_email, having WHERE and LIKE? I usually see someting likeWHERE subscriber_email = '$email'as an example.LIKEbecause I'm not sure does=is case insensitive.subscriber_emailcolumn asunique?INSERT IGNOREit should be enough, yes. You should also use bulk inserts instead of one SQL statement per entry.