PHP,MYSQL,JSOn社交网站的快速搜索功能提示

时间:2011-10-28 14:36:32

标签: php jquery mysql json

您好我正在开发社交网站,需要一个建议。我使用codeignitor框架,php作为核心语言,jQuery作为客户端脚本。

现在,我想在用户可以搜索其他用户的网站上实现用户搜索功能。

如果我使用php进行分页,它有点慢,性能不快,即使数据库很大,也会更慢。

所以我试过的是,我查询了前1000个人并使用json编码并使用此插件一次显示前10个结果http://www.stephenrhoades.com/?p=8

是否有人可以帮助我完成我需要遵循的所有内容,包括技术,提示,查询,mysql数据库等。

这是我当前在模型中的功能

    function search_user($cntry,$male,$female,$age_min,$age_max){

               $this->db->select('user_profiles.user_id,user_profiles.first_name,
               user_profiles.birthday,user_profiles.gender,user_profiles.picture,
               user_profiles.last_active,location.city,location.country');

     $this->db->from('user_profiles');

     $this->db->join('location', 'user_profiles.user_id = location.user_id');

     $this->db->limit(800);


    // Search condition :::: Country 

    if($cntry){
    $this->db->like('location.country',$cntry, 'both');
    }

    // Search condition :::: Male or Female 

     if($male==1 && $female==0) {  $g='Male'; $this->db->like('user_profiles.gender',$g, 'both');  }

     if($male==0 && $female==1) {  $g='Female'; $this->db->like('user_profiles.gender',$g, 'both');  }

     // Search condition :::: Age range
     if($age_min && !$age_max){

        $this->db->where('(YEAR(NOW())-YEAR(user_profiles.birthday)) >= ', $age_min);
     }


     if($age_min && $age_max){

        $this->db->where('(YEAR(NOW())-YEAR(user_profiles.birthday)) >= ', $age_min); 
        $this->db->where('(YEAR(NOW())-YEAR(user_profiles.birthday)) <= ', $age_max); 
     }


     $this->db->order_by("user_profiles.last_active", "desc"); 
     $query = $this->db->get();


     $data=array();
     foreach ($query->result() as $row){     
     $row->last_active=$this->online_status($row->last_active);
     $row->birthday=$this->birthday($row->birthday);
     $data[]=$row;
     }


    if ($query->num_rows() > 0)
    {
    return $data;
    }
    else
    {
    $data = array(
                'result' =>'404'
                 );
    return $data; 
    }


}

提前致谢

1 个答案:

答案 0 :(得分:3)

有许多因素可能导致此过程缓慢。这些是我们需要解决的问题:

  • 您在数据库中搜索了哪些列?
  • 这些列是否已编入索引?
  • 您的查询是什么样的?你只返回必要的栏目吗?
  • 您想以多快的速度展示结果?用户输入时,或点击“搜索”后立即?
  • 你如何归还结果?
  • 如何显示结果?

通常,您可以在几毫秒内搜索非常大的数据集 - 因此,只要您正确设置了内容,我就怀疑数据库是否存在问题。

好的,尽管如此......

如果您拥有非常庞大的社交网络,我建议您实施Zend Search Lucene之类的内容。它是一个搜索索引,可以很容易地实现到CodeIgniter中:

http://www.cmjackson.net/2009/02/17/how-to-use-zend_search_lucene-with-the-php-framework-codeigniter/

您可以将您的用户,他们的个人资料数据,帖子,评论等添加到索引中,并使用强大的语言处理查询语言进行搜索,并以您希望的任何格式快速返回结果。使用ajax和json很容易变成“实时”搜索。

相关问题