Laravel渴望加载vs显式联接 [英] Laravel eager loading vs explicit join

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

问题描述

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

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中编写联接查询,这很不错,所以问题是:我是否错误地假设从两个或多个表中检索相关数据时,我不应该为急于加载而烦恼,而是只写我自己的加入语句?

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.

推荐答案

您的理解绝对正确.如果在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.

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

But when you manually call join method which belongs to Illuminate\Database\Query\Builder 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渴望加载vs显式联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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