MongoDB - 为什么我应该使用游标而不是iterator_to_array(在PHP中) [英] MongoDB - Why should I use a cursor instead of iterator_to_array (in PHP)

查看:786
本文介绍了MongoDB - 为什么我应该使用游标而不是iterator_to_array(在PHP中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mongo类的PHP文档说明使用光标 iterator_to_array 是优越的。为什么? 使用 iterator_to_array() c>使您的驱动程序将所有结果一次性加载到内存中,您可能很容易耗尽内存。这不是使用延迟加载的游标的情况!



直接从链接的 docs


 <?php 

$ cursor = $ collection-> find();
var_dump(iterator_to_array($ cursor));

?>

...



在上面的示例中, $ collection 是一个50GB的集合。我们当然不希望将它一次加载到内存中,这是一个游标:允许客户端访问dribs和drabs中的集合。



The PHP documentation for the mongo class says using a cursor instead of iterator_to_array is superior. Why? What benefits/flexibility will I get from that?

解决方案

Using iterator_to_array() makes your driver load all of the results into memory at once, and you could easily run out of memory. This would not be the case with a cursor, which uses lazy-loading!

Straight from the linked docs:

<?php

$cursor = $collection->find();
var_dump(iterator_to_array($cursor));

?>

...

Suppose that, in the example above, $collection was a 50GB collection. We certainly wouldn't want to load that into memory all at once, which is what a cursor is for: allowing the client to access the collection in dribs and drabs.

这篇关于MongoDB - 为什么我应该使用游标而不是iterator_to_array(在PHP中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆