Skip to Content
Menu
This question has been flagged

Hi everyone,
Currently I have 60k account.move.line records in my system, and i have to set a value of compute field so while the compute runs it takes lot of time to set the value in field for 60k records...
Please advise me how can I optimize that.
This is on priority ,  I have pasted the method below to make my question more clear
Thanks in Advance


Compute Method:

line_obj = self.env['account.move.line']
for rec in self:
if rec.account_id:
query = """SELECT sum(balance) from account_move_line where account_id = %s and create_date < %s and date <= %s and id != %s """
self.env.cr.execute(query, (rec.account_id.id, rec.create_date, rec.date, rec.id))
running_balance = self.env.cr.fetchone()[0] or 0.0
if rec.debit:
running_balance += rec.debit
if rec.credit:
running_balance -= rec.credit
rec.running_balance = running_balance
else:
rec.running_balance = 0.00






Avatar
Discard
Author
I have already stored that value as I need that field in some report.
I know that it will obviously take take...I just wanted a efficient way that it can reduce somewhat time.

On Mon, Oct 18, 2021, 11:26 PM Devendra Kavthekar <dkatodoo@gmail.com> wrote:

A new answer on How can I improve Performance of Compute method in Account Move Line has been posted. Click here to access the post :

See post

Sent by Odoo S.A. using Odoo.

Best Answer

You can do this in batch of 10k records or minimum amount of records possible that can allow the commit to happen.

Not sure about the purpose - but it will take more time of course if the query is performed on large dataset.

By the way, it seems you need to:

Do:

date <= %s and id == %s """

Instead of:

date <= %s and id != %s """
?


As a side note, may be you already know:

Dont write compute methods at all, best way if possible. Use create/write or other methods instead.

And if you must write compute method - make it stored when possible.

Avatar
Discard
Related Posts Replies Views Activity
3
Dec 19
6727
0
Jul 15
2972
1
Jun 24
771
0
May 24
221
0
Sep 22
1620