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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:45:21+00:00 2026-06-07T18:45:21+00:00

I have the following query. SELECT MIN(col1) FROM table1 WHERE id1 IN (SELECT id2

  • 0

I have the following query.

SELECT MIN(col1) FROM table1 WHERE id1 IN (SELECT id2 FROM table2)

This produces output that takes ~5 seconds.

However, if I change the query to run as two separate queries, such that:

SELECT id2 FROM table2
SELECT MIN(col1) FROM table1 WHERE id1 IN (_results_from_first_query_)

Then this produces output that takes ~0.05 seconds.

An EXPLAIN shows the following (Apologies for the terrible indenting):

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
1   PRIMARY     table1 ALL  NULL    NULL    NULL    NULL    1107294     Using where
2   DEPENDENT SUBQUERY  table2 eq_ref   PRIMARY     PRIMARY     16  const,func  1   Using index

vs

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
1   SIMPLE  table1  range   PRIMARY,id1     id1     8   NULL    12068   Using where

Why is my query that uses a sub query not using the index?

Further to this, IF the size of my set of IDs from table2 exceeds 650 the index will also be dropped for some reason when doing the IN(_results_from_first_query_) example. Why is this?

  • 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-07T18:45:24+00:00Added an answer on June 7, 2026 at 6:45 pm

    How about ::

    Create an index on table1 for id1 and table2 for id2 and the query should be::

    SELECT MIN(col1) FROM table1 
    
    inner join table2 on id1=id2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this MySQL query that I'd like to optimize: SELECT min(`topic_id`) FROM `gallery_topics`
I have the following query: SELECT MIN(myColumn) as myColumn FROM myTable WHERE date(time_stamp) =
I have the following SQL Query. SELECT MIN(PLD.available_date) as 'FROM', MAX(PLD.available_date) as 'UNTIL', (
I have the following query: SELECT AVG(time) FROM (SELECT UNIX_TIMESTAMP(max(datelast)) - UNIX_TIMESTAMP(min(datestart)) AS time
I have the following query: SELECT tableOneId SUM(a+b+c) AS tableOneData, MIN(d) AS tableTwoData, FROM
I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER
I have the following query: SELECT item.`ID`, item.`Name`, item.`UnitCost`, item.`Price`, discount.`rate` FROM item INNER
I have the following query: SELECT * FROM table_name WHERE (col_1 LIKE '%$keyword%' OR
I have the following query: SELECT * FROM ( `teams` ) WHERE `name` =
I have the following query SELECT e.topicShortName, d.catalogFileID, e.topicID FROM catalog_topics a LEFT JOIN

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.