Suppose you have a hash 'users' whose entries map numeric IDs to JSON-encoded arrays, so, for instance, the integer 1 maps to the string {name: 'John', surname: 'Doe', occupation: 'plumber'}.
The numeric IDs of items in the hash are stored in various lists. Thus, if 'foobar' is one of these lists, to retrieve the actual data from it I would run a simple Lua script (implementing a server-side join operation). Or, as I've just learned, I could use something like
SORT foobar BY inexistent_key GET user:*
but that implies storing each user's data into a separate key which seems expensive (in my case I have many small collections so I want to take advantage of Redis compression of hashes).
The question is finally this: I need to keep these lists ordered alphabetically by, say, each user's surname, then by name. What is the best way to achieve this, without changing too much the underlying data model (if possible)?
So far the best I could think of was using the SORT command together with the BY and STORE clauses, that is, running
SORT foobar BY surname:* STORE foobar
whenever the list is changed, but that way I'd need many keys. If I could use a hash in the BY clause that would be the ideal solution, it seems to me.
If the fields I want to sort by were somehow limited (as in, just a couple hundred surnames) I could think of mapping strings to integers and use a Redis sorted set, but this doesn't seem to be the case.