Yenthe666 / Odoo_Samples

This repository contains samples with Odoo code
GNU Affero General Public License v3.0
241 stars 306 forks source link

Odoo performance #12

Closed namdinh-novobi closed 5 years ago

namdinh-novobi commented 5 years ago

Hi Yenthe,

Today, I have found a thing correlated with the performance in Odoo, so I would like to discuss this with you to see whether it could be called an issue or not?

I have checked it on the latest Odoo (version 12), it has still been there.

The thing I have found, I will describe as code to esaier to explain:

  1. I have a model called "testA", a computed field called "computed_field" inside this model.

class TestA(models.Model): _name = "testA"

computed_field = fields.Char('Computed field', compute='_test_compute')

@api.multi
def _test_compute(self):
    return True
  1. In another model, I have a function to handle some thing, but I will loop on records in the model "testA" and call the computed field.

class TestB(models.Model): _name = "testB"

# some fields here, but we should not care..

def _test_function(self):

    # First, I will get all the data in the model "testA"

    # Getting all data or adding domains to filter, this is not important. The thing we need to have is list of data in testA. In this case, I will have all data, and for example I will have 100 records with ids from 1 to 100

    # testA_records = (1, 2, 3, ....., 100) => This is an sample I have the data in testA_records

    testA_records = self.env['testA'].search([])  

    # After that, I will do a loop on the array testA_records

    for item in testA_records:
        # call the compute field
        computed_value = item.computed_field
  1. At this time, we can see everything is normal. The thing I want to tell is:

If the current item in the loop is 1 (object of testA with id = 1), then when the computed function called, the value of self should be the same. It would be (object of testA with id = 1) too.

But, the thing I do not understand why is although the current item in the loop is 1, but the self in the computed function is one list of items with ids from 1 to 100?

This leads to an issue related to the performance. If the computed function taking to run about 5s for each call, we have 100 items here, it would take time to run is 5s * 100 = 500s. I see passing redundant items to self, it does not make sense, and it makes the time execution slower.

I do not know whether it should be an issue in Odoo or not. Anyone else, Can you explain its reason? and also the solution to bypass it.

Thank you!

Yenthe666 commented 5 years ago

Hi,

You're finding all records with testA_records = self.env['testA'].search([]) and you're looping over them one by one to trigger item.computed_field so it is logical that you're getting all records and they're all being computed?

P.S: Closing this as it is not really related to any off my modules but I'll still get notifications.

namdinh-novobi commented 5 years ago

Yenthe, That's correct. I do not understand why it does not mapped one by one, but it passed list of redudant items while calculating is just related to one item.

Sorry for posting to the wrong issue, because I would like to contact you, but I do not know how to do :).

In case, I need your help. What do i need to do?

Thanks, -Nam

Vào Th 5, 12 thg 9, 2019 vào lúc 13:09 Yenthe Van Ginneken < notifications@github.com> đã viết:

Hi,

You're finding all records with testA_records = self.env['testA'].search([]) and you're looping over them one by one to trigger item.computed_field so it is logical that you're getting all records and they're all being computed?

P.S: Closing this as it is not really related to any off my modules but I'll still get notifications.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Yenthe666/Odoo_Samples/issues/12?email_source=notifications&email_token=AKH73LNR6SUSGQAHNQE45NLQJHMJXA5CNFSM4IV7MDE2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6QYTAQ#issuecomment-530680194, or mute the thread https://github.com/notifications/unsubscribe-auth/AKH73LM6CAGOYWDLQUCYUVTQJHMJXANCNFSM4IV7MDEQ .

Yenthe666 commented 5 years ago

Are you sure that you don't want to replace testA_records = self.env['testA'].search([]) by just the specific record? When you load all those records the compute is going off.

namdinh-novobi commented 5 years ago

I think looping over items to handle logics is normal. In some cases, we do not know which record is specific. I can give you an example like this '# example for calculting salary of employees. '# BONUS which is value passed to the function executing... maybe '# basic_salary is a computed field calculated based on LEVEL of employee maybe... '# employees, we need to have all employees.

for emp in employees: emp.total_salary = emp.basic_salary + BONUS

namdinh-novobi commented 5 years ago

The thing makes me confused is why there are redundant items passed to the computed function while the handling the logic is only correlated to the current item in the loop.