SQL 像SQL一样设计Redis数据库表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21347437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:52:54  来源:igfitidea点击:

Design Redis database table like SQL?

sqlredisnosql

提问by Hari Haran

Suppose my database table structure is like this

假设我的数据库表结构是这样的

id name college address
1  xxx   nnn     xn
2  yyy   nnm     yn
3  zzz   nnz     zn

If i want to get the student details based on the name in sql like this select * from student where name = 'xxx' so how its is possible in redis database

如果我想根据这样的 sql 中的名称获取学生详细信息 select * from student where name = 'xxx' 那么它在 redis 数据库中是如何可能的

回答by sberry

Redis, like other NoSQL datastores, has different requirements based on what you are going to be doing.

Redis 与其他 NoSQL 数据存储库一样,根据您要执行的操作有不同的要求。

Redis has several data structures that could be useful depending on your need. For example, given your desire for a select * from student where name = 'xxx'you could use a Redis hash.

Redis 有多种数据结构,可以根据您的需要使用它们。例如,鉴于您对 a 的渴望,select * from student where name = 'xxx'您可以使用 Redis hash

redis 127.0.0.1:6379> hmset xxx id 1 college nnn address xn
OK
redis 127.0.0.1:6379> hgetall xxx
1) "id"
2) "1"
3) "college"
4) "nnn"
5) "address"
6) "xn"

If you have other queries though, like you want to do the same thing but select on where college = 'nnn'then you are going to have to denormalize your data. Denormalization is usually a bad thing in SQL, but in NoSQL it is very common.

如果您有其他查询,比如您想做同样的事情但选择 onwhere college = 'nnn'那么您将不得不对数据进行非规范化。非规范化在 SQL 中通常是一件坏事,但在 NoSQL 中它很常见。

If your primary query will be against the name, but you may need to query against the college, then you might do something like adding a setin addition to the hashes.

如果您的主要查询将针对名称,但您可能需要针对学院进行查询,那么您可能会执行一些操作,例如set在散列之外添加一个。

redis 127.0.0.1:6379> sadd college.nnn xxx
(integer) 1
redis 127.0.0.1:6379> smembers college.nnn
1) "xxx"

With your data structured like this, if you wanted to find all information for names going to college xn, you would first select the set, then select each hashbased on the name returned in the set.

使用这种结构的数据,如果您想找到所有 xn 上大学姓名的信息,您可以首先选择set,然后hash根据set.

Your requirements will generally drive the design and the structures you use.

您的要求通常会推动您使用的设计和结构。

回答by Mehmet Kaplan

With just 6 principles (which I collected here), it is very easy for a SQL minded person to adapt herself to Redis approach. Briefly they are:

只需 6 条原则(我在此收集),对于一个有 SQL 头脑的人来说,很容易让自己适应 Redis 方法。简而言之,它们是:

  1. The most important thing is that, don't be afraid to generate lots of key-value pairs. So feel free to store each row of the table in a different key.
  2. Use Redis' hash map data type
  3. Form key name from primary key values of the table by a separator (such as ":")
  4. Store the remaining fields as a hash
  5. When you want to query a single row, directly form the key and retrieve its results
  6. When you want to query a range, use wild char "*" towards your key.
  1. 最重要的是,不要害怕生成大量的键值对。因此,请随意将表的每一行存储在不同的键中。
  2. 使用 Redis 的哈希映射数据类型
  3. 由表的主键值形成的键名由分隔符(如“:”)
  4. 将剩余的字段存储为哈希
  5. 当你想查询单行时,直接形成键并检索其结果
  6. 当您要查询范围时,请对您的键使用通配符“*”。

The link just gives a simple table example and how to model it in Redis. Following those 6 principles you can continue to think like you do for normal tables. (Of course without some not-so-relevant concepts as CRUD, constraints, relations, etc.)

该链接仅提供了一个简单的表示例以及如何在 Redis 中对其进行建模。遵循这 6 条原则,您可以继续像处理普通表一样思考。(当然没有一些不那么相关的概念,如 CRUD、约束、关系等)

回答by Siscia

For plain, vanilla redis the other answers are completely correct, however, yesterday (02 - December - 2016) redis 4-rc1 is been released.

对于普通的、vanilla redis,其他答案是完全正确的,但是,昨天(2016 年 12 月 - 2016 年)redis 4-rc1 发布了。

redis v4 provides support for modules and I just wrote a small module to embed SQLite into redis itself; rediSQL.

redis v4 提供了对模块的支持,我只是写了一个小模块来将 SQLite 嵌入到 redis 中;RedisSQL

With that module you can actually use a fully functional SQL database inside your redis instace.

使用该模块,您实际上可以在 redis 实例中使用功能齐全的 SQL 数据库。

回答by u2086105

Redisjust has some basic data structures with it, NoSQL and SQL are different worlds. But You can use Redislike some schemed SQL data store. There are funny program Redisqlon githubwhich try to play with Redis via SQL, and the idea behind Redisqlis such that @sberry mentioned.

Redis只是有一些基本的数据结构,NoSQL 和 SQL 是不同的世界。但是您可以像使用某些计划的 SQL 数据存储一样使用Redisgithub上有一个有趣的程序Redisql试图通过 SQL 来玩 Redis,而Redisql背后的想法就是@sberry 提到的。

回答by Tharanga

You can try searchboxframework. searchbox provides easy way for querying redis data with its Criteria api.

您可以尝试搜索框框架。searchbox 提供了使用其 Criteria api 查询 redis 数据的简单方法。

回答by Kris Zhang

OnceDB is a full-text search in-memory database based on Redis. It supports data management like SQL relational databases and NoSQL schemaless databases.

OnceDB是一个基于Redis的全文搜索内存数据库。它支持 SQL 关系数据库和 NoSQL 无模式数据库等数据管理。

OnceDB does not change the data storage structure of Redis and is fully compatible with Redis. Redis database files can be directly operated in OnceDB and then returned to Redis for use.

OnceDB不改变Redis的数据存储结构,完全兼容Redis。Redis数据库文件可以直接在OnceDB中操作,然后返回Redis使用。

OnceDB automatically creates auxiliary indexes through operators:

OnceDB 通过操作符自动创建辅助索引:

= Ordinary field value, no index
@ Primary key
? Grouping index
* Keyword grouping index, separated by ',' between keywords
\ Sort index, the score weight of the index is the value of the field

for example, execute the following command to add the user data:

例如,执行以下命令添加用户数据:

upsert user username @ dota password = 123456 title ? SDEI skills * java,go,c
> OK

you can search from the index by an operator, such as searching user data containing the c keyword, and printing the username and password fields.

您可以通过操作符从索引中搜索,例如搜索包含 c 关键字的用户数据,以及打印用户名和密码字段。

find user 0 -1 username = * password = * skills * c
find user 0 -1 username = * password = * skills * c
1) (integer) 1
2) "user:dota"
3) "dota"
4) "123456"
5) "java,go,c"

Read more:OnceDB quick start

阅读更多:OnceDB 快速入门

回答by cscan

Hope it is not too late since the original question is long for six year of time. You may try my dbx plugin: https://github.com/cscan/dbxWhich support the simple SQL to maintain the hashes in REDIS. Something like this:

希望现在还为时不晚,因为最初的问题已经持续了六年。你可以试试我的 dbx 插件:https: //github.com/cscan/dbx它支持简单的 SQL 来维护 REDIS 中的哈希值。像这样的东西:

127.0.0.1:6379> dbx.select name, tel from phonebook where gender = 'F' order by age desc

or calling from shell

或从外壳调用

$ redis-cli "dbx.select name, tel from phonebook where gender = 'F' order by age desc" 

Hope this help.

希望这有帮助。