optimizations when doing pluck / calculations

This commit is contained in:
Dylan Knutson
2025-07-29 14:54:37 +00:00
parent 3704239a6c
commit b4fd614280
5 changed files with 206 additions and 65 deletions

View File

@@ -15,41 +15,129 @@ RSpec.describe "loading optimizations" do
)
end
it "queries only the aux table if no main table columns are referenced" do
queries =
SpecHelper.capture_queries do
expect(Car.pluck(:fuel_type)).to eq(%w[gasoline hybrid electric])
end
expect(queries.length).to eq(1)
expect(queries.first).not_to include("JOIN")
shared_examples "queries only the aux table" do
it "queries only the aux table" do
expect(@queries.length).to eq(1)
expect(@queries.first).not_to include("JOIN")
expect(@queries.first).to match(/\bvehicles_car_aux\b/)
expect(@queries.first).not_to match(/\bvehicles\b/)
end
end
it "queries only the aux table if all columns are on the aux table" do
queries =
SpecHelper.capture_queries do
expect(Car.where(engine_size: 1.4..1.9).pluck(:fuel_type)).to eq(
%w[hybrid electric]
)
end
expect(queries.length).to eq(1)
expect(queries.first).not_to include("JOIN")
expect(queries.first).to include("BETWEEN")
expect(queries.first).to match(/\bvehicles_car_aux\b/)
expect(queries.first).not_to match(/\bvehicles\b/)
shared_examples "queries both tables" do
it "queries both tables" do
expect(@queries.length).to eq(1)
expect(@queries.first).to include("JOIN")
expect(@queries.first).to match(/\bvehicles\b/)
expect(@queries.first).to match(/\bvehicles_car_aux\b/)
end
end
it "queries both tables if main table column is referenced" do
queries =
SpecHelper.capture_queries do
rel = Car.where(name: "Toyota Camry")
rel = rel.pluck(:fuel_type)
expect(rel).to eq(%w[gasoline])
describe "pluck" do
context "aux columns are referenced" do
before do
@queries =
SpecHelper.capture_queries do
expect(Car.pluck(:fuel_type)).to eq(%w[gasoline hybrid electric])
end
end
expect(queries.length).to eq(1)
expect(queries.first).to include("JOIN")
it_behaves_like "queries only the aux table"
end
context "aux columns are chained on a where clause" do
before do
@queries =
SpecHelper.capture_queries do
expect(Car.where(engine_size: 1.4..1.9).pluck(:fuel_type)).to eq(
%w[hybrid electric]
)
end
end
it_behaves_like "queries only the aux table"
it "applies the BETWEEN clause" do
expect(@queries.first).to include("BETWEEN")
end
end
context "main table columns are referenced" do
before do
@queries =
SpecHelper.capture_queries do
expect(Car.where(name: "Toyota Camry").pluck(:fuel_type)).to eq(
%w[gasoline]
)
end
end
it_behaves_like "queries both tables"
end
context "main table columns are chained on a where clause" do
before do
@queries =
SpecHelper.capture_queries do
expect(Car.where(name: "Toyota Camry").pluck(:fuel_type)).to eq(
%w[gasoline]
)
end
end
it_behaves_like "queries both tables"
end
end
describe "maximum" do
context "aux columns are referenced" do
before do
@queries =
SpecHelper.capture_queries do
expect(Car.maximum(:engine_size)).to eq(2.0)
end
end
it_behaves_like "queries only the aux table"
end
context "aux columns are chained on a where clause" do
before do
@queries =
SpecHelper.capture_queries do
expect(
Car.where(engine_size: 1.4..1.9).maximum(:engine_size)
).to eq(1.8)
end
end
it_behaves_like "queries only the aux table"
end
context "main table columns are referenced" do
before do
@queries =
SpecHelper.capture_queries do
expect(
Car.where(name: "Toyota Camry").maximum(:engine_size)
).to eq(2.0)
end
end
it_behaves_like "queries both tables"
end
context "main table columns are chained on a where clause" do
before do
@queries =
SpecHelper.capture_queries do
expect(
Car.where(name: "Toyota Camry").maximum(:engine_size)
).to eq(2.0)
end
end
it_behaves_like "queries both tables"
end
end
end
end