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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:32:10+00:00 2026-06-16T21:32:10+00:00

I have a fairly complicated operation that I’m trying to perform with just one

  • 0

I have a fairly complicated operation that I’m trying to perform with just one SQL query but I’m not sure if this would be more or less optimal than breaking it up into n queries. Basically, I have a table called “Users” full of user ids and their associated fb_ids (id is the pk and fb_id can be null).

+-----------------+
| id | .. | fb_id |
|====|====|=======|
| 0  | .. | 12345 |
| 1  | .. | 31415 |
| .. | .. |  ..   |
+-----------------+

I also have another table called “Friends” that represents a friend relationship between two users. This uses their ids (not their fb_ids) and should be a two-way relationship.

+----------------+
| id | friend_id |
|====|===========|
| 0  |     1     |
| 1  |     0     |
| .. |    ..     |
+----------------+
// user 0 and user 1 are friends

So here’s the problem:
We are given a particular user’s id (“my_id”) and an array of that user’s Facebook friends (an array of fb_ids called fb_array). We want to update the Friends table so that it honors a Facebook friendship as a valid friendship among our users. It’s important to note that not all of their Facebook friends will have an account in our database, so those friends should be ignored. This query will be called every time the user logs in so it can update our data if they’ve added any new friends on Facebook. Here’s the query I wrote:

INSERT INTO Friends (id, friend_id)
SELECT "my_id", id FROM Users WHERE id IN
  (SELECT id FROM Users WHERE fb_id IN fb_array)
AND id NOT IN
  (SELECT friend_id FROM Friends WHERE id = "my_id")

The point of the first IN clause is to get the subset of all Users who are also your Facebook friends, and this is the main part I’m worried about. Because the fb_ids are given as an array, I have to parse all of the ids into one giant string separated by commas which makes up “fb_array.” I’m worried about the efficiency of having such a huge string for that IN clause (a user may have hundreds or thousands of friends on Facebook). Can you think of any better way to write a query like this?

It’s also worth noting that this query doesn’t maintain the dual nature of a friend relationship, but that’s not what I’m worried about (extending it for this would be trivial).

  • 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-16T21:32:11+00:00Added an answer on June 16, 2026 at 9:32 pm

    If I am not mistaken, your query can be simplified, if you have a UNIQUE constraint on the combination (id, friend_id), to:

    INSERT IGNORE INTO Friends 
      (id, friend_id)
    SELECT "my_id", id 
    FROM Users 
    WHERE fb_id IN fb_array ;
    

    You should have index on User (fb_id, id) and test for efficiency. if the number of the itmes in the array is too big (more than a few thousands), you may have to split the array and run the query more than once. Profile with your data and settings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL query that's fairly complicated and involves multiple subqueries and unions.
Just out of curiosity, for applications that have a fairly complicated module tree, would
I have a fairly complicated SQL query with a nested subquery. When I try
I have a site that has a fairly complicated footer, see http://www.roadsafetyforchildren.co.uk/ , not
I have codes that allow me to perform a fairly complicated task (for me
I have a fairly complicated linq query that returns an item and a string[]
I have a fairly complicated join query that I use with my database. Upon
I have a fairly complicated installer that I'm writing in Wix that has a
I have a fairly complicated lookup i'm trying to do in Rails and I'm
this is my first posted question. I have a fairly complicated OQL query which

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.