雄辩的ORM表现

Cra*_*gan 5 laravel eloquent

我刚刚在Laravel中测试了Eloquent ORM的性能,并且惊讶地发现一个简单的查询需要花费超过3秒的时间来执行,而普通的Laravel查询在0.1秒内完成.我只返回1500条记录.

DB::table('t_organisations')->get();  -  0.12253594398499 seconds
Organisation::all();   -   3.6389181613922 seconds
Run Code Online (Sandbox Code Playgroud)

当然这不正常!?我不认为我在设置中遗漏了任何东西.我的数据库已标准化.可能是什么问题呢?

hil*_*ius 5

当您这样做时, DB::table('t_organisations')->get(); 它会将所有结果作为数组(或对象)获取,但不会将它们混合到模型中。如果您需要快速解释,请参阅此 stackoverflow 答案

当你做 Organisation::all(); Hydration 过程发生时,这就是为什么请求需要更长的时间(你必须分配内存中的所有对象并用字段填充它们)。有很多关于 hydration 优化的链接/tuts 可以帮助您更好地请求数据库,并避免在不需要时对象的 hydration。


Cra*_*gan 2

感谢你的回复。

下面是mysql查询日志的结果:

组织::全部(); - 1.6772060394287 秒

130710  9:52:43     5 Connect   seltec@localhost on seltec
            5 Prepare   set names 'utf8' collate 'utf8_unicode_ci'
            5 Execute   set names 'utf8' collate 'utf8_unicode_ci'
            5 Close stmt    
            5 Prepare   select * from `users` where `id` = ? limit 1
            5 Execute   select * from `users` where `id` = '2' limit 1
            5 Close stmt    
            5 Prepare   select * from `t_organisations`
            5 Execute   select * from `t_organisations`
130710  9:52:44     5 Close stmt    
130710  9:52:45     5 Quit
Run Code Online (Sandbox Code Playgroud)

DB::table('t_organizations')->get(); - 0.13963603973389 秒

130710  9:55:16     6 Connect   seltec@localhost on seltec
            6 Prepare   set names 'utf8' collate 'utf8_unicode_ci'
            6 Execute   set names 'utf8' collate 'utf8_unicode_ci'
            6 Close stmt    
            6 Prepare   select * from `users` where `id` = ? limit 1
            6 Execute   select * from `users` where `id` = '2' limit 1
            6 Close stmt    
            6 Prepare   select * from `t_organisations`
            6 Execute   select * from `t_organisations`
            6 Close stmt    
            6 Quit
Run Code Online (Sandbox Code Playgroud)

所以没有区别......这意味着延迟必须存在于 Eloquent php 代码中。是的,我安装了 xdebug,不,我不准备浪费时间试图找出它为什么慢!如果查询生成器中的速度更快,那对我来说就足够了!

@Laravels 的开发人员:框架做得很好。它很直观,可以很好地处理授权,特别是使用 Leroy Merlin 的 confide 和 entrust 插件。不过,您可能想看看 Eloquent 的性能问题!

干杯! 克雷格