Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Optimize SQL queries for fetching product variants (#16262)
Marcin Gębalaf3a111394d43
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6f346e036e..880b78e233 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -31,6 +31,7 @@ All notable, unreleased changes to this project will be documented in this file.- Extend valid address values - #15877 by @zedzior- Fixed a rare crash in the introspection query detection code - #15966 by @patrys- Added HTTP compression telemetry - #16125 by @patrys+- Rewrite `productVariants` resolvers to use JOINs instead of subqueries - #16262 by @maarcingebala# 3.19.0diff --git a/saleor/graphql/product/resolvers.py b/saleor/graphql/product/resolvers.pyindex 8ca0b7aa70..c46bf9d6d1 100644--- a/saleor/graphql/product/resolvers.py+++ b/saleor/graphql/product/resolvers.py@@ -165,35 +165,24 @@ def resolve_variant(@traced_resolverdef resolve_product_variants(info: ResolveInfo,- requestor_has_access_to_all,requestor,ids=None,channel: Optional[Channel] = None,limited_channel_access: bool = False,) -> ChannelQsContext:connection_name = get_database_connection_name(info.context)- visible_products = models.Product.objects.using(connection_name).visible_to_user(++ qs = models.ProductVariant.objects.using(connection_name).visible_to_user(requestor, channel, limited_channel_access)- qs = models.ProductVariant.objects.using(connection_name).filter(- product__id__in=visible_products- )- channel_slug = channel.slug if channel else None- if not requestor_has_access_to_all:- visible_products = visible_products.annotate_visible_in_listings(- channel- ).exclude(visible_in_listings=False)- qs = (- qs.using(connection_name)- .filter(product__in=visible_products)- .available_in_channel(channel)- )if ids:db_ids = [from_global_id_or_error(node_id, "ProductVariant")[1] for node_id in ids]qs = qs.filter(pk__in=db_ids)++ channel_slug = channel.slug if channel else Nonereturn ChannelQsContext(qs=qs, channel_slug=channel_slug)diff --git a/saleor/graphql/product/schema.py b/saleor/graphql/product/schema.pyindex 1782ea36d5..faac3fde74 100644--- a/saleor/graphql/product/schema.py+++ b/saleor/graphql/product/schema.py@@ -586,7 +586,6 @@ class ProductQueries(graphene.ObjectType):ids=ids,channel=channel_obj,limited_channel_access=limited_channel_access,- requestor_has_access_to_all=has_required_permissions,requestor=requestor,)kwargs["channel"] = qs.channel_slugdiff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.pyindex c1881f1815..1635ec6318 100644--- a/saleor/graphql/product/types/products.py+++ b/saleor/graphql/product/types/products.py@@ -808,9 +808,6 @@ class ProductVariant(ChannelContextTypeWithMetadata[models.ProductVariant]):@staticmethoddef __resolve_references(roots: list["ProductVariant"], info):requestor = get_user_or_app_from_context(info.context)- requestor_has_access_to_all = has_one_of_permissions(- requestor, ALL_PRODUCTS_PERMISSIONS- )channels = defaultdict(set)roots_ids = []@@ -827,7 +824,6 @@ class ProductVariant(ChannelContextTypeWithMetadata[models.ProductVariant]):limited_channel_access = False if channel_slug is None else Trueqs = resolve_product_variants(info,- requestor_has_access_to_all,requestor,ids=ids,channel=channels_map.get(channel_slug),diff --git a/saleor/product/managers.py b/saleor/product/managers.pyindex 7d20e1d143..dc72212923 100644--- a/saleor/product/managers.py+++ b/saleor/product/managers.py@@ -316,6 +316,48 @@ class ProductVariantQueryset(models.QuerySet):"variant_media__media",)+ def visible_to_user(+ self,+ requestor: Union["User", "App", None],+ channel: Optional[Channel],+ limited_channel_access: bool,+ ):+ from .models import ALL_PRODUCTS_PERMISSIONS++ # User with product permissions can see all variants. If channel is given,+ # filter variants with product channel listings for this channel.+ if has_one_of_permissions(requestor, ALL_PRODUCTS_PERMISSIONS):+ if limited_channel_access:+ if channel:+ return self.filter(product__channel_listings__channel_id=channel.id)+ return self.none()+ return self.all()++ # If user has no permissions (customer) and channel is not given or is inactive,+ # return no variants.+ if not channel or not channel.is_active:+ return self.none()++ # If user has no permissions (customer) and channel is given, return variants+ # that:+ # - have a variant channel listing for this channel and the price is not null+ # - have a product channel listing for this channel and the product is published+ # and visible in listings+ variants = self.filter(+ channel_listings__channel_id=channel.id,+ channel_listings__price_amount__isnull=False,+ )++ today = datetime.datetime.now(pytz.UTC)+ variants = variants.filter(+ Q(product__channel_listings__published_at__lte=today)+ | Q(product__channel_listings__published_at__isnull=True),+ product__channel_listings__is_published=True,+ product__channel_listings__channel_id=channel.id,+ product__channel_listings__visible_in_listings=True,+ )+ return variants+ProductVariantManager = models.Manager.from_queryset(ProductVariantQueryset)