【问题标题】:Realtime database data structure modeling实时数据库数据结构建模
【发布时间】:2019-06-08 22:23:28
【问题描述】:

我们有一个聊天系统,我们有一个分析仪表板。目前我们正在显示最上面所说的句子。模型如下:


messages
    --key1
       -text: "who are you"
    --key2
       -text: "hello"
    --key3
       -text: "who are you"

有一个数据库触发器,每次插入新消息时都会存储如下计数


stat
   --topPhrases
     --keyA
        --phrase: "who are you"
        --count: 2
     --key
        --phrase: "hello"
        --count: 1

我们的仪表板现在查询这些数据并在仪表板上显示为最常用的句子。

我们现在遇到的问题是我们需要向它添加日期元素。所以基本上目前这解决了回答“人们最喜欢说的句子”

我们现在要回答的是“今天、本周、本月最热门的句子”

因此,我们可能需要以不同的方式重新存储 stat 数据模型。请指教。

【问题讨论】:

  • 您可以在stat 中添加一个名为date 的字段,用于存储创建消息的时间。使用查询过滤时间戳并排序count
  • 听起来不正确。显示计数 2 的你是谁的短语可能出现在两个不同的日期。那么你将存储什么日期?

标签: firebase firebase-realtime-database


【解决方案1】:

常见的建议是存储您的应用需要显示的数据。因此,如果您想显示今天、本周和本月的热门句子,这意味着精确存储这些聚合:按天、周和月排列的热门句子。

存储这些的一个简单模型是保持您当前的状态,但是对于每个聚合级别和每个间隔:

stats
   --topPhrases
     --keyA
        --phrase: "who are you"
        --count: 2
     --key
        --phrase: "hello"
        --count: 1
   --topPhrases_byDay
     --20190607
        --keyA
           --phrase: "who are you"
           --count: 2
        --key
           --phrase: "hello"
           --count: 1
     --20190607
        --keyA
           --phrase: "who are you"
           --count: 2
        --key
           --phrase: "hello"
           --count: 1
   --topPhrases_byWeek
     --201922
        --keyA
           --phrase: "who are you"
           --count: 2
        --key
           --phrase: "hello"
           --count: 1
     --201923
        --keyA
           --phrase: "who are you"
           --count: 2
        --key
           --phrase: "hello"
           --count: 1
   --topPhrases_byMonth
     --201905
        --keyA
           --phrase: "who are you"
           --count: 2
        --key
           --phrase: "hello"
           --count: 1
     --201906
        --keyA
           --phrase: "who are you"
           --count: 2
        --key
           --phrase: "hello"
           --count: 1

或者,将所有聚合存储为单个列表,并使用前缀来指示它们的聚合级别(以及其余键的格式):

stats
   --topPhrases
     --keyA
        --phrase: "who are you"
        --count: 2
     --key
        --phrase: "hello"
        --count: 1
     day_20190607
        --keyA
           --phrase: "who are you"
           --count: 2
        --key
           --phrase: "hello"
           --count: 1
     day_20190608
        --keyA
           --phrase: "who are you"
           --count: 2
        --key
           --phrase: "hello"
           --count: 1
     week_201922
        --keyA
           --phrase: "who are you"
           --count: 2
        --key
           --phrase: "hello"
           --count: 1
     week_201923
        --keyA
           --phrase: "who are you"
           --count: 2
        --key
           --phrase: "hello"
           --count: 1
     month_201905
        --keyA
           --phrase: "who are you"
           --count: 2
        --key
           --phrase: "hello"
           --count: 1
     month_201906
        --keyA
           --phrase: "who are you"
           --count: 2
        --key
           --phrase: "hello"
           --count: 1

您肯定会在此处复制大量数据,但这些模型的优势在于现在向用户显示统计数据变得微不足道。这是 NoSQL 数据库的常见权衡,数据写入变得更加复杂,并且存储了更多(重复)数据,但它使读取数据变得微不足道,因此非常可扩展。

【讨论】:

  • 我关心这里的计算。对于发布的每条消息,我正在写入消息节点,然后读取和写入整体聚合,然后更多用于按天存储。并且随着日数据的每次更新,周、月和年的数据也会随之更新。
  • 确实是这个过程,但是不明白是什么问题。
  • 大声笑担心使用过多的计算,这直接意味着计费
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-28
  • 2018-05-28
  • 1970-01-01
  • 1970-01-01
  • 2017-12-11
  • 1970-01-01
  • 2020-01-20
相关资源
最近更新 更多