perf: eliminate N+1 queries in __make_cre_links and __get_all_nodes_and_cres#850
Open
shiwani42 wants to merge 1 commit intoOWASP:mainfrom
Open
perf: eliminate N+1 queries in __make_cre_links and __get_all_nodes_and_cres#850shiwani42 wants to merge 1 commit intoOWASP:mainfrom
shiwani42 wants to merge 1 commit intoOWASP:mainfrom
Conversation
…nd_cres __make_cre_links was issuing a separate SELECT for each linked node — one query to fetch all Links rows for a CRE, then one more per row to fetch the corresponding Node. Replace with a single JOIN query so the whole thing resolves in one round-trip regardless of how many links a CRE has. __get_all_nodes_and_cres was fetching IDs first (SELECT id FROM …) and then re-querying each record individually. For CREs this chained into get_cre_by_db_id, which added a further SELECT external_id per CRE before finally calling get_CREs. Fetch full ORM objects directly and call get_CREs(external_id=) using the already-known external_id, cutting out that extra per-CRE round-trip. The same IDs-first pattern is fixed for the node branch.
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.
Fixes #848
What changed
__make_cre_linksReplaced the per-link
Nodefetch with a single JOIN query. Previously, for a CRE with L links this method issued 1 + L queries. It now issues one query regardless of link count.Before:
After:
__get_all_nodes_and_cresReplaced the IDs-first fetch pattern with direct ORM object queries. For CREs, the previous code called
get_cre_by_db_idper record, which added aSELECT external_id FROM cre WHERE id = ?round-trip before reachingget_CREs. Fetching full ORM objects and passing the already-knownexternal_iddirectly cuts that out. The same fix is applied to the node branch.Tests
All existing tests pass. The changed methods are exercised by
test_get_CREs,test_export,test_get_root_cres, andtest_get_cre_hierarchy.