有许多基于 'WHERE' 的听众是否可以接受
Is it acceptable to have many 'WHERE' based listeners
我正在开发一个需要有效监听以下伪查询的实现:
SELECT * FROM 'trips' WHERE 'travelers' IS ONE OF [ x,y,z ]
由于 firebase 不允许 OR 查询,我目前的解决方案是创建 trips x travellers 的所有可能排列并听取所有排列。
// Returns an array of {}
const permutations = ( locs, uids ) => {
let perms = locs.map( location => {
return uids.map( uid => ( { location: location, uid: uid } ) )
} )
return [].concat( ...perms )
}
// Trip destinations
let destinations = [ 'Amsterdam', 'Berlin' ]
// UIDs of travelers (in reality unique hashes)
let uids = [ 'John', 'Mary' ]
// Listeners
return Promise.all( permutations( locations, friends ).map( ( { location, uid } ) => {
this.db.collection( 'trips' )
.where( 'destination', '==', location )
.where( 'traveler.uid', '==', uid )
.onSnapshot( snapshot => {
Promise.resolve( snapshot.docs )
// Filter out empties
.then( docs => docs.map( doc => doc.exists ? doc : false ) )
.then( docs => docs.filter( doc => doc != false ) )
// Grab data from docs
.then( docs => docs.map( doc => ( { ...doc.data(), id: doc.id } ) ) )
// Call a callback (used int he bigger scheme of a redux listener)
.then( callback )
} )
} ) )
} )
现在这会导致一些听众,但如果 destination/uids 数组变大,这可能会导致大量听众。
我也可以 运行 手动搜索触发器,但我喜欢使用实时更新。
在 Firebase 中是否存在潜在的大量听众问题?是性能还是成本?
您必须更具体地说明 "bazillion"。一般来说,听众很便宜,你不应该太担心它。但是任何事情都有一个实际限制,我想你会在接近你的假设 "bazillion".
时发现它是什么。
1 个听众不会调用与 10 个或 100 个或 1000 个听众不同的计费。您可以预期单个侦听器的计费是线性扩展的。对于在所有查询之间都不同的每个文档,每个文档的成本都将完全相同。
每个侦听器还共享一个连接,因此您不必担心 运行 打开的文件描述符或类似的问题。实际上,您的限制是内存和带宽。
我正在开发一个需要有效监听以下伪查询的实现:
SELECT * FROM 'trips' WHERE 'travelers' IS ONE OF [ x,y,z ]
由于 firebase 不允许 OR 查询,我目前的解决方案是创建 trips x travellers 的所有可能排列并听取所有排列。
// Returns an array of {}
const permutations = ( locs, uids ) => {
let perms = locs.map( location => {
return uids.map( uid => ( { location: location, uid: uid } ) )
} )
return [].concat( ...perms )
}
// Trip destinations
let destinations = [ 'Amsterdam', 'Berlin' ]
// UIDs of travelers (in reality unique hashes)
let uids = [ 'John', 'Mary' ]
// Listeners
return Promise.all( permutations( locations, friends ).map( ( { location, uid } ) => {
this.db.collection( 'trips' )
.where( 'destination', '==', location )
.where( 'traveler.uid', '==', uid )
.onSnapshot( snapshot => {
Promise.resolve( snapshot.docs )
// Filter out empties
.then( docs => docs.map( doc => doc.exists ? doc : false ) )
.then( docs => docs.filter( doc => doc != false ) )
// Grab data from docs
.then( docs => docs.map( doc => ( { ...doc.data(), id: doc.id } ) ) )
// Call a callback (used int he bigger scheme of a redux listener)
.then( callback )
} )
} ) )
} )
现在这会导致一些听众,但如果 destination/uids 数组变大,这可能会导致大量听众。
我也可以 运行 手动搜索触发器,但我喜欢使用实时更新。
在 Firebase 中是否存在潜在的大量听众问题?是性能还是成本?
您必须更具体地说明 "bazillion"。一般来说,听众很便宜,你不应该太担心它。但是任何事情都有一个实际限制,我想你会在接近你的假设 "bazillion".
时发现它是什么。1 个听众不会调用与 10 个或 100 个或 1000 个听众不同的计费。您可以预期单个侦听器的计费是线性扩展的。对于在所有查询之间都不同的每个文档,每个文档的成本都将完全相同。
每个侦听器还共享一个连接,因此您不必担心 运行 打开的文件描述符或类似的问题。实际上,您的限制是内存和带宽。