1. 程式人生 > >【Redis學習筆記三】慢查詢、pipeline、釋出訂閱、Bitmap、HyperLogLog、GEO

【Redis學習筆記三】慢查詢、pipeline、釋出訂閱、Bitmap、HyperLogLog、GEO

  • 目錄
    • 慢查詢
    • pipeline
    • 釋出訂閱
    • Bitmap
    • HyperLogLog
    • GEO

慢查詢

生命週期

傳送命令 --> 排隊 --> 執行命令 --> 返回結果

說明:

1.慢查詢發生在第三階段

2.客戶端超時不一定有慢查詢,四個階段都可能會是超時的原因

兩個配置

slowlog-max-len

1.先進先出佇列

2.固定長度

3.儲存在記憶體內,不會持久化

slowlog-log-slower-than

1.慢查詢閾值(單位:微秒)

2.slowlog-log-slower-than=0,記錄所有命令

3.slowlog-log-slower-than<0,不記錄所有命令

配置方法

1.預設值

config get slowlog-max-len = 128

config get slowlog-log-slower-than = 10000

2.修改配置檔案重啟

3.動態配置

config get slowlog-max-len 100

config get slowlog-log-slower-than 1000

三個命令

slowlog get [n] #獲取慢查詢佇列
slowlog len #獲取慢查詢佇列長度
slowlog reset #清空慢查詢佇列

運維經驗

1.slowlog-max-len閾值不要設定過大,默人10ms,通常設定1ms

2.slowlog-log-slower-than不要設定過小,通常設定1000左右

3.理解命令的生命週期

4.定期持久化慢查詢(針對第2條)

pipeline

什麼是pipeline

client – 傳輸命令 – 計算 – 返回結果

一次時間 = 一次網路時間 + 一次命令時間

n次時間 = n次網路時間 + n次命令時間

pipeline:將命令進行批量打包,在server中計算n次,一次返回結果

pipeline與原生操作對比

原生命令:原子的

pipeline:非原子的

使用建議

1.注意每次pipeline攜帶的資料量

2.pipeline每次只能作用在一個Redis節點上

3.M操作與pipeline的區別

釋出訂閱

角色

釋出者(publisher)

訂閱者(subscriber)

頻道(channel)

API

publish

釋出命令:publish channel message,如:

redis> publish sohu:tv "hello world"
(integer) 3 #訂閱者個數

subscribe

訂閱:subscribe [channel] #一個或多個,如:

redis> subscribe sohu:tv
1) "subscribe"
2) "sohu:tv"
3) (integer) 1
1) "message"
2) "sohu:tv"
3) "hello world"

unsubscribe

取消訂閱:unsubscribe [channel] #一個或多個,如:

redis> unsubscribe sohu:tv
1) "unsubscribe"
2) "sohu:tv"
3) (integer) 0

其他API

psubscribe [pattern...]:訂閱模式

punsubscribe [pattern...]:退訂指定的模式

pubsub channels:列出至少有一個訂閱者的頻道

pubsub numsub [channel...]:列出給定頻道的訂閱者數量

pubsub numpat:列出被訂閱模式的數量

與訊息佇列的區別

  • 釋出訂閱:消費者都會收到
  • 訊息佇列:消費者“搶”訊息

Bitmap

點陣圖

setbit

setbit key offset value:給點陣圖指定索引設定值

getbit

getbit key offset:獲取點陣圖指定索引的值

bitcount

bitcount key [start end]:獲取點陣圖指定範圍(start到end,單位為位元組,如果不指定就是獲取全部)位值為1的個數

bitop

bitop op destkey key [key...]:做多個bitmap的and(交集)、or(並集)、not(非)、xor(異或)操作並將結果儲存在destkey中

bitpos

bitpos key targetBit [start] [end]:計算點陣圖指定範圍(start到end,單位為位元組,如果不指定就是獲取全部)第一個便宜量對應的值等於targetBit的位置

HyperLogLog

1.基於HyperLogLog演算法:極小空間實現獨立數量統計

2.本質還是字串

API

pfadd key element [element...]:向hyperloglog新增元素

pfcount key [key...]:計算hyperloglog的獨立總數

pfmerge destkey sourcekey [courcekey...]:合併多個hyperloglog

redis> pfadd 2018_10_24:unique:ids "uuid-1" "uuid-2" "uuid-3" "uuid-4"
(integer) 1
redis> pfcount 2018_10_24:unique:ids
(integer) 4
redis> pfadd 2018_10_24:unique:ids "uuid-1" "uuid-2" "uuid-3" "uuid-90"
(integer) 1
redis> pfcount 2018_10_24:unique:ids
(integer) 5
redis> pfadd 2018_10_25:unique:ids "uuid-4" "uuid-6" "uuid-7"
(integer) 1
redis> pfmerge 2018_10_24_25:unique:ids 2018_10_24:unique:ids 2018_10_25:unique:ids
OK
redis> pfcount 2018_10_24_25:unique:ids
(integer) 8

使用經驗

1.是否能容忍錯誤?(錯誤率:0.81%)

2.是否需要單條資料?

GEO

GEO:地理資訊定位,儲存經緯度,計算兩地距離,範圍計算等

API

geoadd

geoadd key longitude latitude member [longitude latitude member...]:增加地理位置資訊

redis> geoadd cities:locations 116.28 39.55 beijing
(integer) 1
redis> geoadd cities:locations 117.12 39.08 tianjin 114.29 38.01 shijiazhuang 118.01 39.38 tangshan 115.29 38.51 baoding

geopos

geopos key member [member ...]:獲取地理位置資訊

redis> geopos cities:locations tianjin
1) 1) "117.12000042200099501"
   2) "39.0800000535766543"

geodist

geodist key member1 member2 [unit]:獲取兩個地理位置的距離,其中unit的可選項:m(米)、km(千米)、mi(英里)、ft(尺)

redis> geodist cities:locations tianjin beijing km
"89.2061"