开发者

Efficient index type operations in Redis

开发者 https://www.devze.com 2023-03-14 02:23 出处:网络
I am trying to create a set of indexe开发者_如何学JAVAs in Redis, for doing AND operations. like this:

I am trying to create a set of indexe开发者_如何学JAVAs in Redis, for doing AND operations.

like this:

inx:haircolor:blonde = set(key1,key2,key3)

inx:eyecolor:blue = set(key1,key2)

And I can use sinter to find all keys with Blond hair and blue eyes.

I have Hashes like this:

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.

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.

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!

Edit

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

Which keeps all processing on the DB side.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消