Sample data:
{
"metadata": {
"id": "abcd-12",
"type": "type1"
},
"timestamp": "2023-06-27T03:46:49.786099495Z",
"data": {
"ids": [
"abcd-12",
"efdg-33"
],
"attribute_1": "abc"
}
}
query:
db.ts.find({"metadata.type":"type1","data.ids":{"$in":["efdg-33"]}}).sort({"_id":-1}).limit(1)
log from mongoDB:
{
"t": {
"$date": "2023-06-27T19:10:04.850+00:00"
},
"s": "I",
"c": "COMMAND",
"id": 51803,
"ctx": "conn9",
"msg": "Slow query",
"attr": {
"type": "command",
"ns": "sampledb.ts",
"command": {
"find": "ts",
"filter": {
"metadata.type": "type1",
"data.ids": {
"$in": [
"efdg-33"
]
}
},
"limit": 1,
"projection": {
"data": 0
},
"singleBatch": true,
"sort": {
"_id": -1
},
"lsid": {
"id": {
"$uuid": "e55dfa5f-d438-4767-862a-d4912efc03e3"
}
},
"$db": "sampledb"
},
"planSummary": "COLLSCAN",
"resolvedViews": [
{
"viewNamespace": "sampledb.ts",
"dependencyChain": [
"ts",
"system.buckets.ts"
],
"resolvedPipeline": [
{
"$_internalUnpackBucket": {
"timeField": "timestamp",
"metaField": "metadata",
"bucketMaxSpanSeconds": 86400
}
}
]
}
],
"keysExamined": 0,
"docsExamined": 2606504,
"hasSortStage": true,
"cursorExhausted": true,
"numYields": 2703,
"nreturned": 1,
"queryHash": "130CB8DF",
"planCacheKey": "130CB8DF",
"queryFramework": "classic",
"reslen": 194,
"locks": {
"FeatureCompatibilityVersion": {
"acquireCount": {
"r": 2819
}
},
"Global": {
"acquireCount": {
"r": 2819
}
},
"Mutex": {
"acquireCount": {
"r": 116
}
}
},
"storage": {
"data": {
"bytesRead": 7279476780,
"timeReadingMicros": 7278730
},
"timeWaitingMicros": {
"cache": 2822
}
},
"remote": "129.0.168.1:60736",
"protocol": "op_msg",
"durationMillis": 14869
}
}
it becomes worse when the number of document grows ( about 70 for same metadata.id, 4M for all documents)
Indexing on the data.ids should improve the performance but it looks like the mongoDB ( time series doesn’t support indexing on arrays.
sampldb> db.ts.createIndex( { "data.ids": 1 } )
MongoServerError: Index build failed: b38519f2-ca8f-4244-91fe-845f87f19137: Collection sampldb.system.buckets.ts ( a17c3bff-c74f-4a3d-b18f-aee3bca3404b ) :: caused by :: Indexed measurement field contains an array value
mongoDB version:6.0.6
Please let me know if there is a solution , Thanks.