OK, I have copied my collection to a new one and I have added a single index (on my interest field) so now I have
> db.myCollection.getIndexes ()
[
{
v: 2,
key: { _id: 1 },
name: '_id_',
ns: '...'
},
{
v: 2,
key: { eventType: 1 },
name: 'eventType_1',
ns: '...'
}
]
But when I execute my query (db.myCollection.aggregate ( [ { $sortByCount: "$eventType" } ], { allowDiskUse: false}).explain ("executionStats")
) it still does a COLLSCAN only !!!
{
stages: [
{
'$cursor': {
query: {},
fields: { eventType: 1, _id: 0 },
queryPlanner: {
plannerVersion: 1,
namespace: '...',
indexFilterSet: false,
parsedQuery: {},
queryHash: '8B3D4AB8',
planCacheKey: '8B3D4AB8',
winningPlan: { stage: 'COLLSCAN', direction: 'forward' },
rejectedPlans: []
},
executionStats: {
executionSuccess: true,
nReturned: 783254,
executionTimeMillis: 9941,
totalKeysExamined: 0,
totalDocsExamined: 783254,
executionStages: {
stage: 'COLLSCAN',
nReturned: 783254,
executionTimeMillisEstimate: 1181,
works: 783256,
advanced: 783254,
needTime: 1,
needYield: 0,
saveState: 6217,
restoreState: 6217,
isEOF: 1,
direction: 'forward',
docsExamined: 783254
}
}
}
},
{
'$group': { _id: '$eventType', count: { '$sum': { '$const': 1 } } }
},
{ '$sort': { sortKey: { count: -1 } } }
],
serverInfo: {
host: 'mongo-mongodb-56c7dffc8c-f5mmj',
port: 27017,
version: '4.2.4',
gitVersion: 'b444815b69ab088a808162bdb4676af2ce00ff2c'
},
ok: 1
}
The only way to force index usage is to add the hint; I wouldn’t mind it too much, but even in this case it is very slow (for a 783k items collection), and there are about 143 different values for eventType:
{
stages: [
{
'$cursor': {
query: {},
fields: { eventType: 1, _id: 0 },
queryPlanner: {
plannerVersion: 1,
namespace: '...',
indexFilterSet: false,
parsedQuery: {},
queryHash: 'EFB2EDD9',
planCacheKey: 'EFB2EDD9',
winningPlan: {
stage: 'PROJECTION_COVERED',
transformBy: { eventType: 1, _id: 0 },
inputStage: {
stage: 'IXSCAN',
keyPattern: { eventType: 1 },
indexName: 'eventType_1',
isMultiKey: false,
multiKeyPaths: { eventType: [] },
isUnique: false,
isSparse: false,
isPartial: false,
indexVersion: 2,
direction: 'forward',
indexBounds: { eventType: [ '[MinKey, MaxKey]' ] }
}
},
rejectedPlans: []
},
executionStats: {
executionSuccess: true,
nReturned: 783254,
executionTimeMillis: 6770,
totalKeysExamined: 783254,
totalDocsExamined: 0,
executionStages: {
stage: 'PROJECTION_COVERED',
nReturned: 783254,
executionTimeMillisEstimate: 729,
works: 783255,
advanced: 783254,
needTime: 0,
needYield: 0,
saveState: 6206,
restoreState: 6206,
isEOF: 1,
transformBy: { eventType: 1, _id: 0 },
inputStage: {
stage: 'IXSCAN',
nReturned: 783254,
executionTimeMillisEstimate: 550,
works: 783255,
advanced: 783254,
needTime: 0,
needYield: 0,
saveState: 6206,
restoreState: 6206,
isEOF: 1,
keyPattern: { eventType: 1 },
indexName: 'eventType_1',
isMultiKey: false,
multiKeyPaths: { eventType: [] },
isUnique: false,
isSparse: false,
isPartial: false,
indexVersion: 2,
direction: 'forward',
indexBounds: { eventType: [ '[MinKey, MaxKey]' ] },
keysExamined: 783254,
seeks: 1,
dupsTested: 0,
dupsDropped: 0
}
}
}
}
},
{
'$group': { _id: '$eventType', count: { '$sum': { '$const': 1 } } }
},
{ '$sort': { sortKey: { count: -1 } } }
],
Why totalKeysExamined is equal with the number of documents instead of being equal with the number of distinct values?