问题描述
尝试从Redis排序集中读取1M记录时,我突然遇到性能问题。我使用光标和批量大小为5K的 ZSCAN
。 使用Erlang R14在同一台机器上执行代码Redis的。接收5K元件批次需要近1秒。不幸的是,我没有在这台机器上编译Erlang R16,但是我觉得没关系。
为了比较,Node.js代码与node_redis(hiredis parser)做1M在2秒钟内。对于Python和PHP来说,结果相同。
也许我做错了?
提前感谢。 >
这是我的Erlang代码:
-module(redis_bench)。
-export([run / 0])。
-define(COUNT,5000)。
run() - >
{_,Conn} = connect_to_redis(),
read_from_redis(Conn)。
connect_to_redis() - >
eredis:start_link(host,6379,0,pass)。
read_from_redis(_Conn,0) - >
ok;
read_from_redis(Conn,Cursor) - >
{ok,[Cursor1 | _]} = eredis:q(Conn,[ZSCAN,if:push:sset:test,Cursor,COUNT,?COUNT]),
io:format(Batch〜n),
read_from_redis(Conn,Cursor1)。
read_from_redis(Conn) - >
{ok,[Cursor | _]} = eredis:q(Conn,[ZSCAN,if:push:sset:test,0,COUNT,?COUNT]),
read_from_redis(Conn,Cursor)。
切换到将1M键的读取时间缩短到16秒。不快,但可以接受。
这是新的代码:
-module(redis_bench2)。
-export([run / 0])。
-define(COUNT,200000)。
run() - >
io:format(Start〜n),
redis:connect([{ip,host},{port,6379},{db,0},{pass,pass }]),
read_from_redis()。
read_from_redis(<0>>) - >
ok;
read_from_redis(Cursor) - >
[{ok,Cursor1} | _] = redis:q([ZSCAN,if:push:sset:test,Cursor,COUNT,?COUNT]),
io: format(Batch〜n),
read_from_redis(Cursor1)。
read_from_redis() - >
[{ok,Cursor} | _] = redis:q([ZSCAN,if:push:sset:test,0,COUNT,?COUNT]),
read_from_redis光标)。
I suddenly encountered performance problems when trying to read 1M records from Redis sorted set. I used ZSCAN
with cursor and batch size 5K.
Code was executed using Erlang R14 on the same machine that hosts Redis. Receiving of 5K elements batch takes near 1 second. Unfortunately, I failed to compile Erlang R16 on this machine, but I think it does not matter.
For comparison, Node.js code with node_redis (hiredis parser) does 1M in 2 seconds. Same results for Python and PHP.
Maybe I do something wrong?
Thanks in advance.
Here is my Erlang code:
-module(redis_bench).
-export([run/0]).
-define(COUNT, 5000).
run() ->
{_,Conn} = connect_to_redis(),
read_from_redis(Conn).
connect_to_redis() ->
eredis:start_link("host", 6379, 0, "pass").
read_from_redis(_Conn, 0) ->
ok;
read_from_redis(Conn, Cursor) ->
{ok, [Cursor1|_]} = eredis:q(Conn, ["ZSCAN", "if:push:sset:test", Cursor, "COUNT", ?COUNT]),
io:format("Batch~n"),
read_from_redis(Conn, Cursor1).
read_from_redis(Conn) ->
{ok, [Cursor|_]} = eredis:q(Conn, ["ZSCAN", "if:push:sset:test", 0, "COUNT", ?COUNT]),
read_from_redis(Conn, Cursor).
Switching to redis-erl decreased read time of 1M keys to 16 seconds. Not fast, but acceptable.
Here is new code:
-module(redis_bench2).
-export([run/0]).
-define(COUNT, 200000).
run() ->
io:format("Start~n"),
redis:connect([{ip, "host"}, {port, 6379}, {db, 0}, {pass, "pass"}]),
read_from_redis().
read_from_redis(<<"0">>) ->
ok;
read_from_redis(Cursor) ->
[{ok, Cursor1}|_] = redis:q(["ZSCAN", "if:push:sset:test", Cursor, "COUNT", ?COUNT]),
io:format("Batch~n"),
read_from_redis(Cursor1).
read_from_redis() ->
[{ok, Cursor}|_] = redis:q(["ZSCAN", "if:push:sset:test", 0, "COUNT", ?COUNT]),
read_from_redis(Cursor).
这篇关于Erlang和Redis:读取性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!