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

Reduce dataloader queries #3192

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Reduce dataloader queries #3192

wants to merge 5 commits into from

Conversation

yndajas
Copy link
Member

@yndajas yndajas commented Mar 10, 2025

Trello

This optimises(?) the database queries in LinkedToEditionsSource and ReverseLinkedToEditionsSource, reducing them down to one query each (in practice, one query per level of depth in the GraphQL query). This is a change en route to selecting a subset of columns - we'll open a separate pull request for that change

We're looking into the actual performance of this change at the minute but thought it was worth opening the pull request since the core bones are in place


⚠️ This repo is Continuously Deployed: make sure you follow the guidance ⚠️

This application is owned by the publishing platform team. Please let us know in #govuk-publishing-platform when you raise any PRs.

Follow these steps if you are doing a Rails upgrade.

@yndajas yndajas force-pushed the reduce-dataloader-queries branch 2 times, most recently from 2d84d96 to 7e7c61f Compare March 10, 2025 12:51
Copy link
Member

@brucebolt brucebolt left a comment

Choose a reason for hiding this comment

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

The code changes all seem ok to me.

But I've run the code locally and queried the prime minister page, which gives the role_appointments links in a different order to those in the existing content item. It could be the order has changed since my old dump of the database, but I'd suggest trying this code on integration to double check the order has stays the same when querying against the same database.

@yndajas yndajas force-pushed the reduce-dataloader-queries branch from 7e7c61f to 50c28a4 Compare March 12, 2025 09:41
@mike3985 mike3985 force-pushed the reduce-dataloader-queries branch from 50c28a4 to 7894c42 Compare March 12, 2025 14:15
yndajas and others added 5 commits March 12, 2025 17:01
Co-authored-by: Mike Patrick <mike.patrick2@digital.cabinet-office.gov.uk>
I'm not 100% sure about this change.

We're experimenting with SELECTing subsets of an edition's columns and
JOINed-tables' columns to improve the performance of our GraphQL
resolvers. When you SELECT JOIN columns, Active Record helpfully just
seems to dynamically add them as attributes to the model you're working
with, which means that, e.g.

  SELECT editions.*, documents.content_id, documents.locale FROM editions
  INNER JOIN documents ON document.id = editions.document_id

would result in instances of Edition with their own, native #content_id
and #locale attributes. In those specific examples, the existing
`delegate` doesn't appear to acknowledge these attributes and still
attempts to lazy-load the edition's document to fetch them. Since that
lazy-loading kinda defeats the point of the approach we're experimenting
with, here's an alternative to `delegate` that first checks for
pre-existing attributes of the same name.

There could be better approaches than this worth exploring in the long
run if we do stick with this SELECTing optimisation. (I don't think it's
wise to use these Active Record models in such dramatically different
ways in different parts of the codebase.)
We explicitly order the results by position, but we'll test this
separately. We changed the Active Record code to use a UNION query,
which has meant interweaving edition and link set links, breaking the
order that these tests were implicitly depending on

Co-authored-by: Mike Patrick <mike.patrick2@digital.cabinet-office.gov.uk>
We encountered an issue when creating an edition link separately to its
edition (i.e. not using the `links_hash` transient/argument), whereby
both a link set association was created even when explicitly creating an
edition association

Co-authored-by: Mike Patrick <mike.patrick2@digital.cabinet-office.gov.uk>
We found that the previous eager loading approach wasn't using joins
effectively, so we're more manually crafting the queries to join in an
efficient way. We also found that some associations we're still lazy
loaded after the initial queries - this new approach avoids that issue

For now we're selecting all columns from the editions table. We'll
reduce the selections down to a subset in a later change

The change to using a UNION affects the order of the resulting editions,
so we've added a test to document our expectations

Co-authored-by: Mike Patrick <mike.patrick2@digital.cabinet-office.gov.uk>
@yndajas yndajas force-pushed the reduce-dataloader-queries branch from 7894c42 to 70f046a Compare March 12, 2025 17:16
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