-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only SELECT person's roles columns that we need
The idea with this change is to reduce the amount of data retrieved from the database in an attempt to speed up the overall response time to the GraphQL client. So far we've implemented this optimisation for the root edition, linked-to editions and reverse-linked editions. Here we're introducing the change for the person's role_appointments/roles fields specific to the ministers index type. We've followed the patterns established in the previous implementations with the big differences being mostly: * the larger number of JOINs needed in this query because we're traversing 2 levels of links * the convenience of only needing to handle link set links here because we know that those are the only types of links involved in linking the ministers index's person-role_appointments-roles Because of the large number of JOINS, we've resorted to supplying SQL strings in order to provide our own, hopefully clearer, join table aliases as opposed to leaving that to Active Record to generate.
- Loading branch information
Showing
3 changed files
with
76 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,78 @@ | ||
module Sources | ||
class PersonCurrentRolesSource < GraphQL::Dataloader::Source | ||
def fetch(person_content_ids) | ||
# rubocop:disable Lint/MissingSuper | ||
def initialize | ||
@content_store = :live | ||
end | ||
# rubocop:enable Lint/MissingSuper | ||
|
||
def fetch(person_content_ids_and_selections) | ||
person_content_ids = [] | ||
ids_map = {} | ||
all_selections = { | ||
role_appointment_links: %i[target_content_id], | ||
documents: %i[content_id], | ||
} | ||
editions_selections = Set.new | ||
|
||
person_content_ids_and_selections.each do |person_content_id, selections| | ||
person_content_ids << person_content_id | ||
ids_map[person_content_id] = [] | ||
editions_selections.merge(selections) | ||
end | ||
|
||
all_selections[:editions] = editions_selections.to_a | ||
|
||
all_roles = Edition | ||
.live | ||
.includes( | ||
document: { | ||
reverse_links: { # role -> role_appointment | ||
link_set: { | ||
documents: [ | ||
:editions, # role_appointment | ||
:link_set_links, # role_appointment -> person | ||
], | ||
}, | ||
}, | ||
}, | ||
.joins(document: [reverse_links: :link_set]) | ||
.joins( | ||
<<~SQL, | ||
INNER JOIN documents role_appointment_documents | ||
ON role_appointment_documents.content_id = link_sets.content_id | ||
SQL | ||
) | ||
.joins( | ||
<<~SQL, | ||
INNER JOIN editions role_appointment_editions | ||
ON role_appointment_editions.document_id = role_appointment_documents.id | ||
SQL | ||
) | ||
.joins( | ||
<<~SQL, | ||
INNER JOIN link_sets role_appointment_link_sets | ||
ON role_appointment_link_sets.content_id = role_appointment_documents.content_id | ||
SQL | ||
) | ||
.joins( | ||
<<~SQL, | ||
INNER JOIN links role_appointment_links | ||
ON role_appointment_links.link_set_id = role_appointment_link_sets.id | ||
SQL | ||
) | ||
.where( | ||
document_type: "ministerial_role", | ||
document: { locale: "en" }, | ||
editions: { | ||
content_store: @content_store, | ||
document_type: "ministerial_role", | ||
}, | ||
documents: { locale: "en" }, | ||
reverse_links: { link_type: "role" }, | ||
documents: { locale: "en" }, # role_appointment Documents | ||
editions_documents: { document_type: "role_appointment" }, # role_appointment Editions | ||
link_set_links: { target_content_id: person_content_ids, link_type: "person" }, | ||
role_appointment_documents: { locale: "en" }, | ||
role_appointment_editions: { | ||
content_store: @content_store, | ||
document_type: "role_appointment", | ||
}, | ||
role_appointment_links: { | ||
target_content_id: person_content_ids, | ||
link_type: "person", | ||
}, | ||
) | ||
.where("editions_documents.details ->> 'current' = 'true'") # editions_documents is the alias that Active Record gives to the role_appointment Editions in the SQL query | ||
.where("role_appointment_editions.details ->> 'current' = 'true'") | ||
.order(reverse_links: { position: :asc }) | ||
|
||
ids_map = person_content_ids.index_with { [] } | ||
.select(all_selections) | ||
|
||
all_roles.each_with_object(ids_map) { |role, hash| | ||
hash[person_content_id_for_role(role)] << role | ||
hash[role.target_content_id] << role | ||
}.values | ||
end | ||
|
||
private | ||
|
||
def person_content_id_for_role(role) | ||
role_appointment_documents_for_role(role) | ||
.flat_map(&:link_set_links) | ||
.find { |link| link.link_type == "person" } # role_appointment -> person | ||
.target_content_id | ||
end | ||
|
||
def role_appointment_documents_for_role(role) | ||
role.document.reverse_links | ||
.select { |link| link.link_type == "role" } # role -> role_appointment | ||
.flat_map { |link| link.link_set.documents } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters