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 itemCounts = await getCollectionItemCounts(collectionIdList);
|
||||||
|
|
||||||
final memberCounts = <String, int>{};
|
final memberCounts = <String, int>{};
|
||||||
await Future.wait(collectionIdList.map((collectionId) async {
|
final members = await supabase
|
||||||
try {
|
.from('collection_members')
|
||||||
final rows = await supabase.rpc('get_collection_members', params: {
|
.select('collection_id')
|
||||||
'p_collection_id': collectionId,
|
.inFilter('collection_id', collectionIdList);
|
||||||
});
|
|
||||||
memberCounts[collectionId] = (rows as List).length;
|
|
||||||
} catch (_) {
|
|
||||||
// Keep fallback below when RPC fails.
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
|
|
||||||
// Fallback member count for collections where RPC did not return data.
|
for (final member in members) {
|
||||||
final unresolvedIds = collectionIdList
|
final collectionId = member['collection_id'] as String;
|
||||||
.where((id) => !memberCounts.containsKey(id))
|
memberCounts[collectionId] = (memberCounts[collectionId] ?? 0) + 1;
|
||||||
.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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final collections = <Collection>[];
|
final collections = <Collection>[];
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue