-
Notifications
You must be signed in to change notification settings - Fork 16
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
Batch link expansion queries #3106
Conversation
3de40b0
to
179143e
Compare
179143e
to
fc6d38f
Compare
I tried combining the link set and edition links into a single query .... but ended up making 19 SQL queries instead of 15! I don't think the performance improvement will be significant by combining the two, so have left them as separate queries. |
fc6d38f
to
de1628b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haven't had time to finish looking at this, but no issues so far!
We are filtering the links by the content store of the target document's edition, but no test was included for this. Adding a test prior to making changes that could affect this filtering.
Previously LinkedToEditionsSource would make one query per edition (getting linked editions, given a list of link types). This change makes one query per parent edition, by getting the dataloader to batch queries across multiple editions and link types. For the prime minister page, the number of database queries reduces from 93 to 15, and the ActiveRecord execution time decreases from ~100ms to ~20ms, when run locally. Co-authored-by: Richard Towers <[email protected]>
de1628b
to
cff1b0c
Compare
@@ -10,7 +10,7 @@ | |||
create(:link, link_set: link_set, target_content_id: target_edition_3.content_id, link_type: "test_link") | |||
|
|||
GraphQL::Dataloader.with_dataloading do |dataloader| | |||
request = dataloader.with(described_class, parent_object: source_edition).request("test_link") | |||
request = dataloader.with(described_class, content_store: source_edition.content_store).request([source_edition, "test_link"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: are .request
in this spec and .load
in BaseObject
both ending up at the fetch
method in LinkedToEditionsSource
? 🤯
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It appears to be, but I'm not sure why. The same is done is the other dataloader tests too.
Previously
LinkedToEditionsSource
would make one query per edition (getting linked editions, given a list of link types).We can do better than this by making it take a pair of edition and link type, and then execute a query like:
This allows the dataloader to batch up all the editions and requested link types at each level of depth before sending one big query, which dramatically reduces the number of SQL queries needed.
For the /government/prime-minister page on @brucebolt's machine, this reduces ActiveRecord time from ~100ms to ~20ms.
ActiveRecord doesn't seem to support the kind of tuple-inclusion query I'm making here. It does if all the columns are on the same table, but we need columns in different tables, so I've had to drop down to using Arel.
Trello card