Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Hyperloglog 简析
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
onlyice
January 25, 2021
Programming
190
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Hyperloglog 简析
描述了在面对海量数据时,如何利用 HyperLogLog 做去重和统计,以及它背后的概率原理。
onlyice
January 25, 2021
Other Decks in Programming
See All in Programming
リアルな遅延を測る仕様
kota_yata
1
140
型解析で実現する Go の言語内 DSL / Conference に Go! タイムテーブルの歩き方 for Gophers
mazrean
0
120
一参加者から『中の人』へ 〜全通PHPerがブースに立って学んだ、カンファレンスを100倍楽しむコツ〜
wp_daisuke
0
110
Swift愛好会と私(ウホーイ) / Swift Fan Club and Uhooi
uhooi
0
120
思考垂れ流し開発 ~音声入力 × AIエージェント × 開発ハーネスによる試行錯誤~
npostring
0
810
go-spidermonkeyでAIエージェントのCode Modeを実装する
syumai
3
1.5k
The Good Stuff, Not the Slop: Engineering High-Quality Android Apps with Modern AI Tooling
danybony
0
120
Go 1.27からのGODEBUG / Go 1.27 リリースパーティ #go127party
mazrean
0
290
変化を抱擁するドキュメントの作り方 - ビジネスルール駆動開発がもたらす、コードとの新しい関係
ioki
2
100
Jindong: Introducing Declarative Haptics in Compose Multiplatform
l2hyunwoo
0
140
初めての模倣学習とVLA
natsutan
0
450
tsc.rip を支える技術 / Kyoto.なんか #8
susisu
0
4.3k
Featured
See All Featured
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
560
Context Engineering - Making Every Token Count
addyosmani
9
1.1k
Practical Orchestrator
shlominoach
191
12k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.8k
GitHub's CSS Performance
jonrohan
1033
470k
The SEO identity crisis: Don't let AI make you average
varn
0
550
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
370
WCS-LA-2024
lcolladotor
0
820
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Paper Plane
katiecoart
PRO
2
53k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.2k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2.1k
Transcript
HyperLogLog 简析 林志衡 blog.zhiheng.io 2021/01/23
问题 大数据量去重统计,比如页面 UV。 2
传统方法 把全部数据加入 Set。 优点:精确 缺点:内存消耗巨大 概率方法 HyperLogLog 算法。 优点:内在消耗很少 缺点:不精确,但误差可以控
制在 1% 以内 3
什么是概率方法? 4
一个简单例子 将原始数据通过哈希算法随机生成一批均匀 分布在 [0, 1) 的数字。 只要随机过程足够均匀,比如图中的 hash(x)。 那么: 估算的元素个数
= 1 / 最小的 hash 值 缺点: 如果最小的哈希值碰巧很小,那么估算误差 巨大。 5
另外一种方法 计算末尾连续 0 个数。 前提: 1. 哈希算法计算出来的值是均匀分布的整数 2. 均匀分布的整数中,末位是连续 n
个 0 的 概率为 2^n 结论: 计算一批哈希值中,末位连续 0 最多的数字, 比如是连续 R 位。那么: 估算的元素个数 = 2^R 6
连续末尾 0 计数法 缺点: 1. 估算出来的元素个数 只能是 2 的次方 2.
估算结果可能 不精确(当碰巧出现末尾连 续 0 比较多时) 7
优化精确度 使用多个 hash 函数,生成多批哈希值并估算 出多个元素个数值,再做平均值。 优点: 避免单个 hash 函数带来可能的大误差。 缺点:
计算量成倍增加。 8 LogLog 算法, 1. 取哈希值的前几位作为桶(bucket,也称 register) 2. 按前几位的不同,把数字分入不同的桶中 3. 各个桶分别计算出元素个数,再估算出总 数 优点: 1. 通过分桶实现与多个 hash 函数一样的效果 2. 计算量不会大量增加
LogLog 算法 9 比如取前 4 位来生成桶。 有 2^4 即 16
个桶( 表示)。 数字按其前 4 位分到不同桶。 每个桶可以统计出最长的连续末 尾 0 位数,比如图中桶 11 的结 果为 5(R11 表示)。 将不同桶的结果取算术平均数。 套入下面的公式: 即可计算出更精确的元素个数。 CARDINALITY = ⋅ ⋅ 2 1 σ=1 R 没有研究数学推导的过程。 constant 取值为 0.79402。 这种方式的误差在 1.3/ 。
进一步提升精确度 10 SuperLogLog: 去掉异常值、使用几何平均数。 精确度提升到 1.05 。 HyperLogLog: 使用调和平均数。 精确度提升到
1.04 。
Redis 的 HyperLogLog 实现 11 • Hash 值为 64 位整数
• 使用前 14 位作为桶(register),一共 16K 个桶(16384 ) • 误差在 0.81%
参考 12 1. HyperLogLog in Presto: A significantly faster way
to handle cardinality estimation https://engineering.fb.com/2018/12/13/data-infrastructure/hyperloglog/ 2. Redis new data structure: the HyperLogLog http://antirez.com/news/75
Thanks! 林志衡 blog.zhiheng.io 13