Design Redis database table like SQL?

2019-02-06 19:02发布

问题:

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

回答1:

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

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 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.

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 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 hash based on the name returned in the set.

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



回答2:

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:

  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.

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.)



回答3:

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

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

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



回答4:

Redis just has some basic data structures with it, NoSQL and SQL are different worlds. But You can use Redis like some schemed SQL data store. There are funny program Redisql on github which try to play with Redis via SQL, and the idea behind Redisql is such that @sberry mentioned.



回答5:

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



标签: sql redis nosql