V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
tioover
V2EX  ›  Redis

使用诸如Redis的NoSQL该如何设计结构?

  •  
  •   tioover ·
    tioover · 2011 年 7 月 13 日 · 11478 次点击
    这是一个创建于 5298 天前的主题,其中的信息可能已经有所发展或是发生改变。
    最近对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 条回复    1970-01-01 08:00:00 +08:00
    27493586
        1
    27493586  
       2011 年 7 月 13 日
    如果是我的话我会这么设计

    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
    tioover
        2
    tioover  
    OP
       2011 年 7 月 14 日
    @27493586 谢了~看来就是这样……
    reus
        3
    reus  
       2011 年 7 月 14 日
    在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
    27493586
        4
    27493586  
       2011 年 7 月 14 日
    @reus 学习了,谢谢。Hash还没用过呢,我一读完 http://redis.io/topics/data-types-intro 就跑去用了。。。那文档发表的时候还没加入Hash。

    请问还有别的文章或者学习资料吗?
    reus
        5
    reus  
       2011 年 7 月 14 日
    xwsoul
        6
    xwsoul  
       2012 年 6 月 12 日
    因为是NoSQL不存在结构化...却问准确的说法应该是Key如何设计以及数据类型如何选择....
    xwsoul
        7
    xwsoul  
       2012 年 6 月 12 日
    当然,这个问题我也是想问的...Orz
    qq286735628
        8
    qq286735628  
       2012 年 6 月 12 日   ❤️ 1
    http://coolshell.cn/articles/7270.html
    这篇文章可以研究一下
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   5432 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 09:29 · PVG 17:29 · LAX 01:29 · JFK 04:29
    ♥ Do have faith in what you're doing.