From e0baf65fd2dbd50243aec16e4fcc82e14957aa07 Mon Sep 17 00:00:00 2001 From: David Ormsbee Date: Thu, 26 Sep 2024 22:26:44 -0400 Subject: [PATCH] perf: reduce db calls for ComponentVersion after lookup by UUID When you fetch a single ComponentVersion, you are often going to want to pull data related to its Component, and possibly the LearningPackage it belongs to. This commit adds a select_related call to eliminate the extra roundtrips for this information. --- .../apps/authoring/components/api.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/openedx_learning/apps/authoring/components/api.py b/openedx_learning/apps/authoring/components/api.py index a65eee80..1355a2ff 100644 --- a/openedx_learning/apps/authoring/components/api.py +++ b/openedx_learning/apps/authoring/components/api.py @@ -269,7 +269,15 @@ def get_component_by_uuid(uuid: UUID) -> Component: def get_component_version_by_uuid(uuid: UUID) -> ComponentVersion: - return ComponentVersion.objects.get(publishable_entity_version__uuid=uuid) + return ( + ComponentVersion + .objects + .select_related( + "component", + "component__learning_package", + ) + .get(publishable_entity_version__uuid=uuid) + ) def component_exists_by_key( @@ -510,7 +518,12 @@ def _error_header(error: AssetError) -> dict[str, str]: # Check: Does the ComponentVersion exist? try: - component_version = get_component_version_by_uuid(component_version_uuid) + component_version = ( + ComponentVersion + .objects + .select_related("component", "component__learning_package") + .get(publishable_entity_version__uuid=component_version_uuid) + ) except ComponentVersion.DoesNotExist: # No need to add headers here, because no ComponentVersion was found. logger.error(f"Asset Not Found: No ComponentVersion with UUID {component_version_uuid}")