Sarvesh
Sarvesh

Reputation: 1202

Mongoid queries are fast but converting to results takes time

Mongoid queries are fast. For example if it a query which returns 300 documents takes about 10 miliseconds but if I do

to_a or entries to get the results takes time.

Is there any other way to reduce the time lag.

Upvotes: 1

Views: 291

Answers (1)

Nicolas Modrzyk
Nicolas Modrzyk

Reputation: 14197

I haven't look at the source code yet, but Mongoid queries are lazily evaluated.

Which means, your first call to the criteria does not touch the database, and Mongoid actually really fetch the data when you do 'to_a' or 'entries' hence the time lag.

Upvotes: 2

Related Questions