1
我有一個MySQL
數據庫表,每天有大約10-15k插入,它肯定會在下個月增加。MySQL查詢生成表統計
- 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.
我想創建一個輔助表,命名爲「統計」,和我的服務器,每天早晨上運行一個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.
你怎麼想的? Theres更好的方法?