LoginSignup
0
0

More than 5 years have passed since last update.

RedisのSET型を用いた集合演算

Last updated at Posted at 2014-11-05

数十万程度のユーザ集合に対するベン図を作成するためにA∩B∩C的なものの計算が必要になったため、勉強がてらredisを使ってみました。Pythonなどでのスクリプトは書かずにredis-cliで処理しています。

Redis SET型については以下解説がわかりやすかったです。

ニコニコ生放送に見る Redis 活用ノウハウ
http://gihyo.jp/dev/feature/01/redis/0004

インストール

http://qiita.com/i2bskn/items/42cbf6bc52f9cfa406bb に従う

元データをbulk import(mass insertion)する

元データ作成

http://redis.io/topics/mass-insert
を参考に、以下のようなデータを作成。改行コードをCRLFにする必要がある。

$ head DataSet_A 
SADD A user0000000000000001
SADD A user0000000000000002
SADD A user0000000000000003
…
$ head DataSet_B
SADD B user0000000000000003
SADD B user0000000000000004
SADD B user0000000000000005
…
$ head DataSet_C 
SADD C user0000000000000001
SADD C user0000000000000003
SADD C user0000000000000007
…

ロードします

$ cat DataSet_A DataSet_B DataSet_C | time redis-cli --pipe
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 1700000

この状態でtopで確認すると redis-serverのVIRTは653m、RESは196m

失敗したらflushします

$ redis-cli
127.0.0.1:6379> FLUSHALL
OK

集合演算を実施

$ redis-cli
127.0.0.1:6379>

http://redis.io/commands#set
を参考に

Aの要素数

> SCARD A

Aの要素リスト

> SMEMBERS A

A∩B∩Cリスト

> SINTER A B C

A∩^B∩^Cリスト

> SDIFF A B C

A∪B∪Cリスト

> SUNION A B C

A∪B∪Cの要素数

> SUNIONSTORE X A B C
> SCARD X A B C

所感

・パイプラインはredis-cliでは利用できない?
・1,700,000レコードでRESが196mなので、単純に考えるとメモリ4GBで30Mレコード強扱える?
 (ホットバックアップは考えないで)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0