Skip to content

Conversation

@davidmillen50
Copy link
Contributor

@davidmillen50 davidmillen50 commented Aug 23, 2025

The previous implementation was loading workshops upfront which was causing slow queries which timed out the app. This new query should load only the related workshop sponsors to a workshop in one sql query

Benchmark times when running test file ./spec/features/chapter_spec.rb

Execution order when test file run Old Code runtime (ms) New SQL code runtime (ms) Difference (ms)
1 15.21 2.789 12.421 faster
2 11.018 1.51 9.508 faster
3 12.304 2.964 9.34 faster
4 13.099 1.513 11.586 faster
5 12.886 1.609 11.277 faster
6 3.037 0.772 2.265 faster
7 11.072 2.705 8.367 faster
8 11.02 2.156 8.864 faster
9 11.155 2.122 9.033 faster
10 11.124 2.567 8.557 faster
11 10.757 1.92 8.837 faster
12 11.543 1.979 9.564 faster
13 11.976 2.921 9.055 faster
14 10.869 2.034 8.835 faster
15 10.614 1.998 8.616 faster
16 3.003 0.727 2.276 faster
17 3.016 0.842 2.174 faster
18 10.7 2.486 8.214 faster
19 10.771 1.987 8.784 faster
20 10.943 2.271 8.672 faster
21 2.597 0.705 1.892 faster
22 4.704 0.891 3.813 faster

The previous implementation was loading workshops upfront which was causing slow queries which timed out the app.  This new query should load only the related workshop sponsors to a workshop in one sql query
@jonodrew
Copy link
Contributor

Running this locally, the old code generates this SQL:

WorkshopSponsor Load (1.0ms)  SELECT "workshop_sponsors".* FROM "workshop_sponsors" WHERE (workshop_sponsors.host = TRUE) AND (workshop_sponsors.workshop_id = 1) ORDER BY "workshop_sponsors"."id" ASC LIMIT $1  [["LIMIT", 1]]
  Sponsor Load (0.6ms)  SELECT "sponsors".* FROM "sponsors" WHERE "sponsors"."id" = $1 ORDER BY updated_at desc LIMIT $2  [["id", 1], ["LIMIT", 1]]

while the new generates this:

  WorkshopSponsor Load (10.0ms)  SELECT "workshop_sponsors".* FROM "workshop_sponsors" WHERE "workshop_sponsors"."workshop_id" = $1 AND "workshop_sponsors"."host" = $2 ORDER BY "workshop_sponsors"."id" ASC LIMIT $3  [["workshop_id", 1], ["host", true], ["LIMIT", 1]]
  Sponsor Load (1.6ms)  SELECT "sponsors".* FROM "sponsors" WHERE "sponsors"."id" = $1 ORDER BY updated_at desc LIMIT $2  [["id", 1], ["LIMIT", 1]]

is that as expected?

@davidmillen50
Copy link
Contributor Author

@jonodrew I'm trying a few things out still. Just about to push a change to use raw sql

Use a sql statement with join rather than active record
@davidmillen50 davidmillen50 marked this pull request as ready for review August 25, 2025 11:30
LIMIT 1
SQL

Sponsor.find_by_sql([sql, id]).first
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking a merge: this SQL query is expressible with ActiveRecord (see also Arel), but this change allows 1 query rather than more.

@olleolleolle olleolleolle merged commit 1d0c298 into codebar:master Aug 27, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants