2009-07-03 53 views
0

我要改寫我的代碼,以便將iffecient當涉及到內存和執行時間..創建時間和內存使用效率,PHP腳本

什麼腳本做的是創造一個MySQL的轉儲,其中的數據是GET到巨大的數據表中並插入到另一個數據庫中。

這裏處理的數據大約是17 MB的表數據,並且大約消耗了62 MB的內存。任何有關如何降低內存使用量的建議將來會越來越大?

<?php 
ini_set("max_execution_time", "28800"); 
error_reporting(E_ALL); 

include(dirname(__FILE__)."/includes/prepend.php"); 

$table = "source_table"; 
$target = "destination_table"; 

echo 'Initial: ' . number_format((memory_get_usage()/ 1024)/1024 , 0, '.', ',') . " MB <br>"; 

$db = new DB_cms(); 
$db->beginTransaction(); 

$return.= 'DELETE FROM '.$target.'; '. "\n\n"; 

if($db->query('SELECT * FROM '.$table)){ 
    $i=0; 
    $itemList = array(); 

    while($db->next_record()){ 

     $itemList[$i]["guid"] = $db->f("guid"); 
     $itemList[$i]["title"] = $db->f("title"); 
     $itemList[$i]["description"] = $db->f("description"); 
     $itemList[$i]["copyright"] = $db->f("copyright"); 
     $itemList[$i]["mediaType"] = $db->f("wapMediaType"); 
     $itemList[$i]["price"] = $db->f("displayPrice"); 
     $itemList[$i]["category"] = $db->f("category"); 
     $itemList[$i]["thumbnail"] = $db->f("thumbnail"); 

     //begin json data 
     $json = new Services_JSON(); 
     $keywords_arr = $json->decode($db->f("keywords")); 

     foreach($keywords_arr as $key => $value){ 
      $itemList[$i][$key] = $value; 
     } 

     $credit_arr = $json->decode($db->f("credit")); 
     foreach($credit_arr as $c => $credit){ 
      $itemList[$i][str_replace(' ','',$c)] = $credit; 
     } 
     $i++; 
    } 

    $toInsert = array(); 

    foreach($itemList as $items => $item){ 
     $guid = mysql_real_escape_string($item["guid"]); 
     $title = mysql_real_escape_string($item["title"]); 
     $description = mysql_real_escape_string(ereg_replace('"', "",$item["description"])); 
     $copyright = mysql_real_escape_string($item["copyright"]); 
     $mediaType = mysql_real_escape_string($item["mediaType"]); 
     $price = mysql_real_escape_string($item["price"]); 
     $keywords = mysql_real_escape_string(ereg_replace('"', "",$item["keywords"])); 
     $category = mysql_real_escape_string(ereg_replace('"', "",$item["category"])); 
     $thumbnail = mysql_real_escape_string($item["thumbnail"]); 
     $date = date("Y-m-d H:i:s"); 
     //json decoded data 
     $artist = mysql_real_escape_string($item["artist"]); 
     $label = mysql_real_escape_string($item["label"]); 
     $genre = mysql_real_escape_string($item["genre"]); 
     $media_format = mysql_real_escape_string($item["mediaformat"]); 
     $country = mysql_real_escape_string($item["country"]); 
     $album_title = mysql_real_escape_string(ereg_replace('"', "",$item["albumtitle"])); 

     $toInsert[] = "('0', $guid, 'NULL', '".$mediaType."', '".$category."', '".$keywords."', '".$title."', '".$artist."', '".$album_title."', '".$genre."', '".$label."', '".$media_format."', '".$country."', '".$description."', '".$thumbnail."', '".$price."', '".$copyright."', '".$date."', '0', '".$date."', '0', 'active')"; 
    } 

    $sqlStart = "INSERT INTO `".$target ."` (`SortVar`, `Guid`, `Space`, `MediaType`, `Category`, `Keywords`, `Title`, `Artist`, `Album`, `Genre`, `Label`, `Mediaformat`, `Country`, `Description`, `Thumbnail`, `Price`, `Copyright`, `DateCreated`, `CreatedBy`, `DateModified`, `ModifiedBy`, `Status`) VALUES"; 

    foreach (array_chunk($toInsert, 100) as $insertSet) { 
     $return.= $sqlStart . implode(', ', $insertSet); 
     $return.="; \n"; 
    } 

    //save file 
    $handle = fopen(ABSOLUTE_DUMP_PATH.'file'.'.sql','w+'); 
    if(fwrite($handle,$return)){ 
     fclose($handle); 
     $ret = true; 
    } else { 
     $ret = false; 
    } 

    $usage = memory_get_usage(); 
    $total_usage = ($usage/1024)/1024; 
    echo 'Peak: ' . number_format($total_usage, 0, '.', ',') . " MB<br>"; 
    echo 'End: ' . number_format($total_usage, 0, '.', ',') . " MB<br>"; 
} 
?> 

回答

1

如果你在1個數據庫(這是什麼會顯示你當前的代碼來執行)兩個表之間做這個,做這一切在一個SQL語句。如果你不需要,千萬不要把數據放到PHP中。你會想使用MySQL的insert...select syntax。查詢應看不全然不像這樣:

而不是讀書的一切到一個數組
INSERT INTO `target_table` (`SortVar`, `Guid`, `Space`, `MediaType`, `Category`, `Keywords`, `Title`, `Artist`, `Album`, `Genre`, `Label`, `Mediaformat`, `Country`, `Description`, `Thumbnail`, `Price`, `Copyright`, `DateCreated`, `CreatedBy`, `DateModified`, `ModifiedBy`, `Status`) 
    SELECT * 
    FROM `source_table`; 
+0

如果你看看源代碼,第一部分有關鍵字位(選擇日期)。它看起來好像兩個表之間存在1對1關聯 - 因此,直接的sql副本不僅僅插入到select中。話雖如此,你仍然可以通過在sql中使用字符串拆分來解決問題 - 但我不會依賴它來查看代碼。 – Fake51 2009-07-03 15:25:44

1

,然後將它寫出來的目標文件,嘗試在你寫出來的東西遞增的方式重組程序。