2009-09-03 5 views
0

나는 4 단어 이상의 모든 단어를 사용하고 단어가 사용 된 횟수와 함께 데이터베이스에 저장하는 인덱서를 만드는 데 어려움을 겪었습니다.PHP에서 텍스트 파일 인덱싱

이 인덱서는 4,000 개의 txt 파일에서 실행해야합니다. 현재 약 12-15 분이 걸립니다. 과속을 빠르게한다는 제안이 있다면 궁금합니다. 다음과 같이

은 현재 내가 배열에 단어를 배치하고 있습니다 :
// ============================================================== 
// === Create an index of all the words in the document 
// ============================================================== 
function index(){ 
    $this->index = Array(); 
    $this->index_frequency = Array(); 

    $this->original_file = str_replace("\r", " ", $this->original_file); 
    $this->index = explode(" ", $this->original_file); 

    // Build new frequency array 
    foreach($this->index as $key=>$value){ 
     // remove everything except letters 
     $value = clean_string($value); 

     if($value == '' || strlen($value) < MIN_CHARS){ 
      continue; 
     } 

     if(array_key_exists($value, $this->index_frequency)){ 
      $this->index_frequency[$value] = $this->index_frequency[$value] + 1; 
     } else{ 
      $this->index_frequency[$value] = 1; 
     } 
    } 
    return $this->index_frequency; 
} 

나는 순간에 가장 큰 병목 현상이 데이터베이스에 단어를 저장하는 스크립트입니다 생각합니다. 그것은 ... 추가 한 후, 에세이 테이블에 문서를 추가하고 단어가 테이블에있는 경우 단어 나던 존재하는 경우 그럼 그냥 필드에 (단어의 주파수) essayid 추가 할 필요가

// ============================================================== 
// === Store the word frequencies in the db 
// ============================================================== 
private function store(){ 
    $index = $this->index(); 

    mysql_query("INSERT INTO essays (checksum, title, total_words) VALUES ('{$this->checksum}', '{$this->original_filename}', '{$this->get_total_words()}')") or die(mysql_error()); 

    $essay_id = mysql_insert_id(); 

    foreach($this->index_frequency as $key=>$value){ 

     $check_word = mysql_result(mysql_query("SELECT COUNT(word) FROM `index` WHERE word = '$key' LIMIT 1"), 0); 

     $eid_frequency = $essay_id . "(" . $value . ")"; 

     if($check_word == 0){ 
      $save = mysql_query("INSERT INTO `index` (word, essays) VALUES ('$key', '$eid_frequency')"); 
     } else { 
      $eid_frequency = "," . $eid_frequency; 
      $save = mysql_query("UPDATE `index` SET essays = CONCAT(essays, '$eid_frequency') WHERE word = '$key' LIMIT 1"); 
     } 
    } 
} 

답변

1

응용 프로그램을 프로파일 링하여 병목 현상이 어디인지 정확히 알 수 있습니다. 이렇게하면 향상시킬 수있는 것을 더 잘 이해할 수 있습니다.

DB 최적화 관련 : word 열에 색인이 있는지 확인한 다음 DB에 액세스하는 횟수를 줄이십시오. INSERT ... ON DUPLICATE KEY UPDATE ..., 아마도?

+0

감사합니다. n1313! 나는 DB에 질의 한 시간을 줄이기 위해 일했습니다. 당신의 도움을 주셔서 감사합니다. – Matt