Home > Database > Redis > body text

What is redis index

(*-*)浩
Release: 2019-11-21 13:18:10
Original
6641 people have browsed it

What is redis index

#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 类型
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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;
Copy after login

The above is the detailed content of What is redis index. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template