1

I need to calculate the num of rows created on a daily basis for a huge Table in mysql. I'm currently using

select count(1) from table_name group by Date

THe query is taking more 2000sec and counting. I was wondering if there's any optimized query or a way to optimize my query.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Rishabh Agarwal
  • 91
  • 1
  • 1
  • 7

3 Answers3

1
  • Make sure that "date" field is of "date" type, not datetime nor timestamp
  • Index that column
  • If you need it for one day, add a "where" statement. i.e. WHERE date="2013-07-10"
Jehad Keriaki
  • 545
  • 5
  • 10
1

If you're only interested in items that were created on those dates, you could calculate the count at end-of-day and store it another table.

That lets you run the COUNT query on a much smaller data set (Use WHERE DATE(NOW()) = Date and drop the GROUP BY)

Then then query the new table when you need the data.

ben
  • 1,946
  • 2
  • 18
  • 26
0

Add an index on the Date column, there's no other way to optimize this query that I can think of.

CREATE INDEX ix_date
ON table_name (Date);
Barmar
  • 741,623
  • 53
  • 500
  • 612