[redis] secondary index

Simple numeric index

Example:

HMSET user:1 id 1 username antirez ctime 1444809424 age 38
HMSET user:2 id 2 username maria ctime 1444808132 age 42
HMSET user:3 id 3 username jballard ctime 1443246218 age 33

In the above example, it is very convenient to query each key in each user. However, what if we want to query users aged 33-38?

Secondary indexes come in handy.

add to:

ZADD user.age.index 38 1
ZADD user.age.index 42 2
ZADD user.age.index 33 3

use:

zrangebyscore user.age.index 33 38

Query result 3 1

Do you feel fooled at the moment? Isn't this the sorted set?

Yes, this is the principle of index implementation. Isn't mysql similar? You can also add several secondary indexes to redis, such as maintaining another user No. index to query according to the number.

Of course, the index is not limited to using sorted set, nor can it only retrieve one field, such as GEO in the official website example.

Dictionary index

The score of the ordered set index can only store numbers, but the value can be a string. When the scores are the same, zrangebylex is sorted in the order of the score dictionary. For the ordered set index on the official website, I don't think it's a chicken's rib. I mainly focus on using value as the combined index.

HMSET user:1 id 1 username antirez grade 2 gender girl
HMSET user:2 id 2 username maria grade 1 gender girl
HMSET user:3 id 3 username jballard grade 2 gender boy
HMSET user:4 id 4 username kaka grade 1 gender boy

You need to query the girls in grade 2. The added index is as follows

Zadd myindex grade 0: Gender: id

zadd myindex 0 2:girl:1
zadd myindex 0 1:girl:2
zadd myindex 0 2:boy:3
zadd myindex 0 1:boy:4

Query:

zrangebylex myindex [2:girl +

The query result is 2:girl:1, and the last segment is id.

Is it more like mysql index? redis officials also think it is similar to B-tree

A little note: in dictionary sorting, it is compared string by string, not by number. When the front is quite long, it is relatively large. Therefore, even if your index fields are all numbers, the result set IDS found are not in any order. For example, it thinks that 12 < 129 < 76 < 9. You should pay attention to the strict requirements on the result order.

hexastore

redis uses it to support the direct representation of graphs. In fact, it is still the concept of ordered sets, but it uses "hexagon" to store all relationships

It has the concepts of Subject, Predicate and Object. For example, Kaka was a player of AC Milan. It took six sentences to save from multiple angles

zadd myindex 0 spo:kaka:is-member-of:ACmilan
zadd myindex 0 sop:kaka:ACmilan:is-member-of

zadd myindex 0 ops:ACmilan:is-member-of:kaka
zadd myindex 0 osp:ACmilan:kaka:is-member-of

zadd myindex 0 pos:is-member-of:ACmilan:kaka
zadd myindex 0 pso:is-member-of:kaka:ACmilan

When you need to query the team Kaka played for:

zrangebylex myindex "[spo:kaka:is-member-of:" "[spo:kaka:is-member-of:\xff"

#or
zrangebylex myindex "[pso:is-member-of:kaka:" "[pso:is-member-of:kaka:\xff"

It is similar when you need to query the players who have played for AC Milan

In this example, it seems that you don't have to save six times. Three times is enough. After all, there is no direction involved. In fact, this is just a simple example. Kaka: is friend of: Cr and Kaka: is citizen of: Brazil will be used in real projects. You still need to save all the information of the card or all the k-v s with is member of and is friend of relationships.

Of course, redis can not only use ordered sets as indexes, but also hash and list can be combined properly.

The official also gives a composite index to retrieve multi-dimensional indicators. Obviously, the official never thinks that they can only do simple caching.

index upgrade

For all indexes, you need to remember to maintain them when adding, deleting or modifying data in the future. In order to ensure atomicity, you can use lua. Of course, officials also pointed out that various problems such as network partition will cause inconsistent indexes, and it is necessary to regularly check the data in the index.

reference resources:

https://redis.io/topics/indexes

https://www.zybuluo.com/Rays/note/1079251

https://github.com/antirez/redimension

Keywords: Redis

Added by slimjim on Sat, 15 Jan 2022 23:52:36 +0200