我有一個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