Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer includes over preload #1710

Merged
merged 4 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def read_scope(spec)
stream = stream.where(newer_than_or_equal_condition(spec)) if spec.newer_than_or_equal
stream.order(id: order(spec))
else
stream = @stream_klass.preload(:event).where(stream: spec.stream.name)
stream = @stream_klass.includes(:event).where(stream: spec.stream.name)
stream = stream.where(event_id: spec.with_ids) if spec.with_ids?
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?
Expand Down
42 changes: 41 additions & 1 deletion ruby_event_store-active_record/spec/event_repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module ActiveRecord

it_behaves_like :event_repository, mk_repository, helper


let(:repository) { mk_repository.call }
let(:specification) do
Specification.new(
Expand Down Expand Up @@ -277,6 +276,47 @@ module ActiveRecord
}x
end

specify 'inner join events when event filtering criteria present' do
[
specification.stream("stream").of_type("type"),
specification.stream("stream").as_of,
specification.stream("stream").as_at,
specification.stream("stream").older_than(Time.now),
specification.stream("stream").older_than_or_equal(Time.now),
specification.stream("stream").newer_than(Time.now),
specification.stream("stream").newer_than_or_equal(Time.now),
].each do |spec|
expect {
repository.read(spec.result).to_a
}.to match_query /INNER\s+JOIN\s+.event_store_events./
end
end

specify "avoid N+1 without additional query when join is used for querying" do
repository.append_to_stream(
[SRecord.new, SRecord.new],
Stream.new("stream"),
ExpectedVersion.auto
)
expect { repository.read(specification.stream("stream").of_type("type").result) }.to match_query_count(1)
expect { repository.read(specification.stream("stream").as_of.result) }.to match_query_count(1)
expect { repository.read(specification.stream("stream").as_at.result) }.to match_query_count(1)
expect { repository.read(specification.stream("stream").older_than(Time.now).result) }.to match_query_count(1)
expect { repository.read(specification.stream("stream").older_than_or_equal(Time.now).result) }.to match_query_count(1)
expect { repository.read(specification.stream("stream").newer_than(Time.now).result) }.to match_query_count(1)
expect { repository.read(specification.stream("stream").newer_than_or_equal(Time.now).result) }.to match_query_count(1)
end

specify "don't join events when no event filtering criteria when counting" do
expect {
repository.count(specification.stream("stream").result)
}.to match_query %r{
SELECT\s+COUNT\(\*\)\s+
FROM\s+.event_store_events_in_streams.\s+
WHERE\s+.event_store_events_in_streams.\..stream.\s+=\s+(\?|\$1|'stream')
}x
end

private

def with_precision(time)
Expand Down