# Sales with “subtotal” lines affect performance

**URL:** https://discuss.tryton.org/t/sales-with-subtotal-lines-affect-performance/7369
**Category:** Developer
**Tags:** sales
**Created:** [June 9, 2024, 11:57am UTC](https://discuss.tryton.org/t/sales-with-subtotal-lines-affect-performance/7369 "2024-06-09T11:57:54Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![syswft](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@syswft](https://discuss.tryton.org/u/syswft)
#### Post date: [June 9, 2024, 11:57am UTC](https://discuss.tryton.org/t/sales-with-subtotal-lines-affect-performance/7369/1 "2024-06-09T11:57:54Z")

</div>

When we use lines of type “subtotal” in our sales, we experience a significant performance hit when loading the list of sales.

As I understand it, when the list of sales is loaded, the function “Sale.get\_amount” is called. This function contains the loop:

```auto
untaxed_amount[sale.id] = sum(
    (line.amount for line in sale.lines
        if line.type == 'line'), Decimal(0))

```

And even if only lines of type “line” are to be taken into account here, you can see that in the getter function for “amount” (“SaleLine.get\_amount”) the part in this function for “subtotals” is also called again and again. And this is the part that takes up to 8 seconds on our system, even with just a few (four) sales each with about five “subtotal” lines and about 150 “normal” lines.

I don’t understand why this is so - I suspect it has something to do with the getter for the “amount” field, because if I call “SaleLine.get\_amount” directly (as a test) using this code in “Sale.get\_amount”:

```auto
untaxed_amount[sale.id] = sum(
    (line.get_amount('') for line in sale.lines
        if line.type == 'line'), Decimal(0))

```

the performance problems are gone.

Unfortunately I can’t get any further here - maybe someone can help me?

Thanks

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [June 9, 2024, 12:42pm UTC](https://discuss.tryton.org/t/sales-with-subtotal-lines-affect-performance/7369/2 "2024-06-09T12:42:37Z")

</div>

Calling the getter directly is not that good because it prevents the ORM to pre-compute. The pre-computation is also the problem in this case because the filter on type of line is happening after the instanciation.  
One we would be to reinstantiate the lines after the filtering but then again we will loose some pre-computation.  
Finally I think the best option would be to have a `OneMany` for lines with a filter on the type `line`. As this is this loop only on type `line` is frequent in the code, it may deserve its own field.

The same would apply to purchase and invoice.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [June 22, 2024, 2:53pm UTC](https://discuss.tryton.org/t/sales-with-subtotal-lines-affect-performance/7369/3 "2024-06-22T14:53:14Z")

</div>

I created [Add filtered One2Many fields on sale, purchase and invoice (!1557) · Merge requests · Tryton / Tryton · GitLab](https://foss.heptapod.net/tryton/tryton/-/merge_requests/1557) which should improve the performance for this case.
