INSERT Query via CSV taking ages
Posted: Thu Apr 10, 2014 5:39 am
I am trying to upload a 350,000 line CSV file.
Each row is checking that it doesn't exist already (to avoid duplicates), and then remove the " at the end of the run.
I thought it would take maybe a minute or two. It's taking a lot longer. Is my script badly done, causing it to be slow?
Each row is checking that it doesn't exist already (to avoid duplicates), and then remove the " at the end of the run.
I thought it would take maybe a minute or two. It's taking a lot longer. Is my script badly done, causing it to be slow?
Code: Select all
if ($update == "addcsv")
{
if ($_FILES[csv][size] > 0)
{
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
$newdata = mysql_real_escape_string($data[0]);
$result = mysql_query ("SELECT email FROM subscribed_upload WHERE email = '$newdata'");
$num_result = mysql_num_rows($result);
if ($num_result == 0)
{
mysql_query("INSERT INTO subscribed_upload (email) VALUES
(
'$newdata'
)
")or die(mysql_error());
}
$count = $count + 1;
}
} while ($data = fgetcsv($handle,1000,",","'"));
}
mysql_query ("UPDATE subscribed_upload SET email = REPLACE(email,'\"','')");
}