From a196c432533b79af82286fc96b3aa00a70f5f065 Mon Sep 17 00:00:00 2001 From: Matchu Date: Fri, 19 Nov 2010 16:31:58 -0500 Subject: [PATCH] stop -type:x queries from lagging out due to group by optimization blah --- app/models/item.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/models/item.rb b/app/models/item.rb index 5a55f38e..5c19f02b 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -359,7 +359,7 @@ class Item < ActiveRecord::Base # SWFs that don't match end up being NULL rows. Then we take the max SWF # asset ID, which is NULL if and only if there are no rows that matched # the zone requirement. If that max was NULL, return the object. - scope.joins( + item_ids = select(arel_table[:id]).joins( "LEFT JOIN #{ParentSwfAssetRelationship.table_name} #{psa.name} ON " + psa[:swf_asset_type].eq(SwfAssetType) .and(psa[:parent_id].eq(arel_table[:id])) @@ -373,7 +373,9 @@ class Item < ActiveRecord::Base .to_sql ). group("#{table_name}.id"). - having("MAX(#{sa.name}.id) IS NULL") # SwfAsset.arel_table[:id].maximum has no #eq + having("MAX(#{sa.name}.id) IS NULL"). # SwfAsset.arel_table[:id].maximum has no #eq + map(&:id) + scope.where(arel_table[:id].in(item_ids)) } class Condition < String