M5-024Medium性能与陷阱 · Performance & traps🔥 大厂高频出现 5 次

如何近似计算去重用户数(UV)以降低查询成本?

How do you approximate distinct user counts to reduce query cost?
💡 考场高分一句话答案:

用 APPROX_COUNT_DISTINCT(HyperLogLog++),误差约 1%,成本远低于精确去重。

APPROX_COUNT_DISTINCT (HyperLogLog++), with roughly 1% error and far lower cost than an exact distinct count.

📌 核心考察要点 (Key Points)

  • 原理:HyperLogLog 用哈希的尾零位数估基数,内存 O(log log n)
  • 误差:默认相对误差约 0.8–1%,可通过 precision 参数调整
  • 适用:大盘监控、趋势分析;不适用于需要精确对账的场景
  • 注意:不能对 HLL 结果再做交并差(除非用 HLL 草图合并)
  • Principle: HLL estimates cardinality from hash leading zeros, using O(log log n) memory
  • Error: about 0.8–1% relative by default, tunable via precision
  • Use: dashboards and trend monitoring; not for financial reconciliation
  • Limitation: you cannot intersect or difference HLL results unless you merge the sketches

🔍 大厂面试官连环追问 (Follow-ups)

  • HLL 能用来算交集吗?
  • 什么时候必须用精确去重?

🎯 关联大厂考题实战训练 (5 题)

嫌字多看太累?进入苏格拉底式对话推演实验室

零打字负担,以大厂考官真实困境切入,通过点选直觉选项暴露思维盲区,手把手推导 Why。

💬 开启对话式破冰 ➔
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔