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

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

问题描述

我在Laravel查询生成器和雄辩者之间进行了一些性能测试.使用各种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?

推荐答案

雄辩是Laravel对Active Record模式的实现,它具有所有优点和缺点.当您以CRUD方式处理单个实体时,即从数据库中读取或创建一个新实体,然后保存或删除它,这是一个很好的解决方案.您将从Eloquent的功能中受益匪浅,例如脏检查(仅针对已更改的字段发送SQL UPDATE),模型事件(例如,在有人创建新帐户时发送管理警报或更新统计信息计数器),特征(时间戳,软删除,自定义特征)渴望/延迟加载等.

Eloquent is Laravel's implementation of Active Record pattern and it comes with all its strengths and weaknesses. It is a good solution to use when you process a single entity in a CRUD manner - that is, read from database or create a new entity and then save it or delete. 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 alert or update statistics counter when someone has created a new account), traits (timestamps, soft deletes, custom traits) eager/lazy loading etc.

但是,正如您已经知道的那样,它带有一些性能价格.当您处理单个或仅几个记录时,无需担心.但是对于读取大量记录的情况(例如,用于数据网格,用于报告,用于批处理等),普通DB是更好的方法.

But, as you already know, it comes with some performance price. When you process a single or just 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 DB is better approach.

对于我们的应用程序,我们正是这样做的-在Web表单中使用Laravel的Eloquent处理单个记录,并使用DB(带有SQL视图)检索网格数据,导出数据等.

For our application we are doing exactly that - using Laravel's Eloquent in web forms to process a single record and using DB (with SQL views) to retrieve data for grids, export etc.

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

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