Laravel Eloquent 与查询构建器 - 为什么使用 eloquent 来降低性能 [英] Laravel Eloquent vs query builder - Why use eloquent to decrease performance

查看:44
本文介绍了Laravel Eloquent 与查询构建器 - 为什么使用 eloquent 来降低性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Laravel 查询构建器和 eloquent 之间做了一些性能测试.使用各种 sql 语句(select-update-delete-insert)查询生成器要快得多.

I did some performance test between Laravel query builder and eloquent. Query builder was much faster with various of sql statement (select-update-delete-insert).

所以我的问题是:为什么有人使用 Laravel Eloquent 来对抗普通查询构建器?

So my question is: Why someone uses Laravel Eloquent against plain query builder?

推荐答案

Eloquent 是 Laravel 对 Active Record 模式的实现,它具有所有优点和缺点.

Eloquent is Laravel's implementation of Active Record pattern and it comes with all its strengths and weaknesses.

Active Record 是以 CRUD 方式处理单个实体的一个很好的解决方案 - 即,创建一个具有填充属性的新实体,然后将其保存到数据库,从数据库加载记录或删除.

Active Record is a good solution for processing a single entity in CRUD manner - that is, create a new entity with filled properties and then save it to a database, load a record from a database, or delete.

您将从 Eloquent 的功能中受益匪浅,例如脏检查(仅针对已更改的字段发送 SQL 更新)、模型事件(例如,在有人创建新帐户时发送管理警报或更新统计计数器), 特征(时间戳、软删除、您的自定义特征)急切/延迟加载等.您还可以应用域驱动模式并在您的 Active Record 实体中实现一些业务逻辑,例如,验证、管理关系、计算等.

You will benefit a lot from Eloquent's features such as dirty checking (to send SQL UPDATE only for the fields which have been changed), model events (e.g. to send administrative alerts or update statistics counters when someone has created a new account), traits (timestamps, soft deletes, your custom traits) eager/lazy loading etc. You can also apply domain-driven pattern and implement some pieces of business logic in your Active Record entities, for example, validation, managing relations, calculations etc.

但是,正如您已经知道的,Active Record 带来了一些性能代价.

But, as you already know, Active Record comes with some performance price.

当你处理单条记录或几条记录时,没有什么可担心的.但是对于读取大量记录的情况(例如对于数据网格、报表、批处理等),普通的 Laravel DB 方法是更好的方法.

When you process a single record or a few records, there is nothing to worry about. But for cases when you read lots of records (e.g. for datagrids, for reports, for batch processing etc.) the plain Laravel DB methods is a better approach.

对于基于 Laravel 的应用程序,我们将使用两种我们认为合适的方法.我们使用 Laravel 的 Eloquent for UI 表单来处理单个记录并使用 DB 方法(由 SQL 视图支持,并具有额外的数据库引擎特定性能调整)来检索 UI 表、导出任务等的数据.它也可以工作RESTful API 很好 - Eloquent for GET, PUT, POST, DELETE with a key and DB for GET without key but with filters and sorts and paging.

For our Laravel based applications we are using both approaches as we see appropriate. We use Laravel's Eloquent for UI forms to process a single record and use DB methods (backed by SQL views with additional database engine specific performance tweaks) to retrieve data for UI tables, export tasks etc. It also works well with RESTful APIs - Eloquent for GET, PUT, POST, DELETE with a key and DB for GET without key but with filters and sorting and paging.

这篇关于Laravel Eloquent 与查询构建器 - 为什么使用 eloquent 来降低性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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