Use of secondary indexes in a redis database in comparison with SQL statements


I’m working with a redis database. I have already implemented Python code to access the redis server. The problem is that the code implemented is very complex and it is not easy maintainable.

Secondary indexes in Redis database

To simplify the question I suppose that in my database are present a set of 4 keys inserted by the following commands:

hset key:1 id 1 field1 1001
hset key:2 id 2 field1 999
hset key:3 id 3 field1 1002
hset key:4 id 4 field1 1000

Previous set of keys is ordered by the field id. I have used the Secondary indexing guide of the Redis documentation to implement a secondary index to get the keys list ordered by field1.
To do this, according with the guide, I have created a sorted set called zfield1 inside the database by the following commands:

zadd zfield1 1001 1
zadd zfield1 999 2
zadd zfield1 1002 3
zadd zfield1 1000 4

The sorted set zfield1 is ordered by the field field1.

With the command zrange I get the list of id field ordered by field1:

zrange zfield1 0 -1
1) "2"
2) "4"
3) "1"
4) "3"

The first element of the list obtained by zrange is "2" and this element provides the information to get all the value of the key with the lower field1 value. So by the following command I can get all key values relatives to key:2:

hgetall key:2
1) "id"
2) "2"
3) "field1"
4) "999"

With a suited loop that executes the command hgetall, I can get all the key values ordered by field1.

Compare with a SQL database

I think that the previous presentation is the implementation of the following SQL query (where TABLE1 is a table in a generic SQL database):

SELECT * from TABLE1 order by field1

This is the first time that I use Redis and if I compare it to the SQL query I think that its usage it is more complex respect of a SQL database.
So I have the doubt that there are other more simple ways to implement a SQL query as SELECT * from TABLE1 order by field1 with Redis.


Someone could tell if there are other Redis commands (for example a particular use of the Redis command KEYS) that help to get keys ordered by a secondary index?

Note: Useful links on this topic are also welcome.

Asked By: frankfalse



This is the first time that I use Redis and if I compare it to the SQL query I think that its usage it is more complex respect of a SQL database

Indeed: Redis’ main goal is performance and its data structures and commands are designed with that in mind. There are no native secondary indexes in Redis, as keeping one would have a non-negligible cost: in fact, the guide you referenced shows a pattern you can use to mimic one, as the data type used there is just a sorted set – which is a first citizen in the Redis ecosystem. A relational database is a completely different beast.

If you wish to use multiple indexes in Redis then I would suggest creating and maintaining multiple keys (using the aforementioned sorted set data type would be okay) while you create/modify/delete your main keys.

Answered By: Efran Cobisi
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.