Skip to content

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

Optimize products stocks filter in 3 1 (#8756)

Filip Owczarek4fc702179851
diff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.py
index b0dce57d5b..2a4b2882de 100644
--- a/saleor/graphql/product/filters.py
+++ b/saleor/graphql/product/filters.py
@@ -6,6 +6,7 @@ import django_filters
import graphene
from django.contrib.postgres.search import SearchQuery, SearchRank
from django.db.models import Exists, F, FloatField, OuterRef, Q, Subquery, Sum
+from django.db.models.expressions import ExpressionWrapper
from django.db.models.fields import IntegerField
from django.db.models.functions import Cast, Coalesce
from django.utils import timezone
@@ -272,19 +273,19 @@ def filter_products_by_variant_price(qs, channel_slug, price_lte=None, price_gte
def filter_products_by_minimal_price(
qs, channel_slug, minimal_price_lte=None, minimal_price_gte=None
):
- channels = Channel.objects.filter(slug=channel_slug).values("pk")
+ channel = Channel.objects.filter(slug=channel_slug).first()
+ if not channel:
+ return qs
product_channel_listings = ProductChannelListing.objects.filter(
- Exists(channels.filter(pk=OuterRef("channel_id")))
+ channel_id=channel.id
)
if minimal_price_lte:
product_channel_listings = product_channel_listings.filter(
- discounted_price_amount__lte=minimal_price_lte,
- discounted_price_amount__isnull=False,
+ discounted_price_amount__lte=minimal_price_lte
)
if minimal_price_gte:
product_channel_listings = product_channel_listings.filter(
- discounted_price_amount__gte=minimal_price_gte,
- discounted_price_amount__isnull=False,
+ discounted_price_amount__gte=minimal_price_gte
)
product_channel_listings = product_channel_listings.values("product_id")
return qs.filter(Exists(product_channel_listings.filter(product_id=OuterRef("pk"))))
@@ -569,25 +570,26 @@ def filter_quantity(qs, quantity_value, warehouse_ids=None):
between given range. If warehouses is given, it aggregates quantity only
from stocks which are in given warehouses.
"""
- variants = ProductVariant.objects.filter(product__in=qs)
-
+ stocks = Stock.objects.all()
if warehouse_ids:
_, warehouse_pks = resolve_global_ids_to_primary_keys(
warehouse_ids, warehouse_types.Warehouse
)
- variants = variants.annotate(
- total_quantity=Sum(
- "stocks__quantity", filter=Q(stocks__warehouse__pk__in=warehouse_pks)
- )
- )
- else:
- variants = ProductVariant.objects.filter(product__in=qs).annotate(
- total_quantity=Sum("stocks__quantity")
- )
-
- variants = filter_range_field(variants, "total_quantity", quantity_value)
+ stocks = stocks.filter(warehouse_id__in=warehouse_pks)
+ stocks = stocks.values("product_variant_id").filter(
+ product_variant_id=OuterRef("pk")
+ )
- return qs.filter(Exists(variants.filter(product=OuterRef("pk"))))
+ stocks = Subquery(stocks.values_list(Sum("quantity")))
+ variants = ProductVariant.objects.annotate(
+ total_quantity=ExpressionWrapper(stocks, output_field=IntegerField())
+ )
+ variants = list(
+ filter_range_field(variants, "total_quantity", quantity_value).values_list(
+ "product_id", flat=True
+ )
+ )
+ return qs.filter(pk__in=variants)
class ProductStockFilterInput(graphene.InputObjectType):
diff --git a/saleor/product/migrations/0151_productchannellisting_product_pro_discoun_3145f3_btree.py b/saleor/product/migrations/0151_productchannellisting_product_pro_discoun_3145f3_btree.py
new file mode 100644
index 0000000000..8360f2b3b7
--- /dev/null
+++ b/saleor/product/migrations/0151_productchannellisting_product_pro_discoun_3145f3_btree.py
@@ -0,0 +1,21 @@
+# Generated by Django 3.2.6 on 2021-12-06 13:41
+
+import django.contrib.postgres.indexes
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("product", "0150_collection_collection_search_gin"),
+ ]
+
+ operations = [
+ migrations.AddIndex(
+ model_name="productchannellisting",
+ index=django.contrib.postgres.indexes.BTreeIndex(
+ fields=["discounted_price_amount"],
+ name="product_pro_discoun_3145f3_btree",
+ ),
+ ),
+ ]
diff --git a/saleor/product/migrations/0155_merge_20211208_1108.py b/saleor/product/migrations/0155_merge_20211208_1108.py
new file mode 100644
index 0000000000..ed2dc0c967
--- /dev/null
+++ b/saleor/product/migrations/0155_merge_20211208_1108.py
@@ -0,0 +1,13 @@
+# Generated by Django 3.2.6 on 2021-12-08 11:08
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("product", "0151_productchannellisting_product_pro_discoun_3145f3_btree"),
+ ("product", "0154_productvariant_quantity_limit_per_customer"),
+ ]
+
+ operations = []
diff --git a/saleor/product/models.py b/saleor/product/models.py
index b13451b56a..617ebde653 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -5,7 +5,7 @@ from uuid import uuid4
import graphene
from django.conf import settings
from django.contrib.postgres.aggregates import StringAgg
-from django.contrib.postgres.indexes import GinIndex
+from django.contrib.postgres.indexes import BTreeIndex, GinIndex
from django.contrib.postgres.search import SearchVectorField
from django.core.validators import MinValueValidator
from django.db import models
@@ -548,6 +548,7 @@ class ProductChannelListing(PublishableModel):
ordering = ("pk",)
indexes = [
models.Index(fields=["publication_date"]),
+ BTreeIndex(fields=["discounted_price_amount"]),
]
def is_available_for_purchase(self):