Hello all, seems like very basic requirement but still not able to figure it out.
So I have 10Million records in a collection who’s structure is somewhat like below:
{
"timestamp": {
"$date": "2024-06-09T23:58:18.846Z"
},
"old_value": {
"bidAmount": 0.18
},
"new_value": {
"bidAmount": 0.18
},
"event_master_name": "GAME_BID_UPDATE"
i have created index on bidAmount as below:
db.logs.createIndex({ “new_value.bidAmount”: 1 });
And trying to find docs using below queries which takes minutes to run, am i missing anything?
Both query run in minutes.
1) db.logs.find({ "new_value.bidAmount": { "$gt": 20 } }).sort({ "timestamp": -1 }).limit(100)
2) db.logs.find({ "new_value.bidAmount": { "$gt": 20 } }).hint({ "new_value.bidAmount": 1 }).sort({ "timestamp": -1 }).limit(100)
attaching both plans below for reference:
Plan 1:
{
explainVersion: '1',
stages: [
{
'$cursor': {
queryPlanner: {
namespace: 'temp.system.buckets.logs',
indexFilterSet: false,
parsedQuery: {
'control.max.new_value.bidAmount': { '$_internalExprGt': 2 }
},
queryHash: 'CDA4A9BD',
planCacheKey: '3716670F',
maxIndexedOrSolutionsReached: false,
maxIndexedAndSolutionsReached: false,
maxScansToExplodeReached: false,
winningPlan: {
stage: 'COLLSCAN',
filter: {
'control.max.new_value.bidAmount': { '$_internalExprGt': 2 }
},
direction: 'backward'
},
rejectedPlans: []
}
}
},
{
'$match': {
'$expr': {
'$lte': [
{
'$subtract': [ '$control.max.timestamp', '$control.min.timestamp' ]
},
{ '$const': Long('3600000') }
]
}
}
},
{
'$_internalUnpackBucket': {
exclude: [],
timeField: 'timestamp',
bucketMaxSpanSeconds: 3600,
assumeNoMixedSchemaData: true,
includeMinTimeAsMetadata: true,
eventFilter: { 'new_value.bidAmount': { '$gt': 2 } }
}
},
{
'$_internalBoundedSort': {
sortKey: { timestamp: -1 },
bound: { base: 'min', offsetSeconds: Long('3600') },
limit: Long('100')
}
}
],
serverInfo: {
host: '9ddfbf90235d',
port: 27017,
version: '7.0.11',
gitVersion: 'f451220f0df2b9dfe073f1521837f8ec5c208a8c'
},
serverParameters: {
internalQueryFacetBufferSizeBytes: 104857600,
internalQueryFacetMaxOutputDocSizeBytes: 104857600,
internalLookupStageIntermediateDocumentMaxSizeBytes: 104857600,
internalDocumentSourceGroupMaxMemoryBytes: 104857600,
internalQueryMaxBlockingSortMemoryUsageBytes: 104857600,
internalQueryProhibitBlockingMergeOnMongoS: 0,
internalQueryMaxAddToSetBytes: 104857600,
internalDocumentSourceSetWindowFieldsMaxMemoryBytes: 104857600,
internalQueryFrameworkControl: 'trySbeRestricted'
},
command: {
aggregate: 'system.buckets.logs',
pipeline: [
{
'$_internalUnpackBucket': {
timeField: 'timestamp',
bucketMaxSpanSeconds: 3600,
assumeNoMixedSchemaData: true,
usesExtendedRange: false
}
},
{ '$match': { 'new_value.bidAmount': { '$gt': 2 } } },
{ '$sort': { timestamp: -1 } },
{ '$limit': Long('100') }
],
cursor: {},
collation: { locale: 'simple' }
},
ok: 1
}
Plan2:
{
explainVersion: '1',
stages: [
{
'$cursor': {
queryPlanner: {
namespace: 'temp.system.buckets.logs',
indexFilterSet: false,
parsedQuery: {
'control.max.new_value.bidAmount': { '$_internalExprGt': 2 }
},
queryHash: 'CDA4A9BD',
planCacheKey: '3716670F',
maxIndexedOrSolutionsReached: false,
maxIndexedAndSolutionsReached: false,
maxScansToExplodeReached: false,
winningPlan: {
stage: 'FETCH',
filter: {
'control.max.new_value.bidAmount': { '$_internalExprGt': 2 }
},
inputStage: {
stage: 'IXSCAN',
keyPattern: {
'control.min.new_value.bidAmount': 1,
'control.max.new_value.bidAmount': 1
},
indexName: 'new_value.bidAmount_1',
isMultiKey: false,
multiKeyPaths: {
'control.min.new_value.bidAmount': [],
'control.max.new_value.bidAmount': []
},
isUnique: false,
isSparse: false,
isPartial: false,
indexVersion: 2,
direction: 'forward',
indexBounds: {
'control.min.new_value.bidAmount': [ '[MinKey, MaxKey]' ],
'control.max.new_value.bidAmount': [ '[MinKey, MaxKey]' ]
}
}
},
rejectedPlans: []
}
}
},
{
'$_internalUnpackBucket': {
exclude: [],
timeField: 'timestamp',
bucketMaxSpanSeconds: 3600,
assumeNoMixedSchemaData: true,
eventFilter: { 'new_value.bidAmount': { '$gt': 2 } }
}
},
{ '$sort': { sortKey: { timestamp: -1 }, limit: Long('100') } }
],
serverInfo: {
host: '9ddfbf90235d',
port: 27017,
version: '7.0.11',
gitVersion: 'f451220f0df2b9dfe073f1521837f8ec5c208a8c'
},
serverParameters: {
internalQueryFacetBufferSizeBytes: 104857600,
internalQueryFacetMaxOutputDocSizeBytes: 104857600,
internalLookupStageIntermediateDocumentMaxSizeBytes: 104857600,
internalDocumentSourceGroupMaxMemoryBytes: 104857600,
internalQueryMaxBlockingSortMemoryUsageBytes: 104857600,
internalQueryProhibitBlockingMergeOnMongoS: 0,
internalQueryMaxAddToSetBytes: 104857600,
internalDocumentSourceSetWindowFieldsMaxMemoryBytes: 104857600,
internalQueryFrameworkControl: 'trySbeRestricted'
},
command: {
aggregate: 'system.buckets.logs',
pipeline: [
{
'$_internalUnpackBucket': {
timeField: 'timestamp',
bucketMaxSpanSeconds: 3600,
assumeNoMixedSchemaData: true,
usesExtendedRange: false
}
},
{ '$match': { 'new_value.bidAmount': { '$gt': 2 } } },
{ '$sort': { timestamp: -1 } },
{ '$limit': Long('100') }
],
cursor: {},
collation: { locale: 'simple' },
hint: {
'control.min.new_value.bidAmount': 1,
'control.max.new_value.bidAmount': 1
}
},
ok: 1
}