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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:26:56+00:00 2026-06-13T17:26:56+00:00

I am working with a table with a state column, which typically holds only

  • 0

I am working with a table with a “state” column, which typically holds only 2 or 3 different values. Sometimes, when this table holds several million rows, following SQL statement becomes slow (I assume a full table scan is done):

SELECT state, count(*) FROM mytable GROUP BY state

I expect to get something like this:

disabled |  500000
enabled  | 2000000

(basically I want to know how many items are “enabled” and how many items are “disabled” – actually that’s a number instead of a text in my real application)

I guess adding an index for my state column is pretty useless, since only very few different values can be found there. What other options do I have?

There is also a “timestamp” column (with an index). Ideally the solution should also work well if I add:

WHERE timestamp BETWEEN x AND y

Right now I’m using an SQLite3 database, but it looks like other database engines are not too different, so solutions for other DB engines might be interesting as well.

Thank you!

  • 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-13T17:26:58+00:00Added an answer on June 13, 2026 at 5:26 pm

    I would put a covering index on timestamp,state (in that order). The rationale is:

    • the condition on the timestamp will be much more selective than the state

    • if the state is still in the index (i.e covering index), the engine only has to generate a range scan on the index itself (without having to pay for random I/Os to access the main data of the table).

    Note: if the timestamp range is too wide, it will become slow despite of the index. Because random I/Os are more expensive than sequential I/Os, there is a point where the index range scan will become more expensive than the table scan. As a rule of thumb, if you need to scan more than 10% of the table, the engine should consider to keep the table scan and ignore the index. I’m note sure sqlite is smart enough to support this kind of optimization though.

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

Sidebar

Related Questions

While working with ActiveRecord I have a table which stores a serialized array of
Can anybody tell why is this query not working? DECLARE @unwantedRows TABLE ( ProductId
I have this stored procedure, which has been working for a little while CREATE
I'm working on a mobile website which is growing in popularity and this is
I have a working table view on my iphone app, and am implementing a
I working on a table that keeps record of student attending course. The data
I am working on the table view and do customization for tableFooterView .What I
A site I'm working on takes table data, counts that table data and uses
The database I'm working against has table names such as table_name. That's fine, but
I am having some problems getting a larger application with many table relationships working

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.