如何在Laravel应用程序中更快地处理大型数据库? [英] How to handle large database in Laravel application faster?

查看:60
本文介绍了如何在Laravel应用程序中更快地处理大型数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在laravel应用程序中更快地处理大型mysql数据库的最佳方法是什么.我有一张桌子,有大约6,00,000条记录.有什么最佳实践来处理这些数据,以便以优化,更快的方式选择或查询该表.

What is the best way to process large mysql database faster in laravel application. I have a table which has round 6,00,000 records. What could be best practices to handle this data to select or query this table in optimised and faster way.

推荐答案

如果您要检索特定的一行或一组行.您不必担心.会更快.但是,如果您尝试从表中检索所有行并处理其数据,最好使用 chunk

If you are trying to retrieve a specific row or set of rows. you need not to worry. It will be faster. but if you are trying to retrieve all the rows from the table and manipulate its data, its better to use chunk

Model::chunk(100, function ($chunked_results) {
   //do your work here
});

这将每次检索100行并将它们传递给回调函数.

This will retrieve 100 rows each time and pass them to the callback function.

如果您与他们有关系,最好将它们加载为

if you have relations with them, it is better to eager load them as

Model::with('relations')->chunk(100, function ($chunked_results) {
   //do your work here
});

请参见大块

这篇关于如何在Laravel应用程序中更快地处理大型数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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