This topic created in 5425 days ago, the information mentioned may be changed or developed.
最近对Redis有点兴趣,但是刚刚接触NoSQL很多地方不懂,搜索也搜索不出来,虽然自己目前也用不到复杂的结构,仅仅是个消息队列,但是都说Redis不仅仅能当消息队列,所以有点疑惑,一个数据库的结构该怎么做呢。
比如要一个Blog在SQL里大概是这样(主键省略)
-blog
--post
---content
---author
---date
--author
---name
--commet
---name
---email
---date
在Redis里我的想法大概是下面这样的
比如post对应post_list,post_list[0] 里面有content键author键date键
像这样。
一般是不是就是这样?还是说有更好的方法?或者说一般都只用作消息队列?
8 replies 1970-01-01 08:00:00 +08:00  | | 1 27493586 Jul 13, 2011 如果是我的话我会这么设计
post:[post_id]:author -> author_id post:[post_id]:comments -> list of comment_id post:[post_id]:date post:[post_id]:content
comment:[comment_id]:author -> 注册用户存author_id comment:[comment_id]:name -> 匿名用户留名 comment:[comment_id]:content comment:[comment_id]:date comment:[comment_id]:email
author:[author_id]:posts -> set of post_id author:[author_id]:comments -> set of comment_id author:[author_id]:name |
 | | 3 reus Jul 14, 2011 在redis里面,key是很占空间的,像二楼那样空间效率是很低的,不应该直接k-v,而是用hash HASH post:author [post_id] -> author_id HASH post:comments [post_id] -> packed list of comment_id HASH post:date [post_id] ... 总之用字段名作为hash名,id作为hash的键,可以节省大量空间。这对于redis这样的内存数据库来说是比较重要的。 如果值是集合类型,那就打包一下,对性能没有影响
还有另外一种存储方式,比上面一种占空间略多,但是可以减少一些请求,性能要好些 就是每个post,comment,author作为hash存储,以post:[post_id],comment:[comment_id],author:[author_id]为hash名称,以字段名(author, comments等)作为hash的key。 这种方式可以用一条指令来完成读写,HMSET或者HGETALL/HMGET,上面那种就需要多条HSET/HGET |
 | | 6 xwsoul Jun 12, 2012 因为是NoSQL不存在结构化...却问准确的说法应该是Key如何设计以及数据类型如何选择.... |
 | | 7 xwsoul Jun 12, 2012 当然,这个问题我也是想问的...Orz |