【发布时间】:2013-05-14 19:31:51
【问题描述】:
假设我有 15 个类别和 6 个子类别,并且我有表项,其中我有一组记录,我必须按以下方式获取这些记录
category 1 ---> level 1 ---> 3 items with maximum price
category 1 ---> level 2 ---> 3 items with maximum price
...
...
...
category 15 ---> level 6 ---> 3 items with maximum price
和
@categories.each do |value|
@sub-categories.each do |value1|
array = Item.find(:all, :conditions => ["customer_id IN (?) AND category_id = ? AND sub-category_id = ?", @customer, value.id, value1.id], :order => 'price DESC', :limit => 3)
array.each do |value2|
@max_price_item_of_each_customer << value2
end
end
end
但这将花费大量时间,因为它会迭代。那么我怎样才能以这样的方式改变这一点,可以减少时间呢?任何帮助表示赞赏。
【问题讨论】:
-
你能举一个具体的例子来说明类别、级别和项目可能是什么吗?我很难理解你的数据结构。
标签: ruby-on-rails ruby-on-rails-3 performance query-optimization