-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
-
Labels:None
I've noticed mongoid automatically adds a $orderby"=>{:_id=>1} to find_by query.
For example, User.find_by(:email => 'some@email.com') does the following query
users selector={"$query"=>
, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.8535ms
This is pretty taxing on a large production database. Because of the extra sort, there would need to be a multi-key index of {:email => 1, :_id => 1} instead of just {:email => 1}.
In our case, that's an extra 800MB of ram required to store the extra :_id sorting.
The current workaround I have is to specify a mock sort. For example, I'd do User.order_by(:email.asc).find_by(:email => 'some@email.com')