[英]Rails 3 : ActiveRecord select query optimisation
我有一個rails3應用程序,正在其中嘗試優化一些SQL查詢。
是User.select([:id, name]).all
比User.all
快嗎?
是User.select([:id, :name]).all
比User.all快
select id, name from users
比select * from users
select id, name from users
select * from users
快 所有返回的記錄都由AR包裝,因此僅用2個值(id,name)初始化對象將花費較少的時間。
1.9.3p448 :002 > Benchmark.measure {User.select([:id, :name]).all } => 0.340000 0.010000 0.350000 ( 0.348017) 1.9.3p448 :003 > Benchmark.measure {User.all } => 0.770000 0.060000 0.830000 ( 0.935314)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.