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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:53:58+00:00 2026-05-11T02:53:58+00:00

SELECT i.*, i.id IN ( SELECT id FROM w WHERE w.status=’active’) AS wish FROM

  • 0
SELECT i.*, i.id IN (   SELECT id   FROM w    WHERE w.status='active') AS wish  FROM i INNER JOIN r ON i.id=r.id WHERE r.member_id=1 && r.status='active'  ORDER BY wish DESC  LIMIT 0,50 

That’s a query that I’m trying to run. It doesn’t scale well, and I’m wondering if someone here can tell me where I could improve things. I don’t join w to r and i because I need to show rows from i that are unrepresented in w. I tried a left join, but it didn’t perform too well. This is better, but not ideal yet. All three tables are very large. All three are indexed on the fields I’m joining and selecting on.

Any comments, pointers, or constructive criticisms would be greatly appreciated.

EDIT Addition:

I should have put this in my original question. It’s the EXPLAIN as return from SQLYog.

id|select_type       |table|type          |possible_keys|key      |key_len|ref  |rows|Extra|   1 |PRIMARY           |r    |ref           |member_id,id |member_id|3      |const|3120|Using where; Using temporary; Using filesort   1 |PRIMARY           |i    |eq_ref        |id           |id       |8      |r.id |1   |   2 |DEPENDENT SUBQUERY|w    |index_subquery|id,status    |id       |8      |func |8   |Using where 

EDIT le dorfier – more comments …

I should mention that the key for w is (member_id, id). So each id can exist multiple times in w, and I only want to know if it exists.

  • 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. 2026-05-11T02:53:59+00:00Added an answer on May 11, 2026 at 2:53 am

    WHERE x IN () is identical to an INNER JOIN to a SELECT DISTINCT subquery, and in general, a join to a subquery will typically perform better if the optimizer doesn’t turn the IN into a JOIN – which it should:

    SELECT i.* FROM i INNER JOIN (     SELECT DISTINCT id     FROM w      WHERE w.status = 'active' ) AS wish      ON i.id = wish.id INNER JOIN r     ON i.id = r.id WHERE r.member_id = 1 && r.status = 'active'  ORDER BY wish.id DESC  LIMIT 0,50 

    Which, would probably be equivalent to this if you don’t need the DISTINCT:

    SELECT i.* FROM i INNER JOIN w      ON w.status = 'active'     AND i.id = wish.id INNER JOIN r     ON i.id = r.id     AND r.member_id = 1 && r.status = 'active'  ORDER BY i.id DESC  LIMIT 0,50 

    Please post your schema.

    If you are using wish as an existence flag, try:

    SELECT i.*, CASE WHEN w.id IS NOT NULL THEN 1 ELSE 0 END AS wish FROM i INNER JOIN r     ON i.id = r.id     AND r.member_id = 1 && r.status = 'active'  LEFT JOIN w      ON w.status = 'active'     AND i.id = w.id ORDER BY wish DESC  LIMIT 0,50 

    You can use the same technique with a LEFT JOIN to a SELECT DISTINCT subquery. I assume you aren’t specifying the w.member_id because you want to know if any members have this? In this case, definitely use the SELECT DISTINCT. You should have an index with id as the first column on w as well in order for that to perform:

    SELECT i.*, CASE WHEN w.id IS NOT NULL THEN 1 ELSE 0 END AS wish FROM i INNER JOIN r     ON i.id = r.id     AND r.member_id = 1 && r.status = 'active'  LEFT JOIN (     SELECT DISTINCT w.id     FROM w      WHERE w.status = 'active' ) AS w     ON i.id = w.id ORDER BY wish DESC  LIMIT 0,50 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In my experience, if there are elements of a string… May 12, 2026 at 1:18 am
  • Editorial Team
    Editorial Team added an answer I'm a C# programmer, so I'm sorry if this suggestion… May 12, 2026 at 1:18 am
  • Editorial Team
    Editorial Team added an answer Preferences -> General -> Editors -> Text Editors -> Annotations… May 12, 2026 at 1:18 am

Related Questions

SELECT i.*, i.id IN ( SELECT id FROM w WHERE w.status='active') AS wish FROM
Let's say you have the following two tables: X Table X_ID Y_ID_F X_Value 1
I'm trying to further tune this query. The query returns the status for three
I have a MDB (Message driven bean) that receives messages with String which represent
Excuse the long question! We have two database tables, e.g. Car and Wheel. They

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.