cubbuk
cubbuk

Reputation: 7920

Morphia is there a difference between fetch and asList in performance wise

We are using morphia 0.99 and java driver 2.7.3 I would like to learn is there any difference between fetching records one by one using fetch and retrieving results by asList (assume that there is enough memory to retrieve records through asList).

We iterate over a large collection, while using fetch I sometimes encounter cursor not found exception on the server during the fetch operation, so I need to execute another command to continue, what could be the reason for this?

1-)fetch the record 
2-)do some calculation on it 
3-)+save it back to database again 
4-)fetch another record and repeat the steps until there isn't any more records. 

So which one would be faster? Fetching records one by one or retrieving bulks of results using asList, or isn't there any difference between them using morphia implementation?

Thanks for the answers

Upvotes: 2

Views: 2406

Answers (3)

dkilmer
dkilmer

Reputation: 85

One very useful difference would be if the following two conditions applied to your scenario:

  1. You were using offset and limit in the query.
  2. You were changing values on the object such that it would no longer be returned in the query.

So say you were doing a query on awesome=true, and you were using offset and limit to do multiple queries, returning 100 records at a time to make sure you didn't use up too much memory. If, in your iteration loop, you set awesome=false on an object and saved it, it would cause you to miss updating some records.

In a case like this, fetch() would be a better approach.

Upvotes: 1

mangecoeur
mangecoeur

Reputation: 918

As far as I understand the implementation, fetch() streams results from the DB while asList() will load all query results into memory. So they will both get every object that matches the query, but asList() will load them all into memory while fetch() leaves it up to you.

For your use case, it neither would be faster in terms of CPU, but fetch() should use less memory and not blow up in case you have a lot of DB records.

Upvotes: 3

xeraa
xeraa

Reputation: 10859

Judging from the source-code, asList() uses fetch() and aggregates the results for you, so I can't see much difference between the two.

Upvotes: 2

Related Questions