Odoo is the world's easiest all-in-one management software. It includes hundreds of business apps:
CRM | e-Commerce | Accounting | Inventory | PoS | Project management | MRP | etc.
When developing with the API 7, I always prefer the read() instead of the browse() method.
Indeed it seems that a browse fetches all the fields in the table, whereas the read fetches only the few it needs (that are passed to them), thus resulting in a much faster speed.
Actually I never really liked it, because it is easier/more intuitive to use the browse, and the code is nicer.
So what about performance issues in Odoo8?
Is it still recommended to prefer the read() in the API 8?
It would be a shame, moreover now because the browse() in the new API seems so much easier to use.
Thanks for your hints,
I am posting below the answer that Fabien Pinckaers (founder of Odoo) gave me on the Odoo mailing list. Other people also confirmed to me that they did some benchmarks as well, and they found out that browse() result in many cases to be actually faster than read().
Hello, Using read() is a bad practice. read() is used for web-services calls but in your own method calls you should always use browse(). Not only, it allows s a better quality of the code, but it's also better for the performance.
- read() calls name_get for many2one fields producing extra SQL queries you probably don't need.
- browse() is optimized for prefetching and auto-load of fields.
It's true that browse() may load a few fields you do not need (not all). It prefetches stored fields, because those fields do not costs anything to load in terms of performance.
It's very complex to optimize for performance with read() when the code is complex (involves loops, other method calls). Whereas, with browse(), the framework do the optimization job for you.
Usually, code implemented with read are often with complexities of O(n) or O(n²) as soon as there is loops in your methods. But codes written with browse() are automatically O(1) if browse is called at the beginning of your computations. (not inside loops)
Want a small example? Try this code on res.partner:
for company in self.browse(cr, uid, ids, context=context):
for people in company.child_ids:
print company.name, people.country_id.code
The above will do 6 SQL queries, whatever the length of ids and number of people/countries. (if IDS>200, he will split into subqueries)
The same code with read(), you will probably end up with 3*len(ids) + 3 queries.
Long story short: browse() scale, read() not. (even in v7 or preceding versions)
About This Community
This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.Register
Odoo Training Center
Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.Test it now
|Asked: 11/11/14, 10:39 AM|
|Seen: 2949 times|
|Last updated: 8/26/15, 9:29 AM|