Skip to content

Commit

Permalink
Join event_store_events table only when required
Browse files Browse the repository at this point in the history
For very large streams (30k+ events) joining with event_store_events
adds certain overhead. This is totally unnecessary when filtering by
conditions not related to event_store_events.

https: //github.com//issues/1517

Co-authored-by: Paweł Pacana <[email protected]>
Co-authored-by: Łukasz Reszke <[email protected]>
  • Loading branch information
3 people committed Jan 13, 2023
1 parent 7f098c5 commit 8caeb4e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,17 @@ def read_scope(spec)
else
stream = @stream_klass.preload(:event).where(stream: spec.stream.name)
stream = stream.where(event_id: spec.with_ids) if spec.with_ids?
stream = stream.where(@event_klass.table_name => { event_type: spec.with_types }) if spec.with_types?
stream = stream.joins(:event)
stream = stream.order(as_at(spec)) if spec.time_sort_by_as_at?
stream = stream.order(as_of(spec)) if spec.time_sort_by_as_of?
stream = stream.joins(:event).where(@event_klass.table_name => { event_type: spec.with_types }) if spec.with_types?
stream = stream.joins(:event).order(as_at(spec)) if spec.time_sort_by_as_at?
stream = stream.joins(:event).order(as_of(spec)) if spec.time_sort_by_as_of?
stream = stream.order(id: order(spec))
stream = stream.limit(spec.limit) if spec.limit?
stream = stream.where(start_condition(spec)) if spec.start
stream = stream.where(stop_condition(spec)) if spec.stop
stream = stream.where(older_than_condition(spec)) if spec.older_than
stream = stream.where(older_than_or_equal_condition(spec)) if spec.older_than_or_equal
stream = stream.where(newer_than_condition(spec)) if spec.newer_than
stream = stream.where(newer_than_or_equal_condition(spec)) if spec.newer_than_or_equal
stream = stream.joins(:event).where(older_than_condition(spec)) if spec.older_than
stream = stream.joins(:event).where(older_than_or_equal_condition(spec)) if spec.older_than_or_equal
stream = stream.joins(:event).where(newer_than_condition(spec)) if spec.newer_than
stream = stream.joins(:event).where(newer_than_or_equal_condition(spec)) if spec.newer_than_or_equal
stream
end
end
Expand Down
6 changes: 6 additions & 0 deletions ruby_event_store-active_record/spec/event_repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,12 @@ module ActiveRecord
}.to match_query /SELECT\s+.event_store_events.\..id. FROM .event_store_events.*/
end

specify "don't join events when no event filtering criteria" do
expect {
repository.read(specification.stream("stream").result).to_a
}.to match_query /SELECT\s+.event_store_events_in_streams.\.\*\s+FROM\s+.event_store_events_in_streams.\s+WHERE\s+.event_store_events_in_streams.\..stream.\s+=\s+\?\s+ORDER\s+BY\s+.event_store_events_in_streams.\..id.\s+ASC/
end

private

def with_precision(time)
Expand Down

0 comments on commit 8caeb4e

Please sign in to comment.