Skip to content

Saleor Core: the high performance, composable, headless commerce API.

DB performance improvements in attribute dataloaders (#15474)

AjmalPonnethe9aed3e42230
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2021a86af8..473d14ff76 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,7 @@ All notable, unreleased changes to this project will be documented in this file.
### Other changes
- Don't raise InsufficientStock for track_inventory=False variants #15475 by @carlosa54
+- DB performance improvements in attribute dataloaders - #15474 by @AjmalPonneth
# 3.19.0
diff --git a/saleor/graphql/page/dataloaders.py b/saleor/graphql/page/dataloaders.py
index e542e68ac6..1a4abe3dc9 100644
--- a/saleor/graphql/page/dataloaders.py
+++ b/saleor/graphql/page/dataloaders.py
@@ -1,8 +1,9 @@
from collections import defaultdict
+from django.db.models import Exists, OuterRef
from promise import Promise
-from ...attribute.models import AssignedPageAttributeValue, AttributePage
+from ...attribute.models import AssignedPageAttributeValue, Attribute, AttributePage
from ...page.models import Page, PageType
from ..attribute.dataloaders import AttributesByAttributeId, AttributeValueByIdLoader
from ..core.dataloaders import DataLoader
@@ -94,7 +95,11 @@ class PageAttributesVisibleInStorefrontByPageTypeIdLoader(
def get_queryset(self):
return AttributePage.objects.using(self.database_connection_name).filter(
- attribute__visible_in_storefront=True
+ Exists(
+ Attribute.objects.filter(
+ pk=OuterRef("attribute_id"), visible_in_storefront=True
+ )
+ ),
)
diff --git a/saleor/graphql/product/dataloaders/attributes.py b/saleor/graphql/product/dataloaders/attributes.py
index c502788f5d..5ff6d2eff9 100644
--- a/saleor/graphql/product/dataloaders/attributes.py
+++ b/saleor/graphql/product/dataloaders/attributes.py
@@ -1,11 +1,13 @@
from collections import defaultdict
+from django.db.models import Exists, OuterRef
from promise import Promise
from ....attribute.models import (
AssignedProductAttributeValue,
AssignedVariantAttribute,
AssignedVariantAttributeValue,
+ Attribute,
AttributeProduct,
AttributeVariant,
)
@@ -79,7 +81,11 @@ class ProductAttributesVisibleInStorefrontByProductTypeIdLoader(
def get_queryset(self):
return self.model_name.objects.using(self.database_connection_name).filter(
- attribute__visible_in_storefront=True
+ Exists(
+ Attribute.objects.filter(
+ pk=OuterRef("attribute_id"), visible_in_storefront=True
+ ),
+ ),
)
@@ -107,7 +113,11 @@ class VariantAttributesVisibleInStorefrontByProductTypeIdLoader(
def get_queryset(self):
return self.model_name.objects.using(self.database_connection_name).filter(
- attribute__visible_in_storefront=True
+ Exists(
+ Attribute.objects.filter(
+ pk=OuterRef("attribute_id"), visible_in_storefront=True
+ ),
+ ),
)