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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:15:23+00:00 2026-05-28T01:15:23+00:00

I have two tables, users and followers : Table users : id INT, PRIMARY_KEY

  • 0

I have two tables, users and followers:

Table users:

id          INT, PRIMARY_KEY
name        VARCHAR
joined      INT

This table is indexed on id and joined.

Table ‘followers’:

user        INT
follows     INT 

This table is indexed on users and follows.

This query finds the names of all users followed by a particular user who joined after a particular time. The results need to be
in reverse chronological order.

SELECT u.name 
FROM users u, followers f
WHERE f.user = X
AND f.follows = u.id
AND u.joined > 1234
ORDER BY u.joined DESC

Now, when user X has a large number of followers, EXPLAIN gives the following:

id      key             extra
-----------------------------------
u       joined          Using where
f       follows         Using index

So far so good. (The ‘using where’ is due to some other clauses I’ve removed for brevity).

But, when user X has a small number of followers, this happens:

id      key             extra
-----------------------------------
f       follows         Using temporary, using filesort
u       joined          Using where

If I omit the ORDER BY, I get:

id      key             extra
-----------------------------------
f       follows         
u       joined          Using where

The MySQL optimiser seems to be inspecting the number of rows it has to deal with, and if its small, doing the
followers table first. It seems to ignore the ORDER BY in its optimisation steps, resulting in a slower query due
to the temporary table.

So (finally), my question is: is it possible to force the order in which MySQL performs table searches, and in the likely event this isn’t possible, is there another way to get rid of using temporary?

  • 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-28T01:15:23+00:00Added an answer on May 28, 2026 at 1:15 am

    MySQL DOES offer a clause “STRAIGHT_JOIN” which tells it to do the join between tables in the order you’ve provided. Since you are looking for a single specific “Follower User” put the follower table up front and join from that… Try something like

    SELECT STRAIGHT_JOIN
          u.name
      from
         followers f
            join Users u
               on f.follows = u.id
              and u.joined > 1234
      where
         f.user = X
      order by 
         u.joined DESC
    

    This should FORCE Starting with the “Followers” table, specific for user ID = X, then join out to the users table as SECONDARY based on the rows returned from f.user = X. Make sure your Followers table has an index where “user” is in the first position (in case your index on both columns was (follows, user), it SHOULD be (user, follows). The smallest granularity on your query basis is the one person inquiring about… that comes in FIRST.

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

Sidebar

Related Questions

I have Two tables. 1.Users table (Username , Name) 2.Picture table( ID , Username
I have two tables as follows: Table user_prefs: user_prefs_id int(11) PK user_id int(11) item
I have two tables in my MySQL database, users and tweets, as follows: TABLE
If I have two tables - Logins and Users, as follows: Logins LoginIdNo UserIdNo
using php and mysql I have two tables, a users table and a profiles
I have a simple database with two tables. Users and Configurations. A user has
I have two tables, users and groups . A user can belong to many
I have two tables: Users: ID, first_name, last_name Networks: user_id, friend_id, status I want
hope someone can help. I have two tables: Users -UserID -UserName UsersType -UserTypeID -UserID
We have two tables - Tasks and TasksUsers (users assigned to task). Task has

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.