实时数据库数据结构建模 [英] Realtime database data structure modeling

查看:60
本文介绍了实时数据库数据结构建模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We have a chat system for which we have a analytics dashboard. currently we are showing the top said sentences. The model looks like below:


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

有一个数据库触发器,每当插入一条新消息时,它就会存储一个如下所示的计数

there is a database trigger that every time a new message gets inserted store a count like below


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

我们的仪表板现在查询此数据,并在仪表板上显示为使用的热门句子.

Our dashboard now queries this data and shows on dashboard as top sentences used.

我们现在遇到的问题是,我们需要向其添加date元素.因此,目前基本上,这可以解决人们曾经说过的最热门的句子"

The problem we have is now we need to add date element to it. So basically currently this solves to answer "top said sentences ever by people"

我们现在要回答的是今天,本周,本月最热门的句子"

What we now want to answer is "top said sentences today, this week, this month"

因此,我们可能需要以不同的方式重新存储统计数据模型.请告知.

So, we probably need to re store the stat data model differently. Please advise.

推荐答案

常见的建议是存储应用程序需要显示的数据.因此,如果要显示今天,本周和本月的热门句子,则意味着要精确地存储这些汇总:按天,周和月显示的热门句子.

The common recommendation is to store the data that you app needs to display. So if you want to display top sentences for today, for this week, and for this month, that means storing precisely those aggregates: the top sentences by day, week, and month.

存储这些数据的简单模型是保持当前状态,但随后针对每个聚合级别和每个时间间隔:

A simple model for storing these is to keep your current, but then for each aggregation level, and each interval:

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

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

Alternatively, store all aggregations as a single list, and use prefixes to indicate their aggregation level (and the format of the rest of the key):

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数据库的常见折衷方案,数据写入变得更加复杂,并且存储了更多(重复的)数据,但是读取数据变得微不足道,因此具有很高的可伸缩性.

You're definitely duplicating a lot of data here, but the advantage of these models is that displaying the stats to a user is now trivial. That's a common trade-off with NoSQL databases, writing of data is made more complex, and more (duplicate) data is stored, but it makes reading the data trivial, and thus very scalable.

这篇关于实时数据库数据结构建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆