#redis does not directly support indexing and needs to be maintained by yourself.
For non-range unique indexes, we can simply save the index as a KV pair, and v can save the main key. For range retrieval, or non-unique indexes, redis must be used zset to achieve. (Recommended learning: Redis video tutorial)
Give an example of a traditional user system
uid 用户id name 用户名 credit 用户积分 type 类型
can be placed directly Retrieving
hmset usr:1 uid 1 name aaa credit 10 type 0 hmset usr:2 uid 2 name bbb credit 20 type 1
in a hashset by uid is very fast, but if you want to query users with type=1, you can only scan them all!
In a relational database, we can simply create an index on type
select * from usr where type=1
Such SQL can be executed efficiently. In redis, we need to maintain another zset
zadd usr.index.type 0 0:1 zadd usr.index.type 0 1:2
Note that all weights are set to 0, so that they can be retrieved directly by value, and then can be retrieved through
zrangebylex usr.index.type [1: (1;
The above is the detailed content of What is redis index. For more information, please follow other related articles on the PHP Chinese website!