I’m using Doctrine MongoDB ODM to fetch a small number of documents from a remote MongoDB database.
I confirmed the query took only 1ms to find about 12 matching docs. (i.e. ‘millis’:1 from explain ouput). But iterating through the results took around 250ms.
I couldn’t get any performance gain when I tried combinations of the following options
- select(‘name’)
- hydrate(false)
- eagerCursor(true)
- limit(1)
How can I minimize this delay?
UPDATE: More explanation with sample code
$qb = $dm->createQueryBuilder('Books');
$books = $qb->select('name')
->field('userId')->equals(123)
->field('status')->equals('active')
->eagerCursor(true) // Fetch all data at once
->getQuery()
->execute();
/**
* Due to using Eager Cursor, the database connection should be closed and
* all data should be in memory now.
*/
// POINT A
foreach($books as $book) {
// I do nothing here. Just looping through the results.
}
// POINT B.
/**
* From POINT A to POINT B takes roughly 250ms when the query had 12 matching docs.
* And this doesn't seem to be affected much by the number of records matched.
* As the data is already in the memory, I expected this to be done in range of
* 5~10ms, not 250ms.
*
* Am I misunderstanding the meaning of Eager Cursor?
*/
OK, looks like I misunderstood the meaning of Eager Cursor.
http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/eager-cursors.html
This document suggests that when execute() is called all results are retrieved into the memory. But that’s not quite accurate. Instead, I found the records were fetched when the EasgerCursor was accessed for the first time.
In EagerCursor’s initialize() function, the instance of a normal Mongo Cursor is retrieved and eventually passed into iterator_to_array() function after going through few other functions. I’m pretty sure this is when Mongo Driver does its real work of fetching records.
In my case, this happens in the foreach loop. Hence the delay was observed between Point A and Point B. Just to confirm this, I tried MongoClient implementation and found the overall times were very close between the implementations.
Thanks everyone for the help.