简体   繁体   English

如何从带分数列表的redis zset获取数据?

[英]how to get data from redis zset with score list?

I want to get data from zset with a score list, eg 我想从zset获取带有分数列表的数据,例如

zadd zset 1 a 2 b 3 c 4 d

I want a command like 我想要一个命令

'zrange zset [2, 4]'

and the result is 结果是

b, d

Thanks! 谢谢!

仅此一个ZRANGEBYSCORE命令。

you can use a piece of code to do this, like : 您可以使用一段代码来执行此操作,例如:

for score in score_list
    members = zrangebyscore(zset, score, score)
    member_list += members 

the time cost of ZRANGEBYSCORE is O(LOG(N)+M) and M is the count of members returned. ZRANGEBYSCORE的时间成本为O(LOG(N)+ M),M为返回的成员数。 so we can say the cost in our zrangebyscore is O(LOG(N)), N is the size of zset. 因此我们可以说zrangebyscore中的成本为O(LOG(N)),N为zset的大小。 So the overall time cost is O(M*LOG(N)) which M is the length of score_list. 因此,总时间成本为O(M * LOG(N)),其中M是score_list的长度。 It will be the best time cost. 这将是最好的时间成本。

The only bad thing is we cost M RTTs in network transport. 唯一的坏事是我们在网络传输中花费了M个RTT。 We can use the pipeline to speedup the queries. 我们可以使用管道来加快查询速度。

redis document says: Redis文件说:

A Request/Response server can be implemented so that it is able to process new requests even if the client didn't already read the old responses. 可以实现请求/响应服务器,以便即使客户端尚未读取旧响应,它也可以处理新请求。 This way it is possible to send multiple commands to the server without waiting for the replies at all, and finally read the replies in a single step. 这样,可以将多个命令发送到服务器,而根本不用等待答复,最后一步即可读取答复。

So if you use pipeline tech, It will only cost one RTT time. 因此,如果您使用管道技术,则只需花费一个RTT时间。 And I think every language's client for redis has implemented the pipeline. 而且我认为每种语言的redis客户都已实现了管道。 good luck ! 祝好运 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM