Redis中高效的索引类型操作 [英] Efficient index type operations in Redis
问题描述
我正在尝试在 Redis 中创建一组索引,用于执行 AND 操作.
I am trying to create a set of indexes in Redis, for doing AND operations.
像这样:
inx:haircolor:blonde = set(key1,key2,key3)
inx:eyecolor:blue = set(key1,key2)
inx:haircolor:blonde = set(key1,key2,key3)
inx:eyecolor:blue = set(key1,key2)
我可以使用 sinter
找到所有金色头发和蓝色眼睛的钥匙.
And I can use sinter
to find all keys with Blond hair and blue eyes.
我有这样的哈希:
key1: name=Rick haircolor=blonde eyecolor=blue
key1: name=Rick haircolor=blonde eyecolor=blue
获取结果键并检索匹配哈希的最快方法是什么.
What is the fastest way to take the resulting keys, and retrieve matching hashes.
这只是为了更容易理解的演示数据,我用它来存储分析,我需要做一些大的关键查找集.
This is just demo data to make it easier to understand, I am using this for storing analytics and I would need to do somewhat large sets of key lookups.
我能想到的两个选项是pipelining multi gets + exec或者使用 Lua 脚本来避免通过网络发送一堆密钥.
The 2 options I can think of are pipelining multi gets + exec or using Lua scripting to avoid sending a bunch of keys over the wire.
如果有更好的方法来存储对象数据,并为其建立索引,或者有一种有效的方法来提取所有这些哈希值而不通过网络发送一堆 ID...请填写!
If there is some better way to store object data, and index it OR an efficient way to pull all of those hashes without sending a bunch of id's over the network... please fill me in!
编辑
我最终使用了 LUA 脚本(使用 redis 脚本分支)
I ended up using LUA scripting (using the redis scripting branch)
local fkeys = redis.call('sinter', unpack(KEYS))
local r = {}
for i, key in ipairs(fkeys) do
r[#r+1] = redis.call('hgetall',key)
end
return r
将所有处理保留在 DB 端.
Which keeps all processing on the DB side.
推荐答案
获取键列表,然后执行某些操作以获取值几乎是处理此问题的唯一方法.对于实验性 Lua 脚本来说,它看起来确实是一个很好的用例,尽管即使没有它,您也可以相当有效地获取密钥 - 在您看到真正的性能问题之前,数字需要非常大.
Getting the list of keys and then doing something to get the values is pretty much the only way to handle this. It does look like a good use case for the experimental Lua scripting, though even without that you can probably get the keys fairly efficiently - the numbers need to be really large before you see real performance issues.
您可能还可以进行其他优化,可能使用临时集或排序集,或者仅从每个散列中检索单个相关属性,但这些在很大程度上取决于您尝试检索的数据类型.
There are likely additional optimisations you can make, possibly using temporary or sorted sets or only retrieving a single relevant property from each hash, but these are highly dependent on the type of data you are trying to retrieve.
这篇关于Redis中高效的索引类型操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!