使用php-mysql在超大多维数组中搜索


Searching in mega size multidimentional array using php mysql

我有一个包含数百万条记录的表,其中包含一些多维数组。我需要在每一行中搜索才能找到匹配项,关键匹配项在另一个有数千条记录的表中。

例如,每个人可能有不同数量的教育;(分号)。我的目标是在搜索英国学院的表格时找到那些在英国受过教育的人。

Name        | Educations
------------+--------------------------------------------------------------------------
John Smith  | Oxford University, BSc Business, UK ; London University, MSc Art, UK ; Boston University, PhD in AI, USA
Sara Jones  | Ealing college, Access to IT, UK ; Paris University, BSc Maths, France

目前,我很喜欢preg_match,但将大量机构列表放入带有|(管道)分隔符的数组中似乎不起作用。不过,将数组限制在1000以下似乎是可行的。我不确定这是否与数组大小有关?

对于如何使用preg_match或您所知道的任何其他搜索功能优化搜索大型数组,我将不胜感激。

这是我的部分代码:

// query a list of institute  
$query = "SELECT institute_name FROM $table_institute limit 1000"; 
$result = mysql_query($query) or die(mysql_error());
// create an array of institute
while($row = mysql_fetch_array($result)) { 
   $institute = trim($row['institute_name']); 
   $institute_array = $institute_array  . "|" . $institute; 
 }
 $institute_array = "/'b(" . $institute_array . ")'b/i";
// create a multidimensional array of educations  
$educations = unserialize ($row['educations']);
$count_education = count($educations);
$educations= implode (" ; " , $educations);
$education_list = (explode (" ; ", $educations));
$education_array = array();
// check and compare both array
if ($educations == NULL ) 
 $code_institute = 'Not Listed';
else {
for($i=0; $i<$count_education; $i++) {
       list ($org, $degree, $major, $start_date, $end_date) = explode(' ,, ', $education_list[$i]);
       $education_array[$i] = array(
   'org' => trim($org),
       'degree' => trim($degree),
   'location' => trim($location)
   );
   if (preg_match ($institute_array, $education_list[$i], $matched)) {
           $code_institute = 'Matched';
       $match_no_institute = $match_no_institute + 1;
    }
   else 
    $code_institute = 'Not Matched';
 print_r ("<br> Education : (" . ($i+1) . ") Matching Time: " . $match_no_institute . " Code: " . $code_institute . "   " . $matched[0]);
    }
} 

我可能错了,但除非你在搜索索引列,否则你会过得很糟糕。我从未听说过有人试图在LONGTEXT或varchar字段中搜索已序列化或以某种数组形式放入的"数组"。

一般来说,您应该使用联接表和关联来完成您想要的任务。