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?
*/