laravel光标和laravel块方法有什么区别? [英] What is the difference between laravel cursor and laravel chunk method?

查看:321
本文介绍了laravel光标和laravel块方法有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道laravel块和laravel游标方法之间的区别是什么.哪种方法更适合使用?两者的用例将是什么?我知道您应该使用游标来节省内存,但是它在后端中实际上是如何工作的?

I would like to know what is the difference between laravel chunk and laravel cursor method. Which method is more suitable to use? What will be the use cases for both of them? I know that you should use cursor to save memory but how it actually works in the backend?

带有示例的详细说明将很有用,因为我已经搜索了stackoverflow和其他站点,但没有找到太多信息.

A detailed explanation with example would be useful because I have searched on stackoverflow and other sites but I didn't found much information.

这是laravel文档中的代码段.

Here is the code snippet's from the laravel documentation.

分组结果

Flight::chunk(200, function ($flights) {
    foreach ($flights as $flight) {
        //
    }
});

使用光标

foreach (Flight::where('foo', 'bar')->cursor() as $flight) {
    //
}

推荐答案

的确是一个问题,但是简单的答案在

Indeed This question might attract some opinionated answer, however the simple answer is here in Laravel Docs

仅供参考:

这是块:

如果您需要处理成千上万的口才记录,请使用chunk命令. chunk方法将检索Eloquent模型的块",将其馈送到给定的Closure进行处理.在处理大型结果集时,使用chunk方法将节省内存:

If you need to process thousands of Eloquent records, use the chunk command. The chunk method will retrieve a "chunk" of Eloquent models, feeding them to a given Closure for processing. Using the chunk method will conserve memory when working with large result sets:

这是游标:

cursor方法允许您使用游标遍历数据库记录,该游标将仅执行单个查询.在处理大量数据时,可以使用cursor方法大大减少您的内存使用量:

The cursor method allows you to iterate through your database records using a cursor, which will only execute a single query. When processing large amounts of data, the cursor method may be used to greatly reduce your memory usage:

Chunk从数据库中检索记录,并将其加载到内存中,同时在最后检索到的记录上设置光标,这样就不会发生冲突.

Chunk retrieves the records from the database, and load it into memory while setting a cursor on the last record retrieved so there is no clash.

因此,这样做的好处是,如果您希望在记录发送之前重新格式化它们,或者您希望每次对第n个记录执行一次操作,则很有用.例如,如果您要构建一个view out/excel工作表,则可以按计数记录直到完成,这样所有记录都不会立即加载到内存中,从而达到内存限制.

So the advantage here is if you want to reformat the large record before they are sent out, or you want to perform an operation on an nth number of records per time then this is useful. An example is if you are building a view out/excel sheet, so you can take the record in counts till they are done so that all of them are not loaded into the memory at once and thereby hitting the memory limit.

Cursor使用PHP生成器,您可以检查 php生成器页面,但这是一个有趣的标题:

Cursor uses PHP Generators, you can check the php generators page however here is an interesting caption:

生成器允许您编写使用 foreach 遍历一组数据而无需在内存中构建数组,这可能会导致您超出内存限制,或需要大量的处理时间才能生成.相反,您可以编写一个生成器函数,该函数与普通的功能,除了代替返回生成器一次可以产量提供所需迭代次数的次数.

A generator allows you to write code that uses foreach to iterate over a set of data without needing to build an array in memory, which may cause you to exceed a memory limit, or require a considerable amount of processing time to generate. Instead, you can write a generator function, which is the same as a normal function, except that instead of returning once, a generator can yield as many times as it needs to in order to provide the values to be iterated over.

虽然我不能保证我完全理解Cursor的概念,但是对于Chunk来说,块将以每种记录大小运行查询,将其检索,然后将其传递到闭包中以进一步处理记录.

While I cannot guarantee that I understand fully the concept of Cursor, but for Chunk, chunk runs the query at every record size, retrieving it, and passing it into the closure for further works on the records.

希望这很有用.

这篇关于laravel光标和laravel块方法有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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