Reduce number of database queries to get pages and conditions#2274
Merged
Conversation
bae0b43 to
2de90a5
Compare
97618fe to
a66573e
Compare
a66573e to
801d819
Compare
lfdebrux
reviewed
Oct 9, 2025
801d819 to
0f7f279
Compare
Rather than retrieving the conditions per page of a form when we need all conditions for a form, look them up in a single query by joining on the pages table.
ac09163 to
dedd9e8
Compare
Previously, we would run a SQL query per page in the form to get the next page, and also to get the conditions for the page. Use `includes` so that we get the pages and all their conditions by running fewer queries. Also accept the `next_page` as an argument to Page#as_form_document_step as we already know what the next_page is when we call it from Form#as_form_document so we can avoid an additional query per page. Add some additional tests to the request specs so we can be sure that the form document is updated as expected after updates to pages and conditions.
dedd9e8 to
98db76f
Compare
|
|
🎉 A review copy of this PR has been deployed! You can reach it at: https://pr-2274.admin.review.forms.service.gov.uk/ It may take 5 minutes or so for the application to be fully deployed and working. If it still isn't ready For the sign in details and more information, see the review apps wiki page. |
Contributor
Author
|
@lfdebrux I've added some more tests to make sure the form_documents are updated as expected after any updates to pages/conditions. |
lfdebrux
approved these changes
Oct 15, 2025
thomasiles
added a commit
that referenced
this pull request
Oct 31, 2025
[Reducing the number of DB queries has made page response times faster](#2274) [In Rails 7.2, the default logger started including the number of database queries in the log](rails/rails#51457). We already have DB time in the logs. I think it's useful to include the number of queries as another simple metric. This is useful for checking performance in local development and I think probably for production as well. It would enable us to see if the number of queries is increasing or decreasing over time for specific pages. Example log entry: { "method": "GET", "path": "/forms/1", "format": "html", "controller": "FormsController", "action": "show", "status": 200, "allocations": 67752, "duration": 238.2, "view": 23.53, "db": 156.87, "request_host": "localhost", "request_id": "01d5ce3c-7a6c-4d32-9288-cf4350850af3", "trace_id": null, "form_id": "1", "user_ip": null, "params": { "form_id": "1" }, "queries_count": 26, "cached_queries_count": 15 }
thomasiles
added a commit
that referenced
this pull request
Oct 31, 2025
[Reducing the number of DB queries has made page response times faster](#2274) [In Rails 7.2, the default logger started including the number of database queries in the log](rails/rails#51457). We already have DB time in the logs. I think it's useful to include the number of queries as another simple metric. This is useful for checking performance in local development and I think probably for production as well. It would enable us to see if the number of queries is increasing or decreasing over time for specific pages. Example log entry: { "method": "GET", "path": "/forms/1", "format": "html", "controller": "FormsController", "action": "show", "status": 200, "allocations": 67752, "duration": 238.2, "view": 23.53, "db": 156.87, "request_host": "localhost", "request_id": "01d5ce3c-7a6c-4d32-9288-cf4350850af3", "trace_id": null, "form_id": "1", "user_ip": null, "params": { "form_id": "1" }, "queries_count": 26, "cached_queries_count": 15 }
thomasiles
added a commit
that referenced
this pull request
Nov 3, 2025
[Reducing the number of DB queries has made page response times faster](#2274) [In Rails 7.2, the default logger started including the number of database queries in the log](rails/rails#51457). We already have DB time in the logs. I think it's useful to include the number of queries as another simple metric. This is useful for checking performance in local development and I think probably for production as well. It would enable us to see if the number of queries is increasing or decreasing over time for specific pages. Example log entry: { "method": "GET", "path": "/forms/1", "format": "html", "controller": "FormsController", "action": "show", "status": 200, "allocations": 67752, "duration": 238.2, "view": 23.53, "db": 156.87, "request_host": "localhost", "request_id": "01d5ce3c-7a6c-4d32-9288-cf4350850af3", "trace_id": null, "form_id": "1", "user_ip": null, "params": { "form_id": "1" }, "queries_count": 26, "cached_queries_count": 15 }
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



What problem does this pull request solve?
Reduce the number of SQL queries to get pages/conditions in a few places to improve performance.
The below images show the load times and number of queries run when adding a condition for a form with 118 pages locally.
Before:
After:
Things to consider when reviewing