[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 = <journal_id>
AND period_id = <period_id>
AND create_uid = <user_id>
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
This commit is contained in:
Guewen Baconnier 2015-07-02 20:59:26 +02:00 committed by Olivier Dony
parent 166839fb46
commit 4fe0c6bd60
1 changed files with 2 additions and 1 deletions

View File

@ -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)')