redis 扫描返回空结果但非零游标 [英] redis scan returns empty results but nonzero cursor

查看:45
本文介绍了redis 扫描返回空结果但非零游标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含几百万个键的 redis 数据库.有时我需要通过模式查询键,例如2016-04-28:* 我使用 scan.第一次调用应该是

I have a redis database with a few million keys. Sometimes I need to query keys by the pattern e.g. 2016-04-28:* for which I use scan. First call should be

scan 0 match 2016-04-28:*

如果搜索完成,它将返回一堆键和下一个光标或 0.

it then would return a bunch of keys and next cursor or 0 if the search is complete.

但是,如果我运行查询并且没有匹配的键,scan 仍然返回非零光标但一组空键.每次后续查询都会发生这种情况,因此搜索似乎不会在很长时间内结束.

However, if I run a query and there are no matching keys, scan still returns non-zero cursor but an empty set of keys. This keeps happening to every successive query, so the search does not seem to end for a really long time.

Redis 文档说

SCAN 系列函数不保证每次调用返回的元素数量在给定范围内.命令也允许返回零元素,只要返回的游标不为零,客户端就不应认为迭代完成.

SCAN family functions do not guarantee that the number of elements returned per call are in a given range. The commands are also allowed to return zero elements, and the client should not consider the iteration complete as long as the returned cursor is not zero.

所以当我得到一组空密钥时,我不能停下来.

So I can't just stop when I get empty set of keys.

有什么办法可以加快速度吗?

Is there a way I can speed things up?

推荐答案

您总是需要完成扫描(即 get cursor == 0)以确保没有匹配项.但是,您可以使用 COUNT 选项减少迭代次数.默认值 10 很快 如果这是您的匹配模式的常见情况 - 开始增加它(例如双倍或 2 的幂,但设置最大上限以防万一),使 Redis 更难"搜索键.通过这样做,您将节省网络往返行程,因此它应该加快速度".

You'll always need to complete the scan (i.e. get cursor == 0) to be sure there no no matched. You can, however, use the COUNT option to reduce the number of iterations. The default value of 10 is fast If this is a common scenario with your match pattern - start increasing it (e.g. double or powers of two but put a max cap just in case) with every empty reply, to make Redis "search harder" for keys. By doing so, you'll be saving on network round trips so it should "speed things up".

这篇关于redis 扫描返回空结果但非零游标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆