gpt4 book ai didi

mongodb - 为什么在实际查询只需要几毫秒时获取 MongoDB 结果却很慢?

转载 作者:可可西里 更新时间:2023-11-01 09:55:56 24 4
gpt4 key购买 nike

我正在使用 Doctrine MongoDB ODM 从远程 MongoDB 数据库中获取少量文档。

我确认查询只用了 1 毫秒就找到了大约 12 个匹配的文档。 (即来自解释输出的“millis”:1)。但迭代结果大约需要 250 毫秒。

当我尝试组合使用以下选项时,我无法获得任何性能提升

  • 选择('姓名')
  • 水合物(假)
  • eagerCursor(真)
  • 限制(1)

我怎样才能最大限度地减少这种延迟?


更新:示例代码的更多解释

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

最佳答案

这里有两个感兴趣的区间:一个是从你的代码开始到A点;第二个是从A点到B点。.explain()测量的是前者;你测量的是后者。

特别是,一旦 BSON 文档已传输到客户端(您的 PHP 程序),它们仍然需要反序列化并转换为 PHP 对象。如果您使用的是 Doctrine,那也必须执行额外的处理。

您看到的时间是反序列化过程的时间。文档有多大?必须反序列化文档的全部内容:如果这些内容很大或很复杂(嵌套很深、数组很多等),那么这可能需要一些时间。

您可以通过只获取您需要的字段来减少反序列化时间。如果您将 ->select('_id') 添加到您的查询中,您的循环时间应该会快得多。

关于mongodb - 为什么在实际查询只需要几毫秒时获取 MongoDB 结果却很慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14191157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com