考虑我有 15 个类别,每个类别有 6 个子类别,现在我有项目表,我必须根据最新购买日期从每个子类别中找到 3 个项目。
category 1 ---> level 1 ---> 3 items with latest date
category 1 ---> level 2 ---> 3 items with latest date
...
...
...
category 15 ---> level 5 ---> 3 items with latest date
category 15 ---> level 6 ---> 3 items with latest date
我努力了
@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 => 'created_at DESC', :limit => 3)
array.each do |value2|
@latest_item_of_each_customer << value2
end
end
end
这将作为 15 个类别 x 6 个子类别重复 90 次,因此需要很长时间。请告诉我如何通过有效的查询来减少时间。