1

I'm getting poor performance doing a Model.find(array_of_ids) in mongodb using mongoid drive, and I don't understand why.

Relevant code:

ids = get_ids() #Get an array of 137 _ids for Topic model.
Topic.find(ids) #Find all data, but take 4 seconds to do it.

_id field is auto indexed, so it doesn't need any manual configuration.

So, why is it so slow?

Please ask if you need more of an explanation, and sorry for my poor English.

Jim Simson
  • 2,774
  • 3
  • 22
  • 30
  • 2
    How large are the documents you're finding? If they're huge, that's your problem. If you don't need all the fields there's your solution. – Leopd Apr 06 '13 at 05:08
  • Thanks @Leopd ! Now im using .without(:large_field) and it finds all topics in 50ms. – Leonardo Baptista Apr 08 '13 at 17:54
  • @LeonardoBaptista as Leopd said what is the document ur are finding and also the OS configuration like 32 bit or 64 bit would be great help – Viren May 10 '13 at 06:45

1 Answers1

0

seems that you already got the solution.

I just wanna add on.

If the ids can reach 137, I think you should store the relation in Topic collection instead. It is the other way around. The query will be

Topic.where(:id => self.topic_id)

where self refers to a record of this model.

It is actually has_many and belongs_to relationship.

Mongodb ability to store array, does not mean that for that many (137) records associated from other model you can store that way. The relational method is better. Just put something in array if the data type is not related to other model/collection. For example, topic tag that could be "trendy, teen, childish, cheesy, etc where you do not have a predefined tag, instead, they are defined by your users . This is a good opportunity in using array feature with mongodb.

For your case where the array contains topic lists, where topics are data defined in Topic collection, relational model should have been used.

Just some suggestion, please do not vote me down.