像SQL一样设计Redis数据库表? [英] Design Redis database table like SQL?
问题描述
假设我的数据库表结构是这样的
Suppose my database table structure is like this
id name college address
1 xxx nnn xn
2 yyy nnm yn
3 zzz nnz zn
如果我想像这样根据 sql 中的名称获取学生详细信息select * from student where name = 'xxx'那么它是如何在 redis 数据库中实现的
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
推荐答案
Redis 与其他 NoSQL 数据存储库一样,根据您要执行的操作有不同的要求.
Redis, like other NoSQL datastores, has different requirements based on what you are going to be doing.
Redis 有多种数据结构,可以根据您的需要使用.例如,考虑到您希望 select * from student where name = 'xxx'
,您可以使用 Redis hash
.
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 127.0.0.1:6379> hmset student:xxx id 1 college nnn address xn
OK
redis 127.0.0.1:6379> hgetall student:xxx
1) "id"
2) "1"
3) "college"
4) "nnn"
5) "address"
6) "xn"
如果您有其他查询,例如您想做同样的事情但选择 where College = 'nnn'
那么您将不得不对数据进行非规范化.非规范化在 SQL 中通常是一件坏事,但在 NoSQL 中却很常见.
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.
如果您的主要查询将针对名称,但您可能需要针对学院进行查询,那么您可以执行一些操作,例如在哈希之外添加 set
.
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 set
in addition to the hashes.
redis 127.0.0.1:6379> sadd college:nnn student:xxx
(integer) 1
redis 127.0.0.1:6379> smembers college:nnn
1) "student:xxx"
对于这样结构的数据,如果你想找到所有关于大学 xn 的名字的信息,你应该首先选择 set
,然后选择每个基于 hash
在 set
中返回的名称上.
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 hash
based on the name returned in the set
.
您的要求通常会推动您使用的设计和结构.
Your requirements will generally drive the design and the structures you use.
这篇关于像SQL一样设计Redis数据库表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!