我们有一个聊天系统,我们有一个分析仪表板。目前我们正在显示上面所说的句子。该模型如下所示:
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 数据模型。请指教。
常见的建议是存储您的应用程序需要显示的数据。因此,如果您想显示今天、本周和本月的热门句子,这意味着精确存储这些聚合:按天、周和月排列的热门句子。
存储这些的一个简单模型是保持您的当前状态,但是对于每个聚合级别和每个间隔:
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 数据库的一个常见权衡,数据写入变得更加复杂,并且存储了更多(重复)数据,但它使得读取数据变得微不足道,因此非常具有可扩展性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句