Merge pull request #3671 from ulferts/fix/performance_on_activity

limit aggregation to one journable
pull/3673/head
Oliver Günther 9 years ago
commit e99872a904
  1. 78
      app/models/journal/aggregated_journal.rb

@ -50,10 +50,14 @@ class Journal::AggregatedJournal
raw ? Journal::AggregatedJournal.new(raw) : nil raw ? Journal::AggregatedJournal.new(raw) : nil
end end
# Returns the aggregated journal that contains the vanilla/pure journal with the specified id.
def with_notes_id(notes_id) def with_notes_id(notes_id)
raw_journal = query_aggregated_journals # We need to limit the journal aggregation as soon as possible for performance reasons.
.where("#{table_name}.id = ?", notes_id) # Therefore we have to provide the notes_id to the aggregation on top of it being used
.first # in the where clause to pick the desired AggregatedJournal.
raw_journal = query_aggregated_journals(journal_id: notes_id)
.where("#{table_name}.id = #{notes_id}")
.first
raw_journal ? Journal::AggregatedJournal.new(raw_journal) : nil raw_journal ? Journal::AggregatedJournal.new(raw_journal) : nil
end end
@ -77,7 +81,7 @@ class Journal::AggregatedJournal
} }
end end
def query_aggregated_journals(journable: nil, until_version: nil) def query_aggregated_journals(journable: nil, until_version: nil, journal_id: nil)
# Using the roughly aggregated groups from :sql_rough_group we need to merge journals # Using the roughly aggregated groups from :sql_rough_group we need to merge journals
# where an entry with empty notes follows an entry containing notes, so that the notes # where an entry with empty notes follows an entry containing notes, so that the notes
# from the main entry are taken, while the remaining information is taken from the # from the main entry are taken, while the remaining information is taken from the
@ -90,10 +94,10 @@ class Journal::AggregatedJournal
# that our own row (master) would not already have been merged by its predecessor. If it is # that our own row (master) would not already have been merged by its predecessor. If it is
# (that means if we can find a valid predecessor), we drop our current row, because it will # (that means if we can find a valid predecessor), we drop our current row, because it will
# already be present (in a merged form) in the row of our predecessor. # already be present (in a merged form) in the row of our predecessor.
Journal.from("(#{sql_rough_group(journable, until_version, 1)}) #{table_name}") Journal.from("(#{sql_rough_group(1, journable, until_version, journal_id)}) #{table_name}")
.joins("LEFT OUTER JOIN (#{sql_rough_group(journable, until_version, 2)}) addition .joins("LEFT OUTER JOIN (#{sql_rough_group(2, journable, until_version, journal_id)}) addition
ON #{sql_on_groups_belong_condition(table_name, 'addition')}") ON #{sql_on_groups_belong_condition(table_name, 'addition')}")
.joins("LEFT OUTER JOIN (#{sql_rough_group(journable, until_version, 3)}) predecessor .joins("LEFT OUTER JOIN (#{sql_rough_group(3, journable, until_version, journal_id)}) predecessor
ON #{sql_on_groups_belong_condition('predecessor', table_name)}") ON #{sql_on_groups_belong_condition('predecessor', table_name)}")
.where('predecessor.id IS NULL') .where('predecessor.id IS NULL')
.order("COALESCE(addition.created_at, #{table_name}.created_at) ASC") .order("COALESCE(addition.created_at, #{table_name}.created_at) ASC")
@ -156,34 +160,62 @@ class Journal::AggregatedJournal
# To be able to self-join results of this statement, we add an additional column called # To be able to self-join results of this statement, we add an additional column called
# "group_number" to the result. This allows to compare a group resulting from this query with # "group_number" to the result. This allows to compare a group resulting from this query with
# its predecessor and successor. # its predecessor and successor.
def sql_rough_group(journable, until_version, uid) def sql_rough_group(uid, journable, until_version, journal_id)
if until_version && !journable if until_version && !journable
raise 'need to provide a journable, when specifying a version limit' raise 'need to provide a journable, when specifying a version limit'
elsif journable && journable.id.nil?
raise 'journable has no id'
end end
sql = "SELECT predecessor.*, #{sql_group_counter(uid)} AS group_number conditions = additional_conditions(journable, until_version, journal_id)
"SELECT predecessor.*, #{sql_group_counter(uid)} AS group_number
FROM #{sql_rough_group_from_clause(uid)} FROM #{sql_rough_group_from_clause(uid)}
LEFT OUTER JOIN journals successor #{sql_rough_group_join(conditions[:join_conditions])}
ON predecessor.version + 1 = successor.version AND #{sql_rough_group_where(conditions[:where_conditions])}"
predecessor.journable_type = successor.journable_type AND end
predecessor.journable_id = successor.journable_id
#{until_version ? " AND successor.version <= #{until_version}" : ''} def additional_conditions(journable, until_version, journal_id)
WHERE (predecessor.user_id != successor.user_id OR where_conditions = ''
(predecessor.notes != '' AND predecessor.notes IS NOT NULL) OR join_conditions = ''
#{sql_beyond_aggregation_time?('predecessor', 'successor')} OR
successor.id IS NULL)"
if journable if journable
raise 'journable has no id' if journable.id.nil? where_conditions += " AND predecessor.journable_type = '#{journable.class.name}' AND
sql += " AND predecessor.journable_type = '#{journable.class.name}' AND predecessor.journable_id = #{journable.id}"
predecessor.journable_id = #{journable.id}"
if until_version if until_version
sql += " AND predecessor.version <= #{until_version}" where_conditions += " AND predecessor.version <= #{until_version}"
join_conditions += "AND successor.version <= #{until_version}"
end end
end end
sql if journal_id
where_conditions += "AND predecessor.id IN (
SELECT id_key.id
FROM #{table_name} id_key JOIN #{table_name} journable_key
ON id_key.journable_id = journable_key.journable_id
AND id_key.journable_type = journable_key.journable_type
AND journable_key.id = #{journal_id})"
end
{ where_conditions: where_conditions,
join_conditions: join_conditions }
end
def sql_rough_group_join(additional_conditions)
"LEFT OUTER JOIN #{table_name} successor
ON predecessor.version + 1 = successor.version AND
predecessor.journable_type = successor.journable_type AND
predecessor.journable_id = successor.journable_id
#{additional_conditions}"
end
def sql_rough_group_where(additional_conditions)
"WHERE (predecessor.user_id != successor.user_id OR
(predecessor.notes != '' AND predecessor.notes IS NOT NULL) OR
#{sql_beyond_aggregation_time?('predecessor', 'successor')} OR
successor.id IS NULL)
#{additional_conditions}"
end end
# The "group_number" required in :sql_rough_group has to be generated differently depending on # The "group_number" required in :sql_rough_group has to be generated differently depending on

Loading…
Cancel
Save