Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design Redis database table like SQL?

Tags:

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

like image 732
Hari Haran Avatar asked Jan 25 '14 06:01

Hari Haran


People also ask

Can Redis do SQL?

Spark-Redis library allows you to use the DataFrame APIs to store and access Redis data. In other words, you can insert, update and query data using SQL commands, but the data is internally mapped to Redis data structures.

Do we have tables in Redis?

In Redis, a table doesn't map to a single structure, but rather a more flexible and precise group of structures. We've also took a look at how you would implement these commands in a Node.

Can I use Redis as a DB?

Not only that, you can use it as a multi-model primary database, enabling you to build modern applications, as well as low-latency microservice-based architectures, all on top of Redis.

How do I create a DB in Redis?

You don't create a database in Redis with a command - the number of databases is defined in the configuration file with the databases directive (the default value is 16). To switch between the databases, call SELECT . Show activity on this post. select 1 select 2 ...


1 Answers

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

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 student:xxx
(integer) 1
redis 127.0.0.1:6379> smembers college:nnn
1) "student: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.

like image 197
sberry Avatar answered Oct 17 '22 21:10

sberry