Laravel 急切加载与显式连接

Laravel eager loading vs explicit join(Laravel 急切加载与显式连接)

本文介绍了Laravel 急切加载与显式连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来像是一个显而易见的问题,但我只是想得到一些保证.

This might sound like an obvious question but I just want to get some reassurance.

使用 Laravel 的预先加载功能,据我所知,它将创建两个查询以返回相关结果的完整列表(假设您正在处理两个表).但是,如果我错了,请纠正我,使用 join 语句将使您只剩下一个查询,这会减少到服务器数据库 (MySQL) 的往返次数,并且是一个更有效的查询.

Using Laravel's eager loading functionality, from what I understand it will create two queries to return a whole list of related results (say if you're working with two tables). However, and correct me if I'm wrong, using a join statement will leave you with only one query, which creates one less round trip to the server's database (MySQL) and is a more efficient query.

我知道你可以在 Laravel 中编写连接查询,这很棒,所以问题是:我假设从两个或多个表中检索相关数据时,我是否不应该为急切加载而烦恼,而只是编写我自己的 join 语句?

I know that you can write join queries in Laravel, which is great, so the question is: am I incorrect to assume that when retrieving related data from two or more tables, should I not bother with eager loading and instead just write my own join statements?

****** 编辑 *******

****** Edit *******

一年后回到这个问题,我个人认为,只写查询,原始的,写得好.

Coming back to this one year later, I'd say in my personal opinion, just write the queries, raw, and write them well.

******** 编辑 2 *********

******** Edit 2 *********

好吧,现在六年过去了,我一直在为此获得积分.

Okay now six years later, I keep getting points for this.

我是否从一开始就不清楚,与我上面所说的相反,Eloquent 在这一点上写了很好的查询.使用 Eloquent - 即使存在轻微的查询效率低下,它也可以让您编写非常清晰、可维护的代码,在我职业生涯的这个阶段,我认为这在大多数情况下更为重要案件.仅在性能增强至关重要且您可以衡量影响的情况下编写原始查询和优化.

Whether I was unclear from the beginning or not, contrary to what I've said above, Eloquent at this point writes great queries. Use Eloquent - even if there's a slight query inefficiency, it allows you to write very clear, maintainable code which at this point in my career I would argue is more important in most cases. Only write raw queries and optimize in cases where performance enhancements are critical and you can measure the impact.

推荐答案

你的理解完全正确.如果您在 Laravel 中使用 join() 编写了一个 join 语句来连接两个或多个表,那么它只会进行一个查询,其中使用了 Eloquent 模型和 eager loading 技术需要多个查询.

You are absolutely right about your understanding. If you write a join statement to join two or more tables using join() in Laravel then it makes only one query where using an Eloquent model with eager loading technique requires more than one query.

我应该不去急于加载而是自己编写连接语句

should I not bother with eager loading and instead just write my own join statements

实际上,eager loading 是一种使用 Eloquent ORM 轻松加载相关模型的技术,它在幕后使用 Query Builder 并让您使用 Eloquent Model Object 而无需自己进行查询并以不同的方式表示数据,使用 Eloquent ORM 您可以直接与表示数据库中对象的模型进行交互附加功能.最重要的是,它隐藏了 SQL 的复杂性,并允许您使用 PHP 代码以 OOP 方式进行数据库查询.

Actually, the eager loading is a technique to load related models using Eloquent ORM easily and it uses Query Builder behind the scene and lets you use Eloquent Model Object without making the query by your self and represents the data differently, Using Eloquent ORM you are able to interact with model directly which represent objects in the database with additional features. Most importantly, it hides the complexity of SQL and allows you to do the database query in an OOP fashion using PHP code.

但是当您手动调用属于 IlluminateDatabaseQueryBuilder 类的 join 方法时,您将直接使用 Query Builder这需要您编写更多代码并需要更多sql query 知识,因为它不会对您隐藏查询,而是帮助您更精确地进行查询,但您仍然可以进行查询.

But when you manually call join method which belongs to IlluminateDatabaseQueryBuilder class then you are using the Query Builder directly which requires you write more code and requires more knowledge of sql query because it doesn't hide the query from you but helps you make queries more precisely, but you still make queries.

两者都是不同的东西,它们的工作方式也不同.您可以使用术语 ORM vs Query BuilderGoogle 上进行搜索.

Both are different things and they work differently. You may search on Google using the term ORM vs Query Builder.

这篇关于Laravel 急切加载与显式连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Laravel 急切加载与显式连接

基础教程推荐