perf(collections): remove N+1 member count RPCs with single batched query
This commit is contained in:
parent
1dc440cd25
commit
c2d8e754a2
1 changed files with 7 additions and 24 deletions
|
|
@ -114,31 +114,14 @@ class CollectionService {
|
|||
final itemCounts = await getCollectionItemCounts(collectionIdList);
|
||||
|
||||
final memberCounts = <String, int>{};
|
||||
await Future.wait(collectionIdList.map((collectionId) async {
|
||||
try {
|
||||
final rows = await supabase.rpc('get_collection_members', params: {
|
||||
'p_collection_id': collectionId,
|
||||
});
|
||||
memberCounts[collectionId] = (rows as List).length;
|
||||
} catch (_) {
|
||||
// Keep fallback below when RPC fails.
|
||||
}
|
||||
}));
|
||||
final members = await supabase
|
||||
.from('collection_members')
|
||||
.select('collection_id')
|
||||
.inFilter('collection_id', collectionIdList);
|
||||
|
||||
// Fallback member count for collections where RPC did not return data.
|
||||
final unresolvedIds = collectionIdList
|
||||
.where((id) => !memberCounts.containsKey(id))
|
||||
.toList(growable: false);
|
||||
if (unresolvedIds.isNotEmpty) {
|
||||
final members = await supabase
|
||||
.from('collection_members')
|
||||
.select('id, collection_id')
|
||||
.inFilter('collection_id', unresolvedIds);
|
||||
|
||||
for (final member in members) {
|
||||
final collectionId = member['collection_id'] as String;
|
||||
memberCounts[collectionId] = (memberCounts[collectionId] ?? 0) + 1;
|
||||
}
|
||||
for (final member in members) {
|
||||
final collectionId = member['collection_id'] as String;
|
||||
memberCounts[collectionId] = (memberCounts[collectionId] ?? 0) + 1;
|
||||
}
|
||||
|
||||
final collections = <Collection>[];
|
||||
|
|
|
|||
Loading…
Reference in a new issue