我有一个包含文件列表(大约 6000 个文件)的数据库。所有这些文件都有与之相关的某些附加详细信息(例如项目编号、部门、客户、评论、学科)。
尽管代码和搜索有效,但速度很慢。包含两个词的简单搜索大约需要一分钟才能完成。
我的代码如下。我想知道的是,我可以做些什么来简化和优化我的搜索功能?
public function search() {
$Terms = explode(' ',$this->request->data['KmFiles']['search']);
$possible = 0;
$Matches = array();
foreach($Terms as $Term) {
$Files = $this->KmFile->find('list',
array(
'conditions' => array(
'file_name LIKE' => '%' . $Term . '%'
),
'fields' => array('id')
)
);
$possible++;
$Clients = $this->KmClient->find('list',
array(
'conditions' => array(
'clients LIKE' => '%' . $Term . '%'
),
'fields' => array('km_file_id')
)
);
$possible++;
$Disciplines = $this->KmDiscipline->find('list',
array(
'conditions' => array(
'disciplines LIKE' => '%' . $Term . '%'
),
'fields' => array('km_file_id')
)
);
$possible++;
$Projects = $this->KmProject->find('list',
array(
'conditions' => array(
'projects LIKE' => '%' . $Term . '%'
),
'fields' => array('km_file_id')
)
);
$possible++;
$Sectors = $this->KmSector->find('list',
array(
'conditions' => array(
'sectors LIKE' => '%' . $Term . '%'
),
'fields' => array('km_file_id')
)
);
$possible++;
$Comments = $this->KmComment->find('list',
array(
'conditions' => array(
'comments LIKE' => '%' . $Term . '%'
),
'fields' => array('km_file_id')
)
);
$possible++;
$Matches = array_merge($Matches,$Files,$Clients,$Disciplines,$Projects,$Sectors,$Comments);
}
if(count($Matches) > 0) {
$NumberOfMatches = array_count_values($Matches);
$Matches = array_unique($Matches);
$k=0;
foreach($Matches as $Match) {
$Result = $this->KmFile->find('all',
array(
'conditions' => array(
'id' => $Match
)
)
);
$Results[$k] = $Result[0];
$Results[$k]['Relevance'] = round(($NumberOfMatches[$Match] / $possible) * 100,2);
$relevance[] = $Results[$k]['Relevance'];
$k++;
}
array_multisort($relevance,SORT_DESC,$Results);
$Stats['Count'] = count($Results);
$Stats['Terms'] = $this->request->data['KmFiles']['search'];
$this->set(compact('Results','Stats'));
} else {
$Stats['Count'] = 0;
$Stats['Terms'] = $this->request->data['KmFiles']['search'];
$this->set(compact('Stats'));
}
}
我知道这是一段很长的代码,但我对 CakePHP 还很陌生,所以不知道如何改进它。
任何帮助将不胜感激。