1

我需要计算每天为 mysql 中的一个巨大表创建的行数。我目前正在使用

select count(1) from table_name group by Date

查询需要更多 2000 秒并且还在计数。我想知道是否有任何优化的查询或优化我的查询的方法。

4

3 回答 3

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"
于 2013-07-10T21:04:56.840 回答
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.

于 2013-07-10T21:05:41.133 回答
0

在列上添加索引Date,我想不到其他方法可以优化此查询。

CREATE INDEX ix_date
ON table_name (Date);
于 2013-07-10T21:00:23.513 回答