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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:46:45+00:00 2026-06-18T15:46:45+00:00

I have a quite big SQLite3 database table with a numeric indexed field on

  • 0

I have a quite big SQLite3 database table with a numeric indexed field on which I have to search for a list of ranges of values. As the numeric values are huge 64-bit numbers, a IN clause would not an option. The queries typically look like this:

SELECT * FROM sometable WHERE ID BETWEEN 10 AND 11 
                           OR ID BETWEEN 20 AND 21 
                           OR ID BETWEEN 30 AND 31;

I have experienced a strange performance limit. With up to 9 BETWEEN terms, the query is extremely fast (ID field is indexed). But starting with 10 terms, the query becomes several orders or magnitude slower! I do not have found any explanation to that limit in the documentation.

I found that the EXPLAIN QUERY PLAN instruction can be used to see the change of behavior. I made my experiments with SQLite 3.7.12 in case that matters.

For the sake of demonstration, let’s create a very simple and empty table:

CREATE TABLE sometable(name TEXT, ID INTEGER);
CREATE INDEX id_idx ON sometable (ID ASC);

This query:

EXPLAIN QUERY PLAN SELECT * FROM sometable WHERE ID BETWEEN 10 AND 11 
 OR ID BETWEEN 20 AND 21 OR ID BETWEEN 30 AND 31 OR ID BETWEEN 40 AND 41
 OR ID BETWEEN 50 AND 51 OR ID BETWEEN 60 AND 61 OR ID BETWEEN 70 AND 71
 OR ID BETWEEN 80 AND 81 OR ID BETWEEN 90 AND 91;     

produces that result:

0|0|0|SEARCH TABLE sometable USING INDEX id_idx (ID>? AND ID<?) (~31250 rows)
0|0|0|SEARCH TABLE sometable USING INDEX id_idx (ID>? AND ID<?) (~31250 rows)
0|0|0|SEARCH TABLE sometable USING INDEX id_idx (ID>? AND ID<?) (~31250 rows)
0|0|0|SEARCH TABLE sometable USING INDEX id_idx (ID>? AND ID<?) (~31250 rows)
0|0|0|SEARCH TABLE sometable USING INDEX id_idx (ID>? AND ID<?) (~31250 rows)
0|0|0|SEARCH TABLE sometable USING INDEX id_idx (ID>? AND ID<?) (~31250 rows)
0|0|0|SEARCH TABLE sometable USING INDEX id_idx (ID>? AND ID<?) (~31250 rows)
0|0|0|SEARCH TABLE sometable USING INDEX id_idx (ID>? AND ID<?) (~31250 rows)
0|0|0|SEARCH TABLE sometable USING INDEX id_idx (ID>? AND ID<?) (~31250 rows)

While that query:

EXPLAIN QUERY PLAN SELECT * FROM sometable WHERE ID BETWEEN 10 AND 11 
 OR ID BETWEEN 20 AND 21 OR ID BETWEEN 30 AND 31 OR ID BETWEEN 40 AND 41
 OR ID BETWEEN 50 AND 51 OR ID BETWEEN 60 AND 61 OR ID BETWEEN 70 AND 71
 OR ID BETWEEN 80 AND 81 OR ID BETWEEN 90 AND 91 OR ID BETWEEN 100 AND 101;

produces that result:

0|0|0|SCAN TABLE sometable (~500000 rows)

SCAN TABLE means that the index is not used and the whole original table is searched, resulting in poor performance.

Is there a way (pragma / compilation switch / trick) to avoid that limit?

  • 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-18T15:46:46+00:00Added an answer on June 18, 2026 at 3:46 pm

    As you can see, SQLite tries to split up the query into multiple subqueries so that each range can be looked up individually in the index.

    However, when there are too many ranges, the query optimizer assumes that the sum of the cost of all the individual subqueries is larger than just going once through the table.

    If your ranges contain less than 31250 rows, or if your table has more than 1000000 rows, you can try to use the ANALYZE command to improve the cost estimates.

    As a last resort, you can split up the query manually to force separate lookups:

    SELECT * FROM sometable WHERE ID BETWEEN 10 AND 11
    UNION ALL
    SELECT * FROM sometable WHERE ID BETWEEN 20 AND 21
    UNION ALL
    SELECT * FROM sometable WHERE ID BETWEEN 30 AND 31 
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have quite a few situations where I have database structures similar to: TABLE
I have a text file which is tab separated. They can be quite big
I have an Inno Setup installer with a Tasks list which has gotten quite
I have Java application, which, unfortunately, begins to consume quite big amounts of memory
I have a quite big software project written with an IDE which stores the
I have a class named DataBoundObject which is quite big. And works good. Its
I have a listview that has datasource getting from Network ( quite big list).
I have a quite big program in .net 3.5 SP1 which is compiling just
I have quite a big table (about 4M of entries) for emails that are
I have quite big document in html format that generated from Microsoft Word. It

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.