我正在使用Doctrine MongoDB ODM從遠程MongoDB數據庫中獲取少量文檔。爲什麼在實際查詢只花費幾毫秒時,MongoDB結果很慢?
我確認查詢只用了1ms找到約12個匹配的文檔。 (即解釋輸出中的「millis」:1)。但迭代結果花費了大約250毫秒。
我不能得到任何性能增益,當我試圖下列選項
- 選擇( '名稱')
- 水合物(假)
- eagerCursor(真)
- 限組合(1)
我該如何最小化這種延遲?
UPDATE:更多的解釋與示例代碼
$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?
*/
什麼是查詢?是否只涉及一個集合? – Thilo
是的,只有一個集合。基本上我正在獲取匹配{userId:123,status:'active'}的記錄。我已經有了這些字段的索引。 {userId:1,status:1} – Jaepil
如果您跳過ODM並通過Mongo控制檯進行查詢和迭代,會發生什麼情況? – 2013-01-07 10:02:32