Repository: saleor/saleor

Issue:
DB performance improvements in attribute dataloaders
As noticed in [this PR](https://github.com/saleor/saleor/pull/15404/) there are some performance improvements that could be made in attribute dataloaders, to use subqueries instead of joins.

- https://github.com/saleor/saleor/pull/15404/files/0b97162a4503fac6153ef2cd2d37b6a80aeb32a4#r1489110075
- https://github.com/saleor/saleor/pull/15404/files/0b97162a4503fac6153ef2cd2d37b6a80aeb32a4#r1489110459
- https://github.com/saleor/saleor/pull/15404/files/0b97162a4503fac6153ef2cd2d37b6a80aeb32a4#r1489110819


Discussion:
@maarcingebala Can you please assign this issue for me?
@AjmalPonneth Assigned 🚀 

Resolving patch:
diff --git a/saleor/graphql/page/dataloaders.py b/saleor/graphql/page/dataloaders.py
--- 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
--- 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
+                ),
+            ),
         )
 
 


Test patch: