1

I have the table(named users_events) consists of below columns

  1. date(date_time)
  2. eventid(autoincrement field) PRIMARY KEY
  3. two foreign keys
  4. two long text fields
  5. three integer fields and having the indexes on date_time column(btree) and two text fields(fulltext).

We have 14 million records in the table. When I try below query it's taking more than 30 seconds

 select count(*) from users_events

please help me out to how to reduce query time..

4
  • 2
    This is quite difficult to say...You could try if this query is faster: SELECT COUNT(eventid) FROM users_events. Furthermore, you could have a look in the query execution plan if there are some proposes like missing indexes etc. Commented Apr 25, 2022 at 14:20
  • What is the real purpose to have a count of over 14mil records. Just factual or what. Maybe having a separate summary table, even if by month having a count of event records pre-grouped by month/year, then it would be a simple summation of the pre-aggregates in-time, since the historical data probably is not changing. Then you only may need to run updated query for whatever the latest month is since it could keep growing as the day progresses. Commented Apr 25, 2022 at 15:25
  • As others have said. There's no meaningful diffemece bwtween large numbers. If you are looking for the biggest number select eventid from user_events order by eventid desc limit 1 should be really fast and bit the index Commented Apr 25, 2022 at 19:04
  • Using * can be faster than using a specific column, as a specific column will invoke the index if one exists for it, like it would for an ID, this has been benchmarked numerous times Commented Apr 26, 2022 at 5:32

1 Answer 1

2

Build and maintain a Summary Table

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.