Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7278793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:55:24+00:00 2026-05-28T22:55:24+00:00

I am trying to find a better algorithm here. In my problem, a user

  • 0

I am trying to find a better algorithm here. In my problem, a user has 200 friends in his list. My database looks like this:

    username          friends
     someid           2,3,4,6,7

What I did is when a new friend is added. The persons unique id is added to his list so here 2,3,4,6,7 are the ids of the users friends.

And the normal users info database looks like this

   friends info 
    id     profile image    name               info
    2         his image    his name       presently

My algorithm is get the id 2 and fetch all the data from the table putting id = 2 then similarly 3,4,6 and more but if user has 200 friends, I was thinking it may cause a serious issue on server it keeps server busy because it should fetch data for 2 and again for 3.

So I was thinking for a better algorithm because if user has more friends the server have to perform MySQL (select * ) statement for 200 times if they are 200 friends this may cause a serious server issue.

I want to know if there is a simple algorithm to do or may be should I change the way how data is saved on database?

Sometimes I think to fetch and store all the data in a php variable and then save it to JavaScript variable and then using JavaScript perform the operations, because JavaScript is client side and removes server issue.

Update

Is the size of table is limited? I mean can a table consist of infinite rows?

friends_table
  user id       friend id
    2                3
    2                4
users_info table
  user id           image           small info
     3            some image             // 
     4              //                   //
     2                 //                //

As people suggested I would change the structure but how do I link friends_tabe to users info table?

   $result = mysql(select * from friends_table where id = userdid);
   $row = mysql_fetch_array($result);

above query gets all the ids of friends

Now I have to get the friends info using their id so I have to perform this statement

   for($i=0 ;$i<count($row);$i++)
   mysql(select * from users_info where id=$row[$i])

I think I should perform this statement so many times. In other words, do I have to loop this statement?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:55:25+00:00Added an answer on May 28, 2026 at 10:55 pm

    Instead of storing ids of all friends in one column create a many-to-many relationship with table that maps users to their friends.

    user_friends table:
    
    | user_id  | friend_id |
    |==========|===========|
    |  someid  |     2     |
    |  someid  |     3     |
    |  someid  |     4     |
    |  someid  |     6     |
    |  someid  |     7     |
    

    Then you can select all friends info in one query

    SELECT `friends_info` * from `friends_info` 
    LEFT JOIN user_friends ON user_friends.friend_id = friends_info.id
    WHERE user_friends.user_id = someid
    

    You can use the query like that:

    $result = mysql_query("<the query goes here>"); // Execute the query
    
    while ($row = mysql_fetch_array($result, MYSQL_NUM)) { // For each row..
       (...)    // ...do sth with the data
    }
    
    mysql_free_result($result);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find a better way to integrate IRB with my normal
any better way to write this ? $(this).parent().parent().find( dd ul).toggle(); update.. I am trying
Trying to find an example that has css rollover using sprites & sliding door
I am trying to find a better interface, or better method to draw stuff
I'm trying to find a better and faster way to insert pretty massive amount
I'm trying to better understand why one of our database update scripts failed to
I am trying to find a better approach for storing people's name in the
I'm trying to find a better way to use a div table with ASP.NET
I'm trying to find a better way to do this as it could take
I am trying to find if there is better way to check if the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.