Adhaata
Adhaata

Reputation: 82

PHP, MYSQL,JSOn Fast search functionality tips for social networking website

Hi I am developing social networking website and need a suggestion. I am using codeignitor framework, php as core language and jQuery as client side script.

Now I want to implement user search functionality to the website where a user can search for another users.

If I am using pagination with php it is bit slow and performance is not fast and even if the database will be huge then it will be more slow.

so what I have tried is, I made query of first 1000 people and encoded with json and showing first 10 results at a time by using this plugin http://www.stephenrhoades.com/?p=8

Is there anyone who can help my what all thing I need to follow here including technology, tips, query, mysql database etc.

Here is my current function in model

    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; 
    }


}

Thanks in advance

Upvotes: 0

Views: 1535

Answers (1)

swatkins
swatkins

Reputation: 13640

There are numerous factors that could lead to this process being slow. These are questions that we'd need to address:

  • What columns are you searching in your database?
  • Are those columns indexed?
  • What does your query look like? Are you only returning necessary columns?
  • How quickly do you want to show results? Immediately as user types, or after they click 'search'?
  • How do you return results?
  • How do you display results?

Generally, you can search VERY large datasets in a matter of milliseconds - so I doubt the database is the issue as long as you have things set up correctly.

OK, with all that being said...

If you do have a VERY large social network, I would recommend implementing something like Zend Search Lucene. It's a search index and can be implemented into CodeIgniter pretty easily:

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

You can add your users, their profile data, posts, comments, etc. into the index and search it with a powerful language processing query language, and return results very quickly and in any format you wish. This is very easy to turn into a "live" search with ajax and json.

Upvotes: 3

Related Questions