I have a slow query in my application. After created two indexes it uses them with better performance in local DB. But when I deployed on production DB it still uses the origin index.
Below this what I did.
Properties in collection tasks
: team_id
, project_id
, created_by
and assignee
, etc.
The query looks like below
db.tasks.find({
team_id: new ObjectId(teamId),
$or: [
{
project_id: newObjectId(projectId),
created_by: userId
},
{
assignee: userId
}
]
})
Originally there's only one index against team_id
, which will check over 10k docs. Then I added two new indexes
project_1_created_by_1: {
project: 1,
created_by: 1
}
assignee_1: {
assignee: 1
}
In local DB I ran my query with explain({ verbose: true })
. I can see MongoDB evaluated indexes
[
QueryOptimizerCursor: [
'project_1_created_by_1',
'assignee_1',
],
BtreeCursor: 'team_1'
]
Finally QueryOptimizerCursor
won.
But when I ran it on production MongoDB the result of explain({ verbose: true })
shown it only evaluated team_1
and BasicCursor
.
[
BtreeCursor: `team_1`,
BasicCursor
]
Does anyone give me some information why MongoDB didn't use new indexes I created, even worse it didn't evaluate it.
PS: I can confirm the new indexes were ready in my production database since when I use query db.tasks.find({project: xxx, created_by:yyy}).explain()
it uses the new one I created.
Updated
The version of of production MongoDB was 2.4.12 while local was 2.6.7. When I installed a new copy of MongoDB 2.4.12 at local and ran the same query it used team
index rather than QueryOptimizerCursor
.
Not pretty sure if this is only because of MongoDB 2.6.7 is more smarter than 2.4.12.