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

  • SEARCH
  • Home
  • 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 7403581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:07:24+00:00 2026-05-29T05:07:24+00:00

I have two tables: users and user_friends with a 1:1 reflective join on the

  • 0

I have two tables: users and user_friends with a 1:1 reflective join on the users table. (i.e. user_friends records are like [user_id, friend_id], [friend_id, user_id]).

Let us say I have a user called “self”. What I am trying to do is find all users who have a mutual friend in common with “self” and who meet some other simple criteria in the users table. This is the best I came up with so far, which runs in about 18 seconds.

Can this query be improved?

SELECT 
  DISTINCT(friend_id) 
FROM 
  user_friends, users 
WHERE 
  users.id != #{self.id}
AND (
  signed_up IS NOT NULL 
  OR 
  user_id IN (
    SELECT 
      friend_id 
    FROM 
      user_friends 
    WHERE 
      user_id = #{self.id}
  )
) 
AND 
  users.id = friend_id
AND 
  relationship_status = 'Single'
AND 
  current_location_id IS NOT NULL
;

In addition, here is the EXPLAIN PLAN for this query. Is the “temporary” table causing the problem?

*************************** 1. row ***************************
           id: 1
  select_type: PRIMARY
        table: user_friends
         type: index
possible_keys: NULL
          key: PRIMARY
      key_len: 16
          ref: NULL
         rows: 1316316
        Extra: Using where; Using index; Using temporary
*************************** 2. row ***************************
           id: 1
  select_type: PRIMARY
        table: users
         type: eq_ref
possible_keys: PRIMARY,fk_users_current_location_id
          key: PRIMARY
      key_len: 8
          ref: yoke_int.user_friends.friend_id
         rows: 1
        Extra: Using where; Distinct
*************************** 3. row ***************************
           id: 2
  select_type: DEPENDENT SUBQUERY
        table: user_friends
         type: eq_ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 16
          ref: const,func
         rows: 1
        Extra: Using index
  • 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-29T05:07:26+00:00Added an answer on May 29, 2026 at 5:07 am

    There are a couple ways to improve your query.

    1. Use a JOIN rather than filterfrom two tables.
    2. Rearrange your ANDs so that the simpler ones short circuit faster, stopping the subquery from being ran as often.

    Code:

    SELECT 
      DISTINCT(friend_id)
    FROM 
      user_friends
      JOIN users on users.id = friend_id
    WHERE 
      users.id != #{self.id}
    AND 
      relationship_status = 'Single'
    AND 
      current_location_id IS NOT NULL
    AND (
      signed_up IS NOT NULL 
      OR 
      user_id IN (
        SELECT 
          friend_id 
        FROM 
          user_friends 
        WHERE 
          user_id = #{self.id}
      )
    ) 
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables users table: id|name user_relationships id | user_id | friend_id and
Say, I have two tables like these: Table group Table user +----+-----------+ +----+----------+------+----------+ |
I have two tables: Users: ID, first_name, last_name Networks: user_id, friend_id, status I want
Let's say you have two tables, Users and UserRoles. Here's how the two tables
I have two tables. USER user_id password FRIEND_LIST user_id friend_id If user 1 is
I have a simple database with two tables. Users and Configurations. A user has
I have two tables like of this structure: content (content_id, content_type, user_id, time, comment_count)
I have Two tables. 1.Users table (Username , Name) 2.Picture table( ID , Username
If I have two tables 'users' and 'orders' with the same user_id KEY, and
I have two tables users - id - name - email users_groups - user_id

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.