执行单个数据库查询以查找页面所需的所有结果是否更快,然后使用if / else语句通过它执行多个循环以提取要在每个部分中显示的内容,或者执行多个数据库查询选择您需要的确切数据,然后分别循环遍历每个数据?
例如:
@completed = HostTask.find(:all, :include => [:task], :conditions =>["host_tasks.decommission_id = ? AND host_tasks.completed IS NOT NULL", params[:id]], :order => "tasks.position")
@incomplete = HostTask.find(:all, :include => [:task], :conditions =>["host_tasks.decommission_id = ? AND host_tasks.completed IS NULL", params[:id]], :order => "tasks.position")
然后循环显示每个。或者只是:
@tasks = HostTask.find(:all, :include => [:task], :conditions =>["host_tasks.decommission_id = ?", params[:id]], :order => "tasks.position")
通过@tasks循环两次,只显示@ tasks.completed是否为null然后不为空?
答案 0 :(得分:1)
答案 1 :(得分:1)
我认为单一查询作为数据库连接并且调用很昂贵会“更快”。您使用的是应用服务器还是其他连接池?这可能有所帮助 取决于您带回的数据量。