Home  >  Article  >  Database  >  Introduction to relational query in redis

Introduction to relational query in redis

尚
forward
2020-04-18 09:05:202445browse

Introduction to relational query in redis

This article explains how Redis saves relational data and how to match, range, and fuzzy queries with examples.

1. Storage of relational data

Take the Staff object as an example. In a relational database or a memory grid product like GridGain (the bottom layer uses the memory mode storage of the H2 database), we Save the object's data in table form. Because the memory grid is cached based on objects, an additional column (Staff column) is needed to save the encoding of the entire object, such as serialized binary or JSON format, etc., so that it can be directly returned to the application for deserialization.

In Redis, we can use id as the unique identifier and save it using three data structures: key-value, hash, and zSet. Key-value is to save the id and the entire object. After determining the id range, the object can be returned to the client based on it.

Introduction to relational query in redis

2. Matching query

Using hget or hmget of the hash table can realize dept='IT' or dept in ('IT', 'QA' ) This is a single-value or multi-value exact match query. After getting the id list, query the key-value to get the object.

3. Range query

Because we save age as the score of zSet, and the value is the id, we can use the zrangeByScore method of zSet to obtain the value of the score within a certain interval.

4. Fuzzy query

After Redis 2.8.9, zSet added a very useful method zrangeByLex. We save the scores as 0 and the value is in the format of name:id. You can use zrangeByLex. Get the value of a letter within a certain range. For example, zrangeByLex name [A, (F, you can query Allen, Aaron, Carter.

5. Paging query

At the same time, zrangeByLex also supports paging query, and the syntax is similar to limit start, offset.

6. Limitations

The above examples illustrate how several common queries are implemented in Redis, but after all, Redis is only key-value storage, so it has many limitations.

For example:

1) It is impossible to implement a query with multiple conditions, such as age>25 AND name like 'A%'. If you want to implement it, you need multiple commands and calculate the union or intersection.

2) Fuzzy query in Chinese is more difficult.

For more redis knowledge, please pay attention to the redis introductory tutorial column.

The above is the detailed content of Introduction to relational query in redis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete