我有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