Morphia has a difference between fetch and asList in performance

We use morphia 0.99 and the java driver 2.7.3. I would like to know if there is a difference between fetching records one by one using fetch and getting asList results (suppose there is enough memory for asList to get records).

We iterate over a large collection, and when using fetch I sometimes encounter a cursor not found exception on the server during the fetch operation, so I need to run 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 will be faster? Getting records one by one or getting arrays of results using asList or is there no difference between them using morphine implementation?

thanks for answers

+2
source share
3 answers

Judging by the source code, asList() uses fetch() and aggregates the results for you, so I donโ€™t see much difference between them.

+2
source

As far as I understand the implementation, fetch() threads are output from the database, and asList() will load all the query results into memory. That way they both will get every object that matches the request, but asList() will load them all into memory, and fetch() leaves it for you.

For your use case, it will not be faster in terms of CPU, but fetch() should use less memory and not explode if you have a lot of database records.

+3
source

One very useful difference is that the following two conditions apply to your scenario:

  • You used offset and limit in the request.
  • You changed the values โ€‹โ€‹on the object so that it no longer returned to the request.

So, say you made the request at awesome=true , and you used offset and limit to execute multiple requests, returning 100 records at a time to make sure you didn't use too much memory. If you set awesome=false on your object in your iteration loop and save it, this will skip updating some records.

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

+1
source

All Articles