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

Fix N+1 queries fetching all projects #16109

Merged

Conversation

eduardoj
Copy link
Member

@eduardoj eduardoj commented May 8, 2024

Craft with a scope the join condition of projects and values. This prevent N+1 queries.

Also, remove the loop to render more than one value for the OBS:QualityCategory attribute. This attribute can only keep one value.

Before

The last query (SELECT attrib_values.* FROM attrib_values INNER JOIN...) is repeated 25 times.

Project Load (0.7ms)  SELECT `projects`.* FROM `projects` WHERE `projects`.`id` != 0 AND NOT (name rlike '^home:.+') ORDER BY projects.name ASC LIMIT 25 OFFSET 0
↳ app/datatables/project_datatable.rb:33:in `map'
AttribValue Load (0.5ms)  SELECT `attrib_values`.* FROM `attrib_values` INNER JOIN `attribs` ON `attribs`.`id` = `attrib_values`.`attrib_id` INNER JOIN `attrib_types` ON `attrib_types`.`id` = `attribs`.`attrib_type_id` INNER JOIN `attrib_namespaces` ON `attrib_namespaces`.`id` = `attrib_types`.`attrib_namespace_id` WHERE `attribs`.`project_id` = 10 AND `attrib_types`.`name` = 'QualityCategory' AND `attrib_namespaces`.`name` = 'OBS'
↳ app/queries/obs_quality_categories_finder.rb:8:in `map'
AttribValue Load (0.9ms)  SELECT `attrib_values`.* FROM `attrib_values` INNER JOIN `attribs` ON `attribs`.`id` = `attrib_values`.`attrib_id` INNER JOIN `attrib_types` ON `attrib_types`.`id` = `attribs`.`attrib_type_id` INNER JOIN `attrib_namespaces` ON `attrib_namespaces`.`id` = `attrib_types`.`attrib_namespace_id` WHERE `attribs`.`project_id` = 1 AND `attrib_types`.`name` = 'QualityCategory' AND `attrib_namespaces`.`name` = 'OBS'
↳ app/queries/obs_quality_categories_finder.rb:8:in `map'
[...]
AttribValue Load (0.8ms)  SELECT `attrib_values`.* FROM `attrib_values` INNER JOIN `attribs` ON `attribs`.`id` = `attrib_values`.`attrib_id` INNER JOIN `attrib_types` ON `attrib_types`.`id` = `attribs`.`attrib_type_id` INNER JOIN `attrib_namespaces` ON `attrib_namespaces`.`id` = `attrib_types`.`attrib_namespace_id` WHERE `attribs`.`project_id` = 1 AND `attrib_types`.`name` = 'QualityCategory' AND `attrib_namespaces`.`name` = 'OBS'
↳ app/queries/obs_quality_categories_finder.rb:8:in `map'

After

Project Load (2.2ms)  SELECT projects.*, attrib_values.value AS attrib_value FROM `projects` LEFT OUTER JOIN attribs ON (attribs.project_id = projects.id AND attribs.attrib_type_id = 15) LEFT OUTER JOIN attrib_values ON attribs.id = attrib_values.attrib_id WHERE `projects`.`id` != 0 AND NOT (name rlike '^home:.+') ORDER BY projects.name ASC LIMIT 25 OFFSET 0
↳ app/datatables/project_datatable.rb:36:in `map'

For reviewers

@eduardoj eduardoj added the review-app Apply this label if you want a review app started label May 8, 2024
@github-actions github-actions bot added the Frontend Things related to the OBS RoR app label May 8, 2024
@obs-bot
Copy link
Collaborator

obs-bot commented May 8, 2024

@eduardoj eduardoj force-pushed the fix/all_projects_n_plus_1_queries branch 2 times, most recently from 4ace85a to e427178 Compare May 14, 2024 09:22
Craft with a scope the join condition of projects and values. This
prevent N+1 queries.

Also, remove the loop to render more than one value for the
OBS:QualityCategory attribute. This attribute can only keep one value.
@eduardoj eduardoj force-pushed the fix/all_projects_n_plus_1_queries branch from e427178 to 4d7b39e Compare May 14, 2024 15:28
@eduardoj eduardoj merged commit 875f14b into openSUSE:master May 15, 2024
20 of 21 checks passed
@eduardoj eduardoj deleted the fix/all_projects_n_plus_1_queries branch May 15, 2024 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Frontend Things related to the OBS RoR app review-app Apply this label if you want a review app started
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants