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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:33:02+00:00 2026-06-07T07:33:02+00:00

1 – PRIMARY used in a secondary index, e.g. secondary index on ( PRIMARY

  • 0

1 – PRIMARY used in a secondary index, e.g. secondary index on (PRIMARY,column1)

2 – I’m aware mysql cannot continue using the rest of an index as soon as one part was used for a range scan, however: IN (...,...,...) is not considered a range, is it? Yes, it is a range, but I’ve read on mysqlperformanceblog.com that IN behaves differently than BETWEEN according to the use of index.

Could anyone confirm those two points? Or tell me why this is not possible? Or how it could be possible?

UPDATE:

Links:
http://www.mysqlperformanceblog.com/2006/08/10/using-union-to-implement-loose-index-scan-to-mysql/
http://www.mysqlperformanceblog.com/2006/08/14/mysql-followup-on-union-for-query-optimization-query-profiling/comment-page-1/#comment-952521

UPDATE 2: example of nested SELECT:

SELECT * FROM user_d1 uo 
WHERE EXISTS (
    SELECT 1 FROM `user_d1` ui
    WHERE ui.birthdate BETWEEN '1990-05-04' AND '1991-05-04'
    AND ui.id=uo.id
)    
ORDER BY uo.timestamp_lastonline DESC
LIMIT 20

So, the outer SELECT uses timestamp_lastonline for sorting, the inner either PK to connect with the outer or birthdate for filtering.

What other options rather than this query are there if MySQL cannot use index on a range scan and for sorting?

  • 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-07T07:33:05+00:00Added an answer on June 7, 2026 at 7:33 am

    The column(s) of the primary key can certainly be used in a secondary index, but it’s not often worthwhile. The primary key guarantees uniqueness, so any columns listed after it cannot be used for range lookups. The only time it will help is when a query can use the index alone

    As for your nested select, the extra complication should not beat the simplest query:

    SELECT * FROM user_d1 uo 
    WHERE uo.birthdate BETWEEN '1990-05-04' AND '1991-05-04'
    ORDER BY uo.timestamp_lastonline DESC
    LIMIT 20
    

    MySQL will choose between a birthdate index or a timestamp_lastonline index based on which it feels will have the best chance of scanning fewer rows. In either case, the column should be the first one in the index. The birthdate index will also carry a sorting penalty, but might be worthwhile if a large number of recent users will have birth dates outside of that range.

    If you wish to control the order, or potentially improve performance, a (timestamp_lastonline, birthdate) or (birthdate, timestamp_lastonline) index might help. If it doesn’t, and you really need to select based on the birthdate first, then you should select from the inner query instead of filtering on it:

    SELECT * FROM (
        SELECT * FROM user_d1 ui
        WHERE ui.birthdate BETWEEN '1990-05-04' AND '1991-05-04'
    ) as uo
    ORDER BY uo.timestamp_lastonline DESC
    LIMIT 20
    

    Even then, MySQL’s optimizer might choose to rewrite your query if it finds a timestamp_lastonline index but no birthdate index.

    And yes, IN (..., ..., ...) behaves differently than BETWEEN. Only the latter can effectively use a range scan over an index; the former would look up each item individually.

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

Sidebar

Related Questions

I am using Simple.Data and have been trying to find an example that will
We have a title <title>Text &#8211; text</title> I want to change it with jquery
When I execute the following query on http://dbpedia.org/sparql using (dotnetRDF) VDS.RDF.Query.SparqlRemoteEndpoint.QueryWithResultSet() everything works fine.
My NSXMLParser breaks on this string: <title>AAA &#8211; BCDEFGQWERTYUIO</title> I parsed it in this
I have installed IIS. I have browsed to my website on IIS using the
I have a PHP application that stores PHP datetime to my MySQL table. I
I am using FCKeditor in rails app. Input: ™ © ® ¢ € ¥
When I view JSON data as an alert when the page loads, I see:
An extension to my previous question: Text cleaning and replacement: delete \n from a
I've got the following login script.. <?php $name = $_POST[name]; $password = $_POST[password]; $query

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.