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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:13:27+00:00 2026-06-12T21:13:27+00:00

Please note that the database structure probably can’t be changed without a lot of

  • 0

Please note that the database structure probably can’t be changed without a lot of work due to the amount of users and data on it.

The “friends” table is basically like this:

> show create table `friends`
CREATE TABLE `friends` (
    `id` int(10) unsigned NOT NULL auto_increment,
    `user1` int(10) unsigned NOT NULL,
    `user2` int(10) unsigned NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `user1_2` (`user1`,`user2`),
    KEY `user1` (`user1`),
    KEY `user2` (`user2`)
) ENGINE=InnoDB AUTO_INCREMENT=747908

To fetch a user’s friends, I have three options:

  1. Individually select user2 where user1 equals the user’s ID, and vice versa, then combine the results in PHP.
  2. SELECT IF(user1=@userid,user2,user1) FROM friends WHERE @userid IN (user1,user2)
  3. SELECT user2 FROM friends WHERE user1=@userid
    UNION SELECT user1 FROM friends WHERE user2=@userid

I tried timing options 2 and 3, and this is where I have a problem: The first time I run it, option 2 takes about 400ms whereas option 3 only takes less then 1ms. Every other time, however, opton 2 takes 0.6ms and option 2 takes 0.8ms.

What should I do? Which option is actually faster? The EXPLAIN queries return this:

id   select_type  table      type  possible_keys key     key_len ref   rows   Extra
1    SIMPLE       friends    index NULL          user1_2 8       NULL  386438 Using where; Using index

id   select_type  table      type  possible_keys key     key_len ref   rows   Extra
1    PRIMARY      friends    ref   user1,user1_2 user1_2 4       const 8      Using index
2    UNION        friends    ref   user2         user2   4       const 8
NULL UNION RESULT <union1,2> ALL   NULL          NULL    NULL    NULL  NULL
  • 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-12T21:13:28+00:00Added an answer on June 12, 2026 at 9:13 pm

    As usual when benchmarking, beware of caches.

    Measure your SELECT queries using SQL_NO_CACHE clause (see the SELECT syntax).

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

Sidebar

Related Questions

Can I write XSD schema for this? Please note that the category is repeated
How can I convert Persian date to Gregorian date using System.globalization.PersianCalendar? Please note that
Please note that I'm not asking how but why. And I don't know if
[Please note that this is a different question from the already answered How to
(Please note that I have seen a similar question on StackOverflow recently, however I
Please note that this is not homework and i did search before starting this
Please note that I did ask this on Super User and received absolutely no
Please note that this question is from 2008 and now is of only historic
When open a query in Microsoft project, the start date changes. Please note that,
I mean things like: FK1 -> 1FK2 -> 2PK Please, note that 1FK2 is

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.