【问题标题】:iOS Swift Firebase Database SchemaiOS Swift Firebase 数据库架构
【发布时间】:2018-03-16 16:02:31
【问题描述】:

我有一个需要存储这些对象的应用

User 
{
   auto-generated-id: String,
   name: String,
   schoolID: String,
   myClasses: String[]
}

School
{
   auto-generated-id: String,
   name: String,
   adress: String
}

Classes
{
  auto-generated-id: String,
  number: String,
  subject: String,
  schoolID: String
}

但是我不熟悉 NoSQL 结构,不知道如何存储这些对象以进行高效查询

我的应用将提供课程主题、班级编号、学校 ID

我需要的查询

1)

select * 
from School s, Classes c
where c.subject = 'ABC' and c.number = '100', and c.schoolID = "1234" 

2)

select c.*
from User u, Classes c, School s
where u.auto-generated-id = "1234" and u.schoolID = s.schoolID and c.schoolID = s.schoolID 

我可以重新排列我的模式以适应 nosql 方法并仍然执行我的查询的最佳方法是什么

由于我的结构不佳,我不断收到以下警告

使用未指定的索引。您的数据将在客户端下载和过滤。考虑将 ".indexOn": "classNum" 添加到您的安全规则的 somePath 以获得更好的性能

【问题讨论】:

  • 现在可以安全地忽略 .indexOn 错误。这是您需要稍后解决的性能调整错误,但目前对于很小的数据集,它不会产生太大影响。
  • 我喜欢这个问题,但您能否用查询 1 的所需输出非常简要地更新您的问题。即用例将有助于理解最终结果是什么。即学生想要任何教授 ABC 科目的学校的名称和地址。number 字段代表什么?

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

试试这个数据库结构:

Users
 userid
   name:userx
   email: userx@gmail.com
 userid
   name:usery
   email:usery@gmail.com

Schools
   pushid
     name:school1
     address: at *
   pushid
     name:school2
     address:ios section

Classes
   pushid
     number:1
     subject: Firebase
     schoolname: school1
   pushid
     number:2
     subject:ios development
     schoolname:school2

UserClass
  userid<---------if of userx
    randomid
      classname: Firebase
      schoolname: school1
  userid<--------------if of usery
    randomid
      classname:ios developement
      schoolname: school2

检查一下:

https://firebase.googleblog.com/2013/04/denormalizing-your-data-is-normal.html

在 Firebase 中没有 joinswhere clause。 你只能指定一个路径,所以不能使用两个表,只有一个顶部节点和通过其他节点,例如:child("Users").child(userid)

【讨论】:

  • 如果答案中包含一个示例查询,以这种结构解决了 OP 的查询 1),这可能对 OP 和未来的读者非常有帮助。
  • 已经4天了..OP没有真正回应或说什么..所以我不知道他是否使用它或它解决了他的问题