Community: Framework mailing list archives

expert-framework@mail.odoo.com

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

by
Michael Telahun
- 11/06/2014 06:49:40

On 11/06/2014 11:12 AM, Hans Yonathan wrote:
> Hi Fabien,
> 
>  
> 
> Wow, I just know that browse is better than read,
> 
> Actually because I read developer memento (there’s a note that read
> method is faster that browse), so I always assume that read is faster.
> 
> But in practice, I always use browse because it easier to code, and I
> don’t find any different of performance for it.

This is my experience as well. I once tried optimizing some payroll code
with read() instead of browse() and found that it took much longer. Ever
since then I prefer browse() over read().

> 
> How about the note that we must avoid use browse method inside looping?

I think what you may be referring to is a recommendation to *NOT* use
browse inside a loop. For example:

Do This:

for record in self.browse():
    ....

Not This:

for foo in bars:
    record = self.browse()
    ....

In the first instance the SQL query for the records is done once, at the
beginning, but in the second it is being called once for every iteration
of the loop.

Regards,
Mike.


> 
>  
> 
>  
> 
> Thank You.
> 
>  
> 
> Best Regards,
> 
> *Hans Yonathan*
> 
> *Odoo Support*
> 
> FALINWA Limited
> 
> /Expert in Finance, Information System & Business Intelligence/
> 
> /Odoo Silver Partner/
> 
> /Website: //www.falinwa.com/ 
> 
> /Tel. 13670137019 (China - Shenzhen)///
> 
>  
> 
> *From:*Fabien Pinckaers [mailto:fp@odoo.com]
> *Sent:* Thursday, November 06, 2014 4:02 PM
> *To:* Community: Framework
> *Subject:* Re: Browse() vs read() performance in Odoo 8
> 
>  
> 
> 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)
> 
>  
> 
>  
> 
> 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: https://www.odoo.com/groups/community-framework-62
> 
>> Post to: mailto:expert-framework@mail.odoo.com
> 
>> Unsubscribe: https://www.odoo.com/groups?unsubscribe
> 
>> 
> 
>  
> 
>  
> 
> -- 
> 
> Fabien Pinckaers
> 
> Odoo Founder
> 
>  
> 
> Phone: +32.81.81.37.00
> 
> Web: https://www.odoo.com
> 
> Twitter: @fpodoo
> 
>  
> 
> Instant Demo: https://odoo.com/start
> 
> _______________________________________________
> Mailing-List: https://www.odoo.com/groups/community-framework-62
> Post to: mailto:expert-framework@mail.odoo.com
> Unsubscribe: https://www.odoo.com/groups?unsubscribe
> 
> _______________________________________________
> Mailing-List: https://www.odoo.com/groups/community-framework-62
> Post to: mailto:expert-framework@mail.odoo.com
> Unsubscribe: https://www.odoo.com/groups?unsubscribe
>