赞
踩
1.es,去重,cartinality metric,对每个bucket中的指定的field进行去重,取去重后的count,类似于count(distcint)
GET /tvs/sales/_search { "size": 0, "aggs": { "group_by_color":{ "date_histogram": { "field": "sold_date", "interval": "month" }, "aggs": { "group_by_brand": { "cardinality": { "field": "brand" } } } } } }
2.cardinality,count(distinct),5%的错误率,性能在100ms左右
(1):precision_threshold优化准确率和内存开销
brand去重,如果brand的unique value,在100个以内,小米,长虹,三星,TCL,HTL。。。
GET /tvs/sales/_search { "size": 0, "aggs": { "group_by_color":{ "date_histogram": { "field": "sold_date", "interval": "month" }, "aggs": { "group_by_brand": { "cardinality": { "field": "brand", "precision_threshold": 100 } } } } } }
field,去重,count,这时候,unique value,10000,precision_threshold=10000,10000 * 8 = 80000个byte,80KB
HyperLogLog++ (HLL)算法性能优化
cardinality底层算法:HLL算法,HLL算法的性能
会对所有的uqniue value取hash值,通过hash值近似去求distcint count,误差
默认情况下,发送一个cardinality请求的时候,会动态地对所有的field value,取hash值; 将取hash值的操作,前移到建立索引的时候
PUT /tvs/ { "mappings": { "sales": { "properties": { "brand": { "type": "text", "fields": { "hash": { "type": "murmur3" } } } } } } }
- GET /tvs/sales/_search
- {
- "size" : 0,
- "aggs" : {
- "distinct_brand" : {
- "cardinality" : {
- "field" : "brand.hash",
- "precision_threshold" : 100
- }
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。