Using APM on Galaxy, I see that I have some aggregate requests that take a lot o…f time before responding (more than 6s) which is not good
Here is the trace explorer, we can see that it is the observeChanges on moments that is the root cause of this long time :
```
Started at Monday, Dec 18, 2023 15:05:12
wait time 0ms on computation 1ms
find on moments 0ms
find on contacts 0ms
computation 1ms
observeChanges on moments 9372ms Using Oplog
coll : moments
selector : {"isDeleted":false}
func : observeChanges
cursor : true
oplog : true
wasMultiplexerReady : false
queueLength : 0
elapsedPollingTime : 0
noOfCachedDocs : 1565
observeChanges on contacts 1ms Using Oplog
observeChanges on users 110ms Using Oplog
computation 1ms
async 182ms
Completed at Monday, Dec 18, 2023 15:05:21 (Response Time: 9670ms)
```
and the request :
```
set some selector, some userId ...
ReactiveAggregate(
this,
Moments,
[
{
$lookup: {
from: 'contacts',
pipeline: [
....
],
as: 'contactFollowings',
},
},
{
$addFields: {
contacts: {
....
},
},
{
$match: {
$expr: {
$and: selector,
},
},
},
{
$sort: { created: -1 },
},
{
$limit: limit,
},
{ $project: { contacts: 0, contactFollowings: 0 } },
{
$lookup: {
from: 'users',
let: { userId: '$userId' },
pipeline: [
....
],
as: 'user',
},
},
{
$unwind: {
path: '$user',
},
},
{
$facet: {
stage1: [ ... ],
stage2: [ ...],
},
},
{ $addFields: { final: { $concatArrays: ['$stage1', '$stage2'] } } },
{ $project: { stage1: 0, stage2: 0 } },
{
$unwind: {
path: '$final',
},
},
{
$replaceRoot: {
newRoot: '$final',
},
},
],
{
noAutomaticObserver: true,
debounceDelay: 100,
observers: [
Moments.find({ isDeleted: false }),
Contacts.find({ userId }),
Meteor.users.find({}),
],
}
);
});
```
Checklist of what I already verified :
- indexes are fine on all collections
- oplog is set (as shown by "Using Oplog")
- the number of documents to scan is small (less than thousands)
- the same direct request (same selector on moments as observer) on the database takes few ms
- unblock() is done on aggregate call : maybe there is a need for an unblock on the observer inside the library ?$
Welcome to any idea on how to fix this !
Thank you !