我有一个MySQL
数据库表,每天大约有10-15k个插入,并且肯定会在下个月增加。
- Table Example (reservations): *important fields*
+----+--------+----------+---------+-----+
| ID | people | modified | created | ... |
+----+--------+----------+---------+-----+
我需要提供每日统计信息,根据用户选择的日期或日期范围通知条目数(总数和指定人数相同)。 今天我每次请求都要执行两个查询。它工作正常,有理想的延迟,但我想知道它是否会有更多数据稳定。
- Single Date:
SELECT COUNT(*) from reservations WHERE created='DATE USER SELECTED'
SELECT COUNT(*), people from reservations WHERE created='DATE USER SELECTED' GROUP BY people
- Date Range:
SELECT COUNT(*) from reservations WHERE created BETWEEN 'DATE USE SELECTED' AND 'DATE USE SELECTED';
SELECT COUNT(*), people from reservations WHERE created BETWEEN 'DATE USE SELECTED' AND 'DATE USE SELECTED' GROUP BY people
IN MY VIEW
Pros: Real time statistics.
Cons: Can overload the database, with similar and slow queries.
我想创建一个名为' statistics'的辅助表,每天早上在我的服务器上运行一个cronjob来计算所有统计信息。
- Table Example (statistics):
+----+------+--------------------+---------------------------+---------------------------+-----+
| ID | date | numberReservations | numberReservations2People | numberReservations3People | ... |
+----+------+--------------------+---------------------------+---------------------------+-----+
- IN MY VIEW
Pros: Faster queries, do not need to count every request.
Cons: Not real time statistics.
你怎么看?这是一个更好的方法吗?
答案 0 :(得分:1)
如果您的表中包含正确的复合索引,则可以有效地满足您显示的汇总查询。如果您不确定复合索引,可以阅读它们。
(created,people)
上的索引reservations
是这两个查询的正确索引。他们都可以通过一种称为松散范围扫描的高效索引扫描来满足。你会发现它们足够快,你不需要为辅助表而烦恼在您的系统中可预见的未来。
这很好,因为像你提出的二级表是混淆和错误的常见来源。