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 7931577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:45:37+00:00 2026-06-03T20:45:37+00:00

I have a friend table on a social type network I am building with

  • 0

I have a friend table on a social type network I am building with this structure:

uid binary(16) 
memberid int 
friendid int 
datefriended datetime 
isactive bool

This way I have relationships and other data stored in one table only. But say I need to get all my friends, whether I am the friender( memberid ) or I am the friendee( friendid ) then I need to get the user names from the members table. The query is this:

SELECT m.name FROM members m
INNER JOIN friends f on m.memberid = f.friendid
WHERE f.memberid = 1

UNION ALL

SELECT m.name from members m
INNER JOIN friends f on m.memberid = f.memberid
WHERE f.friendid = 1

This to me seems like it could be a resource intensive call to the db if you have lots of users and friends pulling this data. I know there is the option to have a pair of entries for each friend relationship but I’m trying to do this in once query.
Do you have any thoughts on how to improve the query or table structures to increase performance. Or maybe it won’t be as bad as I am foreseeing?

  • 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-06-03T20:45:39+00:00Added an answer on June 3, 2026 at 8:45 pm

    In addition to my comment above advising you to create indexes on friends.memberid, friends.friendid and members.memberid, you don’t need the UNION at all:

    SELECT m.name
    FROM members AS m JOIN friends AS f
      ON (m.memberid, 1) IN ((f.friendid, f.memberid), (f.memberid, f.friendid))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pretty large social network type site I have working on for
I'm building a social networking website. I have a table of Users. Each user
I am working on a social network type project, as most social networks have,
I have a social network The users table is around 60,000 rows The friends
I'm creating a social network in Rails and I have a model like this:
i friend i have a database table amount_id, year, amount, and data like this
I have this table: and I want to display all friend names are without
I have this table CREATE TABLE [dbo].[friend_blocked_list]( [subdomain] [varchar](50) NOT NULL, [un] [nvarchar](50) NOT
I have a table like this. (BIGINT) (BIGINT) PLAYER_ID FRIEND_ID ---------------------- 1 2 2
I have a table called 'friends', this contains the columns person1 and person2 .

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.