Skip to content

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

Improve performance when requesting totalCount with in-stock filter (#16904)

Maciej Korycinskieb61406cecc5
diff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.py
index a11409aa04..919a5a95a8 100644
--- a/saleor/graphql/product/filters.py
+++ b/saleor/graphql/product/filters.py
@@ -407,22 +407,26 @@ def filter_products_by_stock_availability(qs, stock_availability, channel_slug):
.values_list(Sum("quantity_reserved"))
)
reservation_subquery = Subquery(queryset=reservations, output_field=IntegerField())
-
+ warehouse_pks = list(
+ Warehouse.objects.using(qs.db)
+ .for_channel_with_active_shipping_zone_or_cc(channel_slug)
+ .values_list("pk", flat=True)
+ )
stocks = (
Stock.objects.using(qs.db)
- .for_channel_and_country(channel_slug)
.filter(
+ warehouse_id__in=warehouse_pks,
quantity__gt=Coalesce(allocated_subquery, 0)
- + Coalesce(reservation_subquery, 0)
+ + Coalesce(reservation_subquery, 0),
)
.values("product_variant_id")
)
+
variants = (
ProductVariant.objects.using(qs.db)
.filter(Exists(stocks.filter(product_variant_id=OuterRef("pk"))))
.values("product_id")
)
-
if stock_availability == StockAvailability.IN_STOCK:
qs = qs.filter(Exists(variants.filter(product_id=OuterRef("pk"))))
if stock_availability == StockAvailability.OUT_OF_STOCK:
diff --git a/saleor/warehouse/models.py b/saleor/warehouse/models.py
index bcb33dc2b4..b26ce65a3e 100644
--- a/saleor/warehouse/models.py
+++ b/saleor/warehouse/models.py
@@ -2,25 +2,11 @@ import itertools
import uuid
from collections import defaultdict
from collections.abc import Iterable
-from typing import (
- TYPE_CHECKING,
- Optional,
- TypedDict,
- TypeVar,
- Union,
- cast,
-)
+from typing import TYPE_CHECKING, Optional, TypedDict, TypeVar, Union, cast
from django.contrib.postgres.indexes import BTreeIndex
from django.db import models
-from django.db.models import (
- Exists,
- F,
- OuterRef,
- Prefetch,
- Q,
- Sum,
-)
+from django.db.models import Exists, F, OuterRef, Prefetch, Q, Sum
from django.db.models.expressions import Subquery
from django.db.models.functions import Coalesce
from django.db.models.query import QuerySet
@@ -65,6 +51,39 @@ class WarehouseQueryset(models.QuerySet["Warehouse"]):
)
).order_by("pk")
+ def for_channel_with_active_shipping_zone_or_cc(self, channel_slug: str):
+ WarehouseChannel = Channel.warehouses.through
+ ShippingZoneChannel = Channel.shipping_zones.through
+ WarehouseShippingZone = ShippingZone.warehouses.through
+
+ channel = Channel.objects.filter(slug=channel_slug).values("pk")
+ warehouse_channels = WarehouseChannel.objects.filter(
+ Exists(channel.filter(pk=OuterRef("channel_id")))
+ ).values("warehouse_id")
+
+ shipping_zone_channels = ShippingZoneChannel.objects.filter(
+ Exists(channel.filter(pk=OuterRef("channel_id")))
+ ).values("shippingzone_id")
+
+ warehouse_shipping_zones = WarehouseShippingZone.objects.filter(
+ Exists(warehouse_channels.filter(warehouse_id=OuterRef("warehouse_id"))),
+ Exists(
+ shipping_zone_channels.filter(
+ shippingzone_id=OuterRef("shippingzone_id")
+ )
+ ),
+ ).values("warehouse_id")
+ return self.filter(
+ Q(
+ Exists(warehouse_channels.filter(warehouse_id=OuterRef("id"))),
+ click_and_collect_option__in=[
+ WarehouseClickAndCollectOption.LOCAL_STOCK,
+ WarehouseClickAndCollectOption.ALL_WAREHOUSES,
+ ],
+ )
+ | Q(Exists(warehouse_shipping_zones.filter(warehouse_id=OuterRef("id"))))
+ )
+
def for_country_and_channel(self, country: str, channel_id: int):
ShippingZoneChannel = Channel.shipping_zones.through
WarehouseShippingZone = ShippingZone.warehouses.through
diff --git a/saleor/warehouse/tests/test_warehouse.py b/saleor/warehouse/tests/test_warehouse.py
index 081c4ceb53..e73534b1ca 100644
--- a/saleor/warehouse/tests/test_warehouse.py
+++ b/saleor/warehouse/tests/test_warehouse.py
@@ -807,3 +807,19 @@ def test_applicable_for_click_and_collect_no_quantity_check_additional_stock(
# then
assert len(result) == 1
assert result.first().id == all_warehouse.id
+
+
+def test_for_channel_with_active_shipping_zone_or_cc(
+ warehouses_for_cc, warehouse, warehouse_JPY, warehouse_no_shipping_zone, channel_USD
+):
+ # given
+ expected_warehouse_pks = [wh.pk for wh in warehouses_for_cc]
+ expected_warehouse_pks.append(warehouse.pk)
+
+ # when
+ warehouses = Warehouse.objects.for_channel_with_active_shipping_zone_or_cc(
+ channel_USD.slug
+ )
+
+ # then
+ assert set(warehouses.values_list("pk", flat=True)) == set(expected_warehouse_pks)