Laravel Eloquent vs DB facade:为什么使用Eloquent会降低性能? [已关闭]

7 浏览
0 Comments

Laravel Eloquent vs DB facade:为什么使用Eloquent会降低性能? [已关闭]

我对比了Laravel的DB门面查询构建器和Laravel的Eloquent ORM的性能测试。对于许多SQL语句(SELECT、UPDATE、DELETE、INSERT),DB门面比Eloquent要快得多。那么为什么有人会选择较慢的Laravel Eloquent而不是更快的DB门面呢?

0
0 Comments

问题原因:为什么要使用Laravel Eloquent而不是DB facade?

问题解决方法:使用Laravel Eloquent的性能问题如何解决?

Laravel Eloquent vs DB facade: Why use Eloquent and decrease performance? [closed]

Laravel提供了两种数据访问方式,一种是使用DB facade,另一种是使用Eloquent。那么为什么我们应该使用Eloquent而不是DB facade呢?虽然Eloquent相对于DB facade在性能方面存在一些问题,但是它也有一些优势。

首先,使用Eloquent可以编写面向对象的代码,这使得代码更加易于维护和扩展。相比之下,使用DB facade需要编写原始的SQL语句,这样的代码可读性较差,也不便于后续的修改和维护。

其次,Eloquent的使用比编写原始SQL或使用DB facade更加简单。它不需要与表结构进行绑定,例如,如果要更改表名,只需在Eloquent模型中更改表名即可,而无需修改任何Eloquent查询。这样可以大大减少后续的修改工作量。

此外,Eloquent还可以优雅地维护表之间的关系。只需在Eloquent查询中指定关系类型(如JOIN、LEFT JOIN、RIGHT JOIN等)即可从相关表中获取数据。这样可以使代码更加简洁易读。

另外,Eloquent查询比原始SQL或DB facade更具可读性。使用Eloquent,我们可以在模型中使用方法、作用域、访问器、修饰器等等,这是一种更加可维护的模式。

虽然Eloquent相对于DB facade在性能方面存在一些问题,但我们可以通过一些方法来解决这个问题。例如,我们可以使用Eloquent的延迟加载功能来减少查询次数,或者使用原始SQL语句来执行复杂的查询。此外,我们还可以优化数据库索引、调整数据库配置等来提升性能。

,尽管使用Eloquent可能会降低性能,但是它提供了一种更加面向对象、易于使用和易于维护的数据访问方式。对于大多数情况下,这种性能损失是可以接受的。如果需要执行复杂的查询或需要特定的性能要求,我们可以考虑使用原始SQL语句或DB facade来达到更高的性能。

0
0 Comments

Laravel Eloquent vs DB facade: 为什么使用Eloquent会降低性能?

在某些情况下,你是正确的。

当处理更多数据时,使用Laravel的DB facade查询构建器比使用Laravel的Eloquent ORM更好。

根据性能测试,将1000行插入简单表格中,Eloquent需要1.2秒,而DB facade只需要800毫秒。

那么为什么还要使用Eloquent呢?Eloquent也很重要,因为:

  • 它的语法比DB facade更简单。

  • 对于不了解SQL的开发人员来说更容易使用。使用DB facade需要知道SQL。

  • 使用Eloquent编写的代码更易读,因此更易于维护,而使用DB facade编写的代码不太易读:

    // 使用Eloquent
    $student = App\Student::find($id);
    // 使用DB facade
    $student = DB::table('student')->where('id', $id)->first();
    

  • 如果要更改数据库,使用Laravel Eloquent会更容易,因为它可以处理许多不同的数据库,而DB facade需要编写可能需要为不同的数据库重新编写的SQL。

因此,当您在处理简单的CRUD操作的小型网站时,请使用Eloquent,而在使用许多连接和其他Eloquent不支持的功能时,请使用DB facade。

现实生活中的例子:

  1. 您正在制作一个包含5000名教师、10000名学生以及一些通知和文件的大学网站。使用Laravel Eloquent构建此网站会更简单和易读。

  2. 您正在制作一个高流量的网站,如Stack Overflow,每个月有超过1亿人访问,有超过7000万篇帖子。使用DB facade会更好,因为它更快,会导致显著更快的响应时间。

您可以使用Laravel Debugbar来检查查询性能。

这里是有关Eloquent和DB facade之间性能、内存消耗和代码质量的完整比较。

0
0 Comments

Laravel Eloquent vs DB facade: Why use Eloquent and decrease performance?

Laravel's Eloquent is an implementation of the Active Record pattern, which has its own strengths and weaknesses. Active Record is useful for CRUD operations on a single entity, such as creating, reading, updating, and deleting records.

Eloquent offers various features that can be beneficial, including dirty checking (to only update fields that have changed), model events (for administrative alerts or statistic updates), traits (timestamps, soft deletes, custom traits), and eager/lazy loading. It also allows for the implementation of business logic within the Active Record entities, such as validation, relation management, and calculations.

However, using Eloquent comes with a performance cost. When dealing with a small number of records, this is not a concern. But when working with large datasets, such as datagrids, reports, or batch processing, using the plain Laravel DB facade methods is a more efficient approach.

For Laravel-based applications, a combination of both approaches can be used. Eloquent is suitable for processing a single record in UI forms, while the DB facade methods (backed by SQL views with performance tweaks) can be used to retrieve data for UI tables, export tasks, and RESTful APIs.

Eager loading can somewhat improve performance, but it still involves using the Eloquent (Active Record) object with all its "heavy stuff". Laravel does provide convenience methods on Eloquent models to make SQL queries and retrieved objects lighter, but this deviates from pure Eloquent and becomes a hybrid of Eloquent and QueryBuilder.

Eloquent is ideal for typical CRUD operations on a single model or simple JOINs with a small number of records. However, when dealing with complex joins and a large number of records in a transaction, the query builder methods are more performant. Alternatively, raw SQL queries can be used for the most optimal performance.

One challenge with using Eloquent is the instantiation of models. Laravel creates a new instance of the model for each row returned, which can add up when dealing with thousands of records. In cases where only specific fields are required, using the DB facade's select method would be faster.

Regarding pagination, Eloquent can be a suitable option as it allows for loading a limited number of records as Eloquent models. However, for large grids with read-only views, it is more efficient to create an SQL view that resolves all joins and returns simple flat maps that can be processed using Laravel's DB mechanisms. This eliminates the need for heavy Eloquent models in such scenarios.

In conclusion, while Eloquent is powerful and convenient for certain scenarios, it may not always be the best choice when performance is a priority. The plain Laravel DB facade methods or raw SQL queries can offer better performance when dealing with large datasets or complex operations. By using a combination of both approaches, developers can optimize performance based on the specific requirements of their applications.

0