022 贪婪加载(Eager loading)

Eager Loading

One way to improve performance is to cut down . the number of SQL queries. You can do this through eager loading. Learn all about it in this episode!
 
提高性能的一个途径就是减少SQL查询的次数,可以通过贪婪加载来实现。这一节就来讲述相关内容。
 
---
这个很简单,就是当查询某一个类的对象时,加上include参数,include是一个数组,数组里包含这个模型类关联的模型类的对象。
 
譬如对于下面三个模型类:
 
Task:
has_many :comments
belongs_to :project
 
Project:
has_many :tasks
Comment:
belongs_to :user
User:
has_many :comments
 
Task.find(:all, :include => :projects)
Task.find(:all, :include => [:projects, :comments])
Task.find(:all, :include => [:projects, {:comments => :user}])
 
 

你可能感兴趣的:(loading,休闲,Eager,railscasts,022)