From 4fe0c6bd60d3d092f69a1fec213ddcb9d5eeb8a6 Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Thu, 2 Jul 2015 20:59:26 +0200 Subject: [PATCH] [IMP] account: updated index to optimize _default_get of account.move.line A log analysis showed that the normalized query below was executed very often with a slow explain plan using a seq scan. ```sql SELECT move_id, date FROM account_move_line WHERE journal_id = AND period_id = AND create_uid = AND state = 'draft' ORDER BY id DESC LIMIT 0; ``` This query is called in the _default_get of account.move.line to find the last unbalanced move line. The existing index can be improved to cover this query as well, showing an impressive improvement of the explain plan as explained here: https://github.com/odoo/odoo/pull/7430#issuecomment-119521031 Closes #7430 --- addons/account/account_move_line.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/addons/account/account_move_line.py b/addons/account/account_move_line.py index 230626d0b9a..df6cd1dc8d9 100644 --- a/addons/account/account_move_line.py +++ b/addons/account/account_move_line.py @@ -575,7 +575,8 @@ class account_move_line(osv.osv): res = super(account_move_line, self)._auto_init(cr, context=context) cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'account_move_line_journal_id_period_id_index\'') if not cr.fetchone(): - cr.execute('CREATE INDEX account_move_line_journal_id_period_id_index ON account_move_line (journal_id, period_id)') + cr.execute('CREATE INDEX account_move_line_journal_id_period_id_index ' + 'ON account_move_line (journal_id, period_id, state, create_uid, id DESC)') cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('account_move_line_date_id_index',)) if not cr.fetchone(): cr.execute('CREATE INDEX account_move_line_date_id_index ON account_move_line (date DESC, id desc)')