SQL 查询优化:统计微信崩溃日志在 iOS 和 Android 平台上的发生次数
以下是修改后的代码:
WITH crash_counts AS (
SELECT
CASE
WHEN PlatformType IN (1,3,5) THEN log_iOS_crash.uuid
WHEN PlatformType IN (2,4,6) THEN log_android_crash.crashid
END AS CrashIdentifier,
count(*) AS count
FROM log_iMonkey_Wechat_overview
LEFT JOIN log_iOS_crash ON log_iMonkey_Wechat_overview.CrashIdentifier = log_iOS_crash.uuid
LEFT JOIN log_android_crash ON log_iMonkey_Wechat_overview.CrashIdentifier = log_android_crash.crashid
WHERE Debug = 0 AND PlatformType != 0 AND CrashIdentifier != '' AND Ds >= toDateTime(#wechat_crash_level_time.start#)
AND Ds <= toDateTime(#wechat_crash_level_time.end#)
GROUP BY CrashIdentifier, PlatformType
)
SELECT
PlatformType,
SUM(count) AS index_crashcount_0
FROM crash_counts
WHERE PlatformType IN (1, 3, 5) OR PlatformType IN (2, 4, 6)
GROUP BY PlatformType
ORDER BY index_crashcount_0 DESC
优化说明:
- 使用 WITH 语句创建名为 crash_counts 的子查询,将 CrashIdentifier 和 对应的数量统计出来,避免重复计算。
- 使用 CASE 语句根据 PlatformType 的值选择相应的 CrashIdentifier 字段,避免使用两个 LEFT JOIN 语句。
- 使用 SUM 函数对每个 PlatformType 的 CrashIdentifier 数量进行累加。
- 最后根据 index_crashcount_0 降序输出结果。
改进后的代码更加简洁高效,并提高了可读性。
原文地址: https://www.cveoy.top/t/topic/pIQk 著作权归作者所有。请勿转载和采集!