为聊天应用程序构建 NoSQL 数据库(使用 FireBase) [英] Structure a NoSQL database for a chat application (using FireBase)

查看:28
本文介绍了为聊天应用程序构建 NoSQL 数据库(使用 FireBase)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于多年使用关系数据库,我正在尝试使用 FireBase 开发一个非常基本的聊天/消息传递应用程序

FireBase 使用 NoSQL 数据结构方法,使用 JSON 格式的字符串.

为了了解如何在考虑性能的情况下构建数据库,我进行了大量研究.我试图非规范化"结构并最终得到以下结果:

<代码>{聊天":{1":{10":{对话 ID":x123332"},17":{对话 ID":x124442"}}},对话":{x123332":{消息1":{时间":12344556,"text" : "你好,你好吗?",用户 ID":10},消息2":{时间":12344560,文本":好",用户 ID":1}}}}

数字 1、10、17 是示例用户 ID.

我的问题是,这可以以更好的方式组织吗?目标是随着应用用户的增长而扩大规模,同时仍能获得最佳性能.

解决方案

使用面向文档的数据库结构这样的Firestore,您可以将对话存储如下;

<代码>{聊天室":[{cid":100,"成员":[1, 2],消息":[{"from":1, "to":2, "text":"Hey Dude!带上它"},{"from":2, "to":1, "text":"Sure man"}]},{cid":101,"成员":[3, 4],消息":[{"from":3, "to":4, "text":"我可以做这个工作"},{"from":4, "to":3, "text":"那么我们可以继续"}]}]}


可以通过这种结构运行的 NoSQL 查询示例很少.

<块引用>

获取用户ID为1的登录用户的所有对话.

db.chat_rooms.find({ members: 1 })

<块引用>

获取用户id为1的所有文档、消息.

db.chat_rooms.find({ messages: { from: 1 } })

上述数据库结构也能够在RDMS数据库中使用MySQL或MSSQL实现表关系.这也可以用于群聊室应用程序.

此结构经过优化,可减少您的数据库文档读取使用量,从而节省您为基础设施支付更多费用的资金.

仍然根据我们上面的示例,您将获得 2 次文档读取,因为我们有 4 条消息,但是如果您单独存储所有消息并通过过滤发件人 ID 运行查询,您将获得 4 次数据库查询,这是一种海量当您的数据库中有大量对话历史记录时的数量.

Coming from years of using relational databases, i am trying to develop a pretty basic chat/messaging app using FireBase

FireBase uses a NoSQL data structure approach using JSON formatted strings.

I did a lot of research in order to understand how to structure the database with performance in mind. I have tried to "denormalize" the structure and ended up with the following:

{

"chats" : {

    "1" : {
        "10" : {
            "conversationId" : "x123332"
         },
        "17": {
            "conversationId" : "x124442"
        }
    }
},

"conversations" : {

    "x123332" : {

      "message1" : {

        "time" : 12344556,
        "text" : "hello, how are you?",
        "userId" : 10
      },
      "message2" : {

        "time" : 12344560,
        "text" : "Good",
        "userId" : 1
      }
    }
  }
}

The numbers 1, 10, 17 are sample user id's.

My question is, can this be structured in a better way? The goal is to scale up as the app users grow and still get the best performance possible.

解决方案

Using the document-oriented database structure such Firestore, you can store the conversations as below;

{
   "chat_rooms":[
      {
         "cid":100,
         "members":[1, 2],
         "messages":[
           {"from":1, "to":2, "text":"Hey Dude! Bring it"},
           {"from":2, "to":1, "text":"Sure man"}
          ]
      },
      {
         "cid":101,
         "members":[3, 4],
         "messages":[
           {"from":3, "to":4, "text":"I can do that work"},
           {"from":4, "to":3, "text":"Then we can proceed"}
          ]
      }
   ]
}


Few examples of NoSQL queries you could run through this structure.

Get all the conversations of a logged-in user with the user id of 1.

db.chat_rooms.find({ members: 1 })

Get all the documents, messages sent by the user id of 1.

db.chat_rooms.find({ messages: { from: 1 } })

The above database structure is also capable of implementing in RDMS database as table relationships using MySQL or MSSQL. This is also can be implemented for group chat room applications.

This structure is optimized to reduce your database document reading usage which can save your money from paying more for infrastructure.

According to our above example still, you will get 2 document reads since we have 4 messages but if you store all the messages individually and run the query by filtering sender id, you will get 4 database queries which are the kind of massive amount when you have heavy conversation histories in your database.

这篇关于为聊天应用程序构建 NoSQL 数据库(使用 FireBase)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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