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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:06:34+00:00 2026-05-13T08:06:34+00:00

I have a query that looks like this: select id , int1 , int2

  • 0

I have a query that looks like this:

select
id
, int1
, int2
, (select count(*) from big_table_with_millions_of_rows 
    where id between t.int1 and t.int2)
from myTable t
where
....

This select returns exactly one row. The id used in the inline select is an indexed column (primary key). If I replace t.int1 and t.int2 with the values of int1/int2 returned by this single row, the query completes in milliseconds. If I execute the query as above – i.e. with references to int1/int2, it takes about 10 minutes. When I run profiler and look at what actually happens, I see that 99% of the time the engine is busy returning data from the inline query. It looks as though MySql is actually running the

select ... from big_table_with_millions_of_rows 

bit of the inline query once before applying the

where id between t.int1 and t.int2

bit to the result. Can this be true? If not, then what is going on? I had always thought that inline SELECTs were potentially hazardous because they are executed row-by-row as the last element of the query, but for situations like this, where the initial SELECT is indeed highly selective, it can be very efficient. Can anyone shed any light on this?

EDIT: thanks for the feedback so far. My concern is not so much about the row-by-row nature of the inline query, but rather the fact that it seems unable to use the primary key index when faced with variables rather than (the same) hardcoded values. My guess would be that if ANALYZE has not been run recently, then the optimizer assumes it has to do a table scan as it has no knowledge about the data distribution. But shouldn’t the fact that the range lookup is done on the primary key not compensate for that?

  • 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-13T08:06:35+00:00Added an answer on May 13, 2026 at 8:06 am

    If the correlated subquery isn’t optimized well, then try this query:

    select
      t.id
    , t.int1
    , t.int2
    , count(*)
    from myTable t
    left outer join big_table_with_millions_of_rows b
      on (b.id between t.int1 and t.int2)
    where
    ....
    group by t.id
    

    That should optimize much better.


    Re your updated question: Right, MySQL is not the most sophisticated RDBMS on the market in terms of optimization. Don’t be surprised when MySQL can’t optimize corner cases like this.

    I’m a fan of MySQL for its ease of use and open source and all those good things, but the truth is that its competitors are far ahead of MySQL in terms of technology. Every RDBMS has some “blind spots” but MySQL’s seem to be larger.

    Also be sure you’re using the latest version of MySQL. They improve the optimizer in every release, so you might get better results with a newer version.

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

Sidebar

Related Questions

I have a query that looks like this SELECT * from myTable WHERE Date
I have a query that looks a bit like this: SELECT weekEnd, MAX(timeMonday) FROM
I have a query that looks like this: SELECT OrganizationName, OrganizationID, ReceivableStatus, InvoiceFee FROM
I have a sql query that looks like this: SELECT SUM(A) AS expr1 FROM
I have a query that looks like this: SELECT * FROM employees e LEFT
I have a query that looks like this: SELECT id, description, default_error, custom_error FROM
I have a query that looks like this: SELECT last_name, first_name, middle_initial FROM names
I have a query that looks like this: SELECT article FROM table1 ORDER BY
Alright. I have a query that looks like this: SELECT SUM(`order_items`.`quantity`) as `count`, `menu_items`.`name`
Suppose I have a SQL query that looks like this: SELECT fName from employees

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.