redis必殺命令 HyperLogLog

2021-09-20 09:23:18 字數 1017 閱讀 4785

redis 在 2.8.9 版本新增了 hyperloglog 結構。

redis hyperloglog 是用來做基數統計的演算法,hyperloglog 的優點是,在輸入元素的數量或者體積非常非常大時,計算基數所需的空間總是固定 的、並且是很小的。

在 redis 裡面,每個 hyperloglog 鍵只需要花費 12 kb 記憶體,就可以計算接近 2^64 個不同元素的基 數。這和計算基數時,元素越多耗費記憶體就越多的集合形成鮮明對比。

但是,因為 hyperloglog 只會根據輸入元素來計算基數,而不會儲存輸入元素本身,所以 hyperloglog 不能像集合那樣,返回輸入的各個元素。

基數:

比如資料集 , 那麼這個資料集的基數集為 , 基數(不重複元素)為5。 基數估計就是在誤差可接受的範圍內,快速計算基數。

127.0

.0.1

:6379> pfadd

wdhello

(integer) 1

127.0

.0.1

:6379> pfadd

wdworld

(integer) 1

127.0

.0.1

:6379> pfadd

wd 360

(integer) 1

127.0

.0.1

:6379> pfcount

wd(integer) 3

127.0

.0.1

:6379>

序號  命令及描述

1 pfadd key element [element ...] 新增指定元素到 hyperloglog 中。

2 pfcount key [key ...] 返回給定 hyperloglog 的基數估算值。

3 pfmerge destkey sourcekey [sourcekey ...] 將多個 hyperloglog 合併為乙個 hyperloglog

redis必殺命令 指令碼

redis 指令碼使用 lua 直譯器來執行指令碼。reids 2.6 版本通過內嵌支援 lua 環境。執行指令碼的常用命令為 eval。語法 redis 127.0 0.1 6379 eval script numkeys key key arg arg 例如 127.0 0.1 6379 eva...

redis必殺命令 雜湊 Hash

題記 redis hash 是乙個string型別的field和value的對映表,hash特別適合用於儲存物件。redis 中每個 hash 可以儲存 232 1 鍵值對 40多億 例如 127.0.0.1 6379 hmset xiongben name 王棟 desc 今年24歲 likes ...

redis必殺命令 列表 List

redis列表是簡單的字串列表,按照插入順序排序。你可以新增乙個元素導列表的頭部 左邊 或者尾部 右邊 乙個列表最多可以包含 232 1 個元素 4294967295,每個列表超過40億個元素 例如 wd wd usr local bin redis cli 127.0.0.1 6379 lpush...