Hi,
i have a problem with performance of query; i have a collection with 81 millions of records, where the fields that indicating a macro-category is “idTracciato”
"idTracciato": "458"", "count": 23275575
"idTracciato": "488"", "count": 1207470
"idTracciato": "500"", "count": 1121987
"idTracciato": "511"", "count": 956498
"idTracciato": "456"", "count": 789206
"idTracciato": "475"", "count": 520500
"idTracciato": "304"", "count": 18014
"idTracciato": "207"", "count": 15760
"idTracciato": "107"", "count": 12613
"idTracciato": "198"", "count": 9457
"idTracciato": "411"", "count": 7166
"idTracciato": "100"", "count": 6304
"idTracciato": "410"", "count": 5474
"idTracciato": "462"", "count": 3587
"idTracciato": "132"", "count": 3156
"idTracciato": "117"", "count": 3154
"idTracciato": "102"", "count": 3152
"idTracciato": "232"", "count": 3152
"idTracciato": "177"", "count": 3152
"idTracciato": "210"", "count": 3152
"idTracciato": "461"", "count": 2594
"idTracciato": "482"", "count": 2020
my queries have as last step “$limit” operator with 16 records, so if I have more than 16 records as a result of the query, mongo replies in 1.4 seconds; otherwise if i have no records mongo don’t reply ( after 10 minutes )
the query that have result is
db.flussi_dettagli.aggregate([
{
"$match" : {
"idTracciato" : Long("458")
}
},
{
"$match" : {
"dataCreazione" : {
"$lte" : ISODate("2023-04-05T13:46:00.200+02:00")
}
}
},
{
"$match" : {
"stato" : {
"$ne" : "CACHED"
}
}
},
{
"$match" : {
"metadata.annoRiferimento" : Long("2023"),
"metadata.periodoRiferimento" : "01"
}
},
{
"$addFields" : {
"validazioniReduce" : {
"$reduce" : {
"input" : "$validazioni",
"initialValue" : {
"dataAzione" : 0
},
"in" : {
"$cond" : [
{
"$gte" : [ "$$this.dataAzione", "$$value.dataAzione" ]
},
"$$this",
"$$value"
]
}
}
}
}
},
{
"$match" : {
"flagVersioneMassima" : true
}
},
{
"$addFields" : {
"keyNoStato" : "$key"
}
},
{
"$project" : {
"keyNoStato.stato" : 0
}
},
{
"$project" : {
"_id" : "$keyNoStato",
"data" : 1,
"metadata" : 1,
"progressivo" : 1,
"progressivoMassimo" : 1,
"_oid" : "$_id",
"stato" : 1,
"validazioni" : 1,
"validazioniReduce" : 1,
"derived" : 1,
"key" : 1,
"tempDiscard" : 1,
"idUtente" : 1,
"idTracciato" : 1,
"idFlusso" : 1,
"component_1" : 1,
"component_2" : 1,
"component_3" : 1,
"component_4" : 1
}
},
{
"$skip" : 0
},
{
"$limit" : 16
}
]
)
if i add other stage of match like below
{
"$match" : {
"metadata.annoRiferimento" : Long("2024"),
"metadata.periodoRiferimento" : "01"
}
},
don’t reply…
please help me, i’m in trouble