2014-06-20 122 views
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更好的方法?

回答

1

如果您的表格中包含正確的複合索引,則可以高效地滿足您顯示的聚合查詢。如果你不確定複合索引,你可以閱讀它們。

您的reservations上的索引(created,people)對於這兩個查詢都是正確的。他們都可以通過一種有效的索引掃描來滿意,這種掃描被稱爲寬鬆範圍掃描。你會發現它們足夠快,以至於在系統的可預見的將來你不需要爲輔助表打擾。

這很好,因爲像你這樣的輔助表格是混淆和錯誤的常見來源。