我有一个800MB的文本文件,其中有18,990,870行(每行是一条记录),我需要挑出某些记录,并且如果有匹配,则将它们写入数据库。如何加快处理一个巨大的文本文件?
它走的时代,通过他们的工作,所以我想知道是否有一种方法做任何更快?
我的PHP是一次读取一行如下:
$fp2 = fopen('download/pricing20100714/application_price','r');
if (!$fp2) {echo 'ERROR: Unable to open file.'; exit;}
while (!feof($fp2)) {
$line = stream_get_line($fp2,128,$eoldelimiter); //use 2048 if very long lines
if ($line[0] === '#') continue; //Skip lines that start with #
$field = explode ($delimiter, $line);
list($export_date, $application_id, $retail_price, $currency_code, $storefront_id) = explode($delimiter, $line);
if ($currency_code == 'USD' and $storefront_id == '143441'){
// does application_id exist?
$application_id = mysql_real_escape_string($application_id);
$query = "SELECT * FROM jos_mt_links WHERE link_id='$application_id';";
$res = mysql_query($query);
if (mysql_num_rows($res) > 0) {
echo $application_id . "application id has price of " . $retail_price . "with currency of " . $currency_code. "\n";
} // end if exists in SQL
} else
{
// no, application_id doesn't exist
} // end check for currency and storefront
} // end while statement
fclose($fp2);
谁是jos_mt_links表? 只需检索一次所有条目并在不访问数据库的情况下执行检查。 – Andreas 2010-07-22 14:24:23