Community: Framework mailing list archives

Re: Browse() vs read() performance in Odoo 8

Fabien Pinckaers (fp)
- 11/06/2014 02:58:30

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

- 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, 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

Long story short: browse() scale, read() not. (even in v7 or preceding

On 11/05/2014 11:39 PM, Nicolas SCHMITT wrote:
> Hi all.
> 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,
> Cordially,
> Nicolas
> _______________________________________________
> Mailing-List:
> Post to:
> Unsubscribe:

Fabien Pinckaers
Odoo Founder

Phone: +
Twitter: @fpodoo

Instant Demo: