我有1個名爲錯誤它具有以下結構:優化SQL查詢連接
錯誤
| id | UserID | CrashDump | ErrorCode| Timestamp
| 1 | user1 | Crash 1 | 100 | 2015-04-08 21:00:00
| 2 | user2 | Crash 2 | 102 | 2015-04-10 22:00:00
| 3 | user3 | Crash 4 | 105 | 2015-05-08 12:00:00
| 4 | user4 | Crash 4 | 105 | 2015-06-02 21:22:00
| 5 | user4 | Crash 4 | 105 | 2015-06-03 04:16:00
我希望得到如下數據結果集:
預期結果集
CrashDump | Error Count| Affected Users|
Crash 4 | 3 | 2 |
Crash 2 | 1 | 1 |
Crash 1 | 1 | 1 |
結果集會將每個錯誤的計數保存爲錯誤計數和受影響的用戶(接收到此錯誤的不同用戶)。
我已經能夠使用下面的查詢獲得期望的結果,但它已被證明是非常資源密集型的,並且在巨大的數據集MySQL崩潰。 您能否引導我如何優化我目前的查詢或指導我實現其邏輯的更好方法?任何幫助將不勝感激。
當前查詢:
select B.CrashDump as CrashDump, B.B_UID as affected users, C.C_UID as ErrorCount
from
(
Select count(A.UserID) as B_UID, A.CrashDump, (A.timestamp) as timestmp,
(a.errorcode) as errorCde, (a.ID) as uniqueId
from
(
select UserID , CrashDump, timestamp,errorcode,id
from errors
where Timestamp >='2015-04-08 21:00:00' and Timestamp <='2015-06-10 08:18:15'
group by userID,CrashDump
) as A
group by A.CrashDump
) as B
left outer join
(
select CrashDump , count(UserID) as C_UID
from errors
where Timestamp >='2015-04-08 21:00:00' and Timestamp <='2015-06-10 08:18:15'
group by CrashDump
) as C
On B.CrashDump = C.CrashDump
order by ErrorCount desc limit 0,10
你的問題是使用'GROUP BY'和['GROUP BY'聚合函數]解決的經典問題(http://dev.mysql.com/doc/refman/5.7/en/group-by-functions的.html)。這[回答](http://stackoverflow.com/a/30591063/4265352)顯示你的解決方案。 – axiac