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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:47:55+00:00 2026-05-23T02:47:55+00:00

I have a set of consecutive rows I want to get based upon their

  • 0

I have a set of consecutive rows I want to get based upon their primary key, which is an auto-incrementing integer. Assuming that there are no holes, is there any performance between between:

SELECT * FROM `theTable` WHERE `id` IN (n, ... nk); 

and:

SELECT * FROM `theTable` WHERE `id` BETWEEN n AND nk;
  • 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-23T02:47:55+00:00Added an answer on May 23, 2026 at 2:47 am

    BETWEEN should outperform IN in this case (but do measure and check execution plans, too!), especially as n grows and as statistics are still accurate. Let’s assume:

    • m is the size of your table
    • n is the size of your range

    Index can be used (n is tiny compared to m)

    • In theory, BETWEEN can be implemented with a single “range scan” (Oracle speak) on the primary key index, and then traverse at most n index leaf nodes. The complexity will be O(n + log m)

    • IN is usually implemented as a series (loop) of n “range scans” on the primary key index. With m being the size of the table, the complexity will always be O(n * log m) … which is always worse (neglibile for very small tables m or very small ranges n)

    Index cannot be used (n is a significant portion of m)

    In any case, you’ll get a full table scan and evaluate the predicate on each row:

    • BETWEEN needs to evaluate two predicates: One for the lower and one for the upper bound. The complexity is O(m)

    • IN needs to evaluate at most n predicates. The complexity is O(m * n) … which is again always worse, or perhaps O(m) if the database can optimise the IN list to be a hashmap, rather than a list of predicates.

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

Sidebar

Related Questions

I have a set of n consecutive operations which each one runs in O(1)
I have a set a data that I need to calculate their consecutive mean
I have a table which contains a unique integer field containing consecutive values. When
for around 5 consecutive days i have been trying to set up my computer
I currently have some expectations set up on a mock with consecutive calls: The
I have a sumranges() function, which sums all the ranges of consecutive numbers found
I have a set of structured data, that I'm trying to merge together, based
I have a large data set and i want to convert it into array.
I have set of categories (the number of which changes according to specific state)
Here, I have a one-dimensional integer space (consist of random intervals defined by their

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.