# DB querying performance optimization working with model methods.

**URL:** <https://forum.djangoproject.com/t/db-querying-performance-optimization-working-with-model-methods/37571>\
**Category:** Using the ORM\
**Created:** [December 30, 2024, 4:23pm UTC](https://forum.djangoproject.com/t/db-querying-performance-optimization-working-with-model-methods/37571 "2024-12-30T16:23:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![antony-kosenko](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/antony-kosenko/32/25591_2.png) [@antony-kosenko](https://forum.djangoproject.com/u/antony-kosenko)\
**Post date:** [December 30, 2024, 4:23pm UTC](https://forum.djangoproject.com/t/db-querying-performance-optimization-working-with-model-methods/37571/1 "2024-12-30T16:23:26Z")

</div>

Hi everyone.  
I’m having excessive DB calls number caused “n+1” problem whilce accesing instance’s method in template (which make some DB calls as well).  
Will try to explaing in details:

Models :

```auto
class Stock(models.Model):
    title = models.CharField()
    type = models.ForeignKey(StockType)

class Item(models.Model):
    name = models.CharField()
    
    def get_stock_quantity(self, stock):
        stock_item = self.stock_items.filter(stock=stock).first()
        return stock_item.quantity if stock_item else 0

    def get_stock_quantity_total(self):
        """ Returns total quantity of Item among all stocks """
        items = self.stock_items.all().only("quantity")
        return items.aggregate(sum_qty=models.Sum('quantity'))\
                    .get("sum_qty") or 0

class StockItem(models.Model):
    item = models.ForeignKey(Item)
    stock = models.ForeignKey(Stock)
    quantity = models.PositiveIntegerField()

```

View:

```auto
class StockBalanceView(ListView):
    model = models.Item
    template_name = 'stock_management/stock_balance.html'
    context_object_name = 'items'

    def get_queryset(self):
        qs = super().get_queryset()

        return qs.prefetch_related("stock_items")

```

The methods `get_stock_quantity` and `get_stock_quantity_total` produces huge amount DB calls.  
As I understand the flow: for every `Item` in queryset ORM performs DB call to fetch all `StockItem` instances related for this item. Howevert using `.prefetch_related()` for `stock_items`, `stock_items__stock` won’t reduce number of DB calls. Even after I set `.only("quantity")` did nothing in terms of performance.

In `DjDT` toolbar I’m getting somethin like:

```auto
+ **SELECT** ••• **FROM**"stock_management_stockitem" **WHERE**"stock_management_stockitem"."item_id" = 165
**406 similar queries.**  **Duplicated 2 times.** 0.40

+ **SELECT** ••• **FROM**"stock_management_stockitem" **WHERE**"stock_management_stockitem"."item_id" = 165
**406 similar queries.**  **Duplicated 2 times.**

```

Refactoring code with `.annotate()` reduces DB calls by half (to 203). Why half ?

What I’m missing here?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 30, 2024, 5:20pm UTC](https://forum.djangoproject.com/t/db-querying-performance-optimization-working-with-model-methods/37571/2 "2024-12-30T17:20:32Z")

</div>

What is it that you’re really trying to calculate here?

Are you trying to calculate the sum of the `StockItem.quantity` field for each `Item`?  
If so, you’re doing a lot more database work than necessary.  
(If not, then can you clarify what it _is_ that you’re trying to calculate?)

Also note: You haven’t posted the complete view here - or if it is the complete view, then you need to provide the template, because you aren’t showing anywhere that `get_stock_quantity` or `get_stock_quantity_total` are being called.

---

<div class="post-metadata">

**Author:** ![antony-kosenko](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/antony-kosenko/32/25591_2.png) [@antony-kosenko](https://forum.djangoproject.com/u/antony-kosenko)\
**Post date:** [December 31, 2024, 9:10am UTC](https://forum.djangoproject.com/t/db-querying-performance-optimization-working-with-model-methods/37571/3 "2024-12-31T09:10:51Z")

</div>

Hello, @KenWhitesell.  
You are right. Method calculates the `StockItem` q-ty in each `Stock` through `Item` reverse relation.  
I managed to reduce all queries produced by `get_stock_quantity_total` replacing method call with `.annotate()` and `Sum`. (query reduced from 406 to 5) but still having performance issue with `get_stock_quantity`.

View:

```auto
class StockBalanceView(ListView):
    model = models.Item
    template_name = 'stock_management/stock_balance.html'
    context_object_name = 'items'

    def get_queryset(self):
        qs = super().get_queryset()
        return qs.select_related("maker", "item_type")\
                 .annotate(
                     total=Sum('stock_items__quantity')
                 )

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        context['stocks'] = models.Stock.objects.all()\
                                                .select_related("stock_type", "entity")
        return context

```

Custom tag:

```auto
@register.filter
def get_stock_quantity(item: Item, stock) -> int:
    return item.get_stock_quantity(stock)

```

Template:

```auto
        <table class="table table-bordered sortable shadow-sm align-middle">
            <thead class="table-secondary align-middle">
                <tr class="text-center">
                    <th>Title</th>
                    <th>Maker</th>
                    <th>Category</th>

                    {% for stock in stocks %}
                    <th>
                        <span><a href="{{ stock.get_absolute_url }}" class="link-success" style="text-decoration: none;">{{ stock.name }}</a></span>
                        <br>
                        <span>{{stock.stock_type}} ({{stock.entity}})</span>
                    </th>
                    {% endfor %}

                    <th class="text-center">Total</th>
                </tr>
            </thead>
            <tbody>
                {% for item in items %}
                    <tr class="text-center">
                        <td class="text-start">{{ item.name }}</td>
                        <td>{{ item.maker }}</td>
                        <td>{{ item.item_type }}</td>
                        {% for stock in stocks %}
                            {% with quantity=item|get_stock_quantity:stock %}
                            <td class="{% if quantity < 0 %}bg-warning bg-gradient{% endif %}">{{ quantity }}</td>
                            {% endwith %}
                        {% endfor %}
                        <td class="{% if item.total < 0 %}bg-warning bg-gradient{% endif %}">{{ item.total }}</td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>

```

I guess there is a way with aggregation but can’t figure out for now how to associate `Item` with particular `Stock` in the table head. I guess `Case/When` the way but still no strict solution.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 31, 2024, 1:21pm UTC](https://forum.djangoproject.com/t/db-querying-performance-optimization-working-with-model-methods/37571/4 "2024-12-31T13:21:55Z")

</div>

You should be able to reduce the number of queries by using a [`prefetch_related`](https://docs.djangoproject.com/en/5.1/ref/models/querysets/#prefetch-related) in both queries being executed to “pre-fetch” the related entries of the `StockItem` model.

Also, there _may_ be some “notational” benefit if you defined the `ManyToManyField` between `Stock` and `Item` with `StockItem` as the `through` model.

Also, it looks like you’re iterating through **all** `Stock` for **every** `Item` - even if there’s no relationship between the two. Is that correct? (Is that the desired behavior?)
