我试图通过遵循控制台日志中的建议来创建索引,以提高Firebase Realtime数据库的性能。大多数建议都很容易遵循,但并非全部。
我现有的规则:
"notes": {
".indexOn": ["data/title", "access/author"],
".read": "
auth.uid !== null
",
"$note_id": {
".write": "
(!data.exists() && auth.uid !== null) ||
(
data.child('access').child('author').val() === auth.uid
||
data.child('access/members').child(auth.uid).exists()
)
",
"data": {
".read": "
data.parent().child('access').child('author').val() === auth.uid ||
data.parent().child('access/members').child(auth.uid).exists()
",
".write": "
data.parent().child('access').child('author').val() === auth.uid ||
data.parent().child('access/members').child(auth.uid).exists()
",
"access" : {
".read" : "
(auth.uid !== null) &&
(
data.child('author').val() === auth.uid
||
data.child('members').child(auth.uid).exists()
)
",
"members" :{
".write" : "
(!data.exists() && auth.uid !== null) ||
data.parent().child('author').val() === auth.uid ||
data.parent().child(auth.uid).exists()
"
}
}
}
},
一些建议针对以用户uid
结尾的位置-与以下控制台日志类似:
FIREBASE警告:使用未指定的索引。
考虑在/ notes上添加“ .indexOn”:“ access / members / 3weT1tYhG456HH0CuC45Muc44ax6” 遵守您的安全规则以获得更好的性能
是否可以在Firebase Realtime数据库中添加此索引规则-考虑到位置位置以用户uid
字符串结尾?
答案 0 :(得分:1)
您当前的数据结构使查找特定注释的成员变得容易。查找特定成员的注释并不容易。为了允许第二个用例并确保其可扩展,您将需要添加其他数据结构:
user_notes: {
user_id_1: {
note_id1: true,
note_id2: true,
note_id3: true
},
user_id_2: {
note_id3: true,
note_id4: true,
note_id5: true
}
}
通过这种附加结构,您可以确定用户无需查询即可访问的节点。
这当然意味着您在允许用户访问便笺时需要写到两个位置,但这是很常见且可扩展的,并且仍然可以使用规则进行保护。
另请参阅: