- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
实际上我的 firestore 集合看起来像这样:
user :
|-> 0000 (uid)
|-> avatar : 'url'
|-> name : 'josh'
|-> 1111
|-> avatar : 'url'
|-> name : 'steve'
[...]
follow :
|-> 0000
|-> 1111 : true
|-> 8888 : true
[...]
message :
|-> 0000
|-> 8888
|-> 1264978800 (timestamp)
|-> message = "hello"
|-> 1264978987
|-> message = "How are you"
|-> 8888
|-> 0000
|-> 1264914253
|-> message = "hey dude "
|-> 1264975895
|-> message = "fine and you?"
如果我想获取 0000 和 8888 之间的个人资料和对话(例如获取他们的头像),我需要:
如果我想要0000的所有对话的列表,我需要这样做:
这是非常简单的查询,例如,这看起来比 mysql 中的查询更繁重。
有办法不做所有这些查询吗?我的数据库模式好不好?
感谢您的帮助。
最佳答案
是时候考虑将 NoSQL 与 Firebase 结合使用了。 Firestore 是一个性能至关重要的实时数据库。您的数据结构适合 SQL 数据库,而不适合 NoSQL。
以下是我为此用例建议的数据结构:
user :
|-> 0000 : //uid
|-> id : '0000'
|-> avatar : 'url'
|-> name : 'josh'
|-> follow : ['1111', '8888'] // Array of uid
|-> 1111 : //uid
|-> id : '1111'
|-> avatar : 'url'
|-> name : 'steve'
|-> follow : ['0000'] // Array of uid
[...]
message :
|-> AAAA // message id
|-> id : 'AAAA'
|-> sender
|-> id: '0000'
|-> avatar: 'url'
|-> name: 'josh'
|-> receiver
|-> id: '8888'
|-> avatar: 'url'
|-> name: 'paul'
|-> time : 1264978800 // timestamp
|-> message : "hello"
|-> BBBB // message id
|-> id : 'BBBB'
|-> sender
|-> id: '8888'
|-> avatar: 'url'
|-> name: 'paul'
|-> receiver
|-> id: '0000'
|-> avatar: 'url'
|-> name: 'josh'
|-> time : 1264978800 // timestamp
|-> message : "hey dude"
|-> CCCC // message id
|-> id : 'CCCC'
|-> sender
|-> id: '0000'
|-> avatar: 'url'
|-> name: 'josh'
|-> receiver
|-> id: '8888'
|-> avatar: 'url'
|-> name: 'paul'
|-> time : 1264978800 // timestamp
|-> message : "How are you"
|-> DDDD // message id
|-> id : 'DDDD'
|-> sender
|-> id: '8888'
|-> avatar: 'url'
|-> name: 'paul'
|-> receiver
|-> id: '0000'
|-> avatar: 'url'
|-> name: 'josh'
|-> time : 1264978800 // timestamp
|-> message : "fine and you?"
以上结构是按照NoSQL方式设计的。用户的个人资料被重复多次。通过重复数据来提高性能并降低带宽和 CPU 成本,而这些成本比存储成本高得多。
好处:
现在,让我们看看您的用例:
If I want to get the profile and the conversation between 0000 and 8888 (to get they avatar for exemple), I need to:
- check if their friend
- if yes, get messages of 0000
- get messages of 8888
- get 0000 profile
- get 8888 profile
查询消息
,其中发送者
或接收者
位于follow
中。
If I want a list of all conversation of 0000, I need to do:
- check all user to know if they're friend
- for each friend, get profile
- for each friend, get list of message.
查询消息
,其中发送者
或接收者
等于'0000'
您可能会遇到的其他用例:
您可以使用follow
的 map 数组来存储uid
、name
和avatar
,甚至可以制作如果您打算对其执行多项操作,请作为子集合关注。
与发送的消息数量相比,更改姓名或头像的频率通常较低,并且可能不需要实时更改。如果需要更新所有消息,可以创建一个云函数来批量更新所有消息
说了这么多,显然您可以选择使用 SQL 还是 NoSQL。两者各有优缺点
关于javascript - 有没有办法在 firebase 中减少请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61687711/
我是一名优秀的程序员,十分优秀!