From a3e5f8f8f992561332925d98973225ca4a4cfe99 Mon Sep 17 00:00:00 2001 From: fulleni Date: Wed, 20 Aug 2025 09:05:15 +0100 Subject: [PATCH 1/2] feat(database): add indexes for country aggregation queries - Add 'eventCountry_status_index' on 'headlines' collection - Add 'headquarters_status_index' on 'sources' collection - These indexes will improve performance for country-based analytics --- lib/src/services/database_seeding_service.dart | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/src/services/database_seeding_service.dart b/lib/src/services/database_seeding_service.dart index 2d5c8ea..55ac892 100644 --- a/lib/src/services/database_seeding_service.dart +++ b/lib/src/services/database_seeding_service.dart @@ -118,6 +118,16 @@ class DatabaseSeedingService { .collection('sources') .createIndex(keys: {'name': 'text'}, name: 'sources_text_index'); + // Indexes for country aggregation queries + await _db.collection('headlines').createIndex( + keys: {'eventCountry.id': 1, 'status': 1}, + name: 'eventCountry_status_index', + ); + await _db.collection('sources').createIndex( + keys: {'headquarters.id': 1, 'status': 1}, + name: 'headquarters_status_index', + ); + // --- TTL and Unique Indexes via runCommand --- // The following indexes are created using the generic `runCommand` because // they require specific options not exposed by the simpler `createIndex` From 735018f0a74f46442928f3d6e4e1e69d21ef96a1 Mon Sep 17 00:00:00 2001 From: fulleni Date: Wed, 20 Aug 2025 09:09:43 +0100 Subject: [PATCH 2/2] refactor(database): improve indexing strategy for country aggregation queries - Update index for headlines collection: change key order to prioritize 'status' - Update index for sources collection: change key order to prioritize 'status' - Rename indexes to better reflect their purpose and key order --- lib/src/services/database_seeding_service.dart | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/src/services/database_seeding_service.dart b/lib/src/services/database_seeding_service.dart index 55ac892..9f6f416 100644 --- a/lib/src/services/database_seeding_service.dart +++ b/lib/src/services/database_seeding_service.dart @@ -119,13 +119,17 @@ class DatabaseSeedingService { .createIndex(keys: {'name': 'text'}, name: 'sources_text_index'); // Indexes for country aggregation queries - await _db.collection('headlines').createIndex( - keys: {'eventCountry.id': 1, 'status': 1}, - name: 'eventCountry_status_index', + await _db + .collection('headlines') + .createIndex( + keys: {'status': 1, 'eventCountry.id': 1}, + name: 'status_eventCountry_index', ); - await _db.collection('sources').createIndex( - keys: {'headquarters.id': 1, 'status': 1}, - name: 'headquarters_status_index', + await _db + .collection('sources') + .createIndex( + keys: {'status': 1, 'headquarters.id': 1}, + name: 'status_headquarters_index', ); // --- TTL and Unique Indexes via runCommand ---