2013-07-09 18 views
5

我剛剛在Laravel測試性能雄辯的ORM,並且發現一個簡單的查詢需要3秒鐘的時間才能執行,與在0.1秒內完成的正常Laravel查詢相比,我們感到震驚。我只返回1500條記錄。Eloquent ORM性能

DB::table('t_organisations')->get(); - 0.12253594398499 seconds 
Organisation::all(); - 3.6389181613922 seconds 

肯定這不能正常!?我認爲我沒有錯過任何設置。我的數據庫是正常化的。可能是什麼問題呢?

+0

就我個人而言,我會運行它通過XDebug來查看浪費的時間。 –

+1

您的模型中是否定義了任何關係?如果是這樣的話 - 雄辯需要做大量的連接... – Laurence

+0

總是有一些事情最好使用查詢生成器,特別是在處理大數據時。 – crynobone

回答

2

感謝您的回覆。

這裏是mysql查詢日誌的結果:

Organization :: all(); - 1.6772060394287秒

130710 9:52:43  5 Connect [email protected] 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 

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

130710 9:55:16  6 Connect [email protected] 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 

所以沒有什麼區別有那麼....這意味着延遲必須位於雄辯PHP代碼。是的,我安裝了xdebug,不,我不準備浪費我的時間,試圖找出它爲什麼慢!如果查詢生成器的速度更快,那對我來說已經足夠了!

@Laravels的開發人員:在框架上做的很好。它很直觀,很好地處理授權,特別是使用Leroy Merlin的信任和委託插件。雖然你可能想看看Eloquent的性能問題!

乾杯! Craig

+0

你是什麼意思Leroy Merlin? Entrust和Confide是由Zizaco [link](https://github.com/Zizaco/confide)編寫的 –

+0

Eloquent位於Query Builder的頂層,解析它返回到類實例中的數據。這總比使用Query Builder慢。你可以緩存雄辯的結果:「Organization :: all() - > remember(5);」。 – assertchris

2

確保在測試之間執行RESET QUERY CACHE以清除MySQL查詢緩存。從發佈的時間戳看起來,您首先進行了雄辯查詢,這意味着您在進行第二次測試時可能會緩存它們。這將解釋巨大的性能差異,儘管我懷疑Eloquent比正常的Laravel查詢慢一點點,因爲額外的開銷。

2

當你這樣做時 DB::table('t_organisations')->get(); 它將所有結果作爲一個數組(或多個對象)提取,但不會將它們吸收到模型中。如果您想快速解釋,請參閱this stackoverflow answer

當你 Organisation::all(); 水化過程發生,這就是爲什麼請求需要較長的時間(你必須分配內存中的所有對象,並與領域填充它們)。水合作用優化有很多鏈接/內容,可以幫助您更好地請求數據庫,並避免在不需要時水化物體。