From 29a9cf981729cfe8e55caaadf6bf14a8a4a7708e Mon Sep 17 00:00:00 2001
From: Denis Ledoux <dle@odoo.com>
Date: Tue, 11 May 2021 19:55:36 +0000
Subject: [PATCH] [FIX] account: speeds up `account.payment` deletion

The deletion of `account.payment` were slowed down
because of the missing index

```
                                                                                  QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Delete on account_payment  (cost=20.37..44.45 rows=10 width=34) (actual time=0.449..0.450 rows=0 loops=1)
   ->  Nested Loop  (cost=20.37..44.45 rows=10 width=34) (actual time=0.383..0.404 rows=10 loops=1)
         ->  HashAggregate  (cost=19.95..20.05 rows=10 width=32) (actual time=0.378..0.380 rows=10 loops=1)
               Group Key: "ANY_subquery".id
               Batches: 1  Memory Usage: 24kB
               ->  Subquery Scan on "ANY_subquery"  (cost=0.42..19.92 rows=10 width=32) (actual time=0.035..0.371 rows=10 loops=1)
                     ->  Limit  (cost=0.42..19.82 rows=10 width=4) (actual time=0.032..0.365 rows=10 loops=1)
                           ->  Nested Loop  (cost=0.42..186848.76 rows=96312 width=4) (actual time=0.031..0.364 rows=10 loops=1)
                                 ->  Seq Scan on account_payment_pre_backup pay_backup  (cost=0.00..14763.48 rows=277317 width=4) (actual time=0.015..0.086 rows=53 loops=1)
                                       Filter: ((state)::text <> ALL ('{draft,cancelled}'::text[]))
                                       Rows Removed by Filter: 166
                                 ->  Index Scan using account_payment_pkey on account_payment pay  (cost=0.42..0.62 rows=1 width=4) (actual time=0.005..0.005 rows=0 loops=53)
                                       Index Cond: (id = pay_backup.id)
                                       Filter: (move_id IS NULL)
                                       Rows Removed by Filter: 1
         ->  Index Scan using account_payment_pkey on account_payment  (cost=0.42..2.44 rows=1 width=10) (actual time=0.002..0.002 rows=1 loops=10)
               Index Cond: (id = "ANY_subquery".id)
 Planning Time: 0.294 ms
 Trigger for constraint account_move_line_payment_id_fkey: time=1.236 calls=10
 Trigger for constraint account_payment_sale_order_rel_account_payment_id_fkey: time=0.195 calls=10
 Trigger for constraint credit_token_payment_id_fkey: time=1.308 calls=10
 Trigger for constraint payment_transaction_payment_id_fkey: time=0.288 calls=10
 Trigger for constraint account_invoice_payment_rel_payment_id_fkey1: time=0.191 calls=10
 Trigger for constraint account_move_payment_id_fkey: time=6015.799 calls=10
 Trigger for constraint account_payment_account_bank_statement__account_payment_id_fkey: time=0.444 calls=10
 Execution Time: 6019.988 ms
```

upg-12139

closes odoo/odoo#70712

Signed-off-by: Denis Ledoux (dle) <dle@odoo.com>
---
 addons/account/models/account_move.py | 1 +
 1 file changed, 1 insertion(+)

diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py
index 1fa1b0e30af3..d382db1b85b4 100644
--- a/addons/account/models/account_move.py
+++ b/addons/account/models/account_move.py
@@ -194,6 +194,7 @@ class AccountMove(models.Model):
     payment_reference = fields.Char(string='Payment Reference', index=True, copy=False,
         help="The payment reference to set on journal items.")
     payment_id = fields.Many2one(
+        index=True,
         comodel_name='account.payment',
         string="Payment", copy=False, check_company=True)
     statement_line_id = fields.Many2one(
-- 
GitLab