[SERVER-4299] Adding .sort(...) to an $or query prevents use of indexes. Created: 16/Nov/11  Updated: 29/Feb/12  Resolved: 16/Nov/11

Status: Closed
Project: Core Server
Component/s: Index Maintenance
Affects Version/s: 2.0.1
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Marc Bastien Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: indexing, sort
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Participants:

 Description   

This was generated from the Google Groups discussion, "sorting, does not use defined index."
http://groups.google.com/group/mongodb-user/browse_thread/thread/9bc121708a0a5cde

To reproduce:
> db.example.drop();
> db.example.save({_id:1, "a":"aaa"});
> db.example.save({_id:2, "a":"bbb"});
> db.example.save({_id:3, "a":"ccc"});
> db.example.save({_id:4, "a":"ddd"});
> db.example.save({_id:5, "a":"eee"});
> db.example.save({_id:6, "a":"fff"});
> db.example.save({_id:7, "a":"ggg"});
> db.example.save({_id:8, "a":"hhh"});
> db.example.ensureIndex(

{a:1}

);

> db.example.find({$or:[

{a:"aaa"}

,

{a:"bbb"}

]}).explain(true)

The index is used, and "nscanned" : 2

Now sort the results:

> db.example.find({$or:[

{a:"aaa"}

,

{a:"bbb"}

]}).sort(

{a:1}

).explain(true)
The index is still used, but "nscanned" is now 8, indicating that Mongo is accessing every document in the collection.



 Comments   
Comment by Scott Hernandez (Inactive) [ 16/Nov/11 ]

dup of SERVER-1205

Generated at Thu Feb 08 03:05:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.