On 2016-01-04 10:36, Jon Levy wrote: > I'm working on a script that reveals a strange performance issue that I'm > wondering if anyone is aware of. My script retrieves around 1,000 > instances of Model1, and calls a method on each of those instances, with > each method call generating an instance of Model2. > > I call Model1's search method to get the 1,000-active-record list. Here's > the strange part: calling the instance method on the active records > retrieve from search gives poor performance (around 1,250 seconds for my > script); but if I create new active records using the ids of the > search-retrieved records and calling Model1(id) on each of those ids, > performance is much better (about 95 seconds). > > The script breaks my 1,000 item list into 50-item sublists, and I get the > described performance improvement by inserting the following line into my > sublist-processing code: > > recs = [Model1(i) for i in [i.id for i in recs]] # recs is the sublist. > > Anyone have a clue why this is happening?
I suspect that you are writing on the database after/during each call to the instance method and you are accessing some Function field. If it is the case, here is what could happen. At the first call Tryton will compute the Function fields for the all 1000 records. Then if you call write/create (or save) the local cache will be cleared. And so on the next call to the method, Tryton will compute again the Function fields for the 999 records and so on. So it is always better to create/write at the end of the computation if possible or by batch of cache_size(). And this can also explain why by instantiating record individually the record, you have better performance because in this case the local cache is only computed for 1 instance. -- Cédric Krier - B2CK SPRL Email/Jabber: [email protected] Tel: +32 472 54 46 59 Website: http://www.b2ck.com/ -- You received this message because you are subscribed to the Google Groups "tryton" group. To view this discussion on the web visit https://groups.google.com/d/msgid/tryton/20160104201225.GS11765%40tetsuo.wifi.b2ck.com.
