From 8efeb4c6fca0131570ed17c02dd822e44f7ade38 Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Fri, 11 Aug 2017 16:02:27 +0200 Subject: [PATCH] [FIX] prefetch issues on computed fields (#18799) When deciding to prefetch records (getting records from the cache with no value for the field being fetched), if the field was computed `determine_value` would just get all records, not limited by the normal prefetch limit; for large recordsets this would generate gigantic prefetch lists for records we may not need at all. Fix by applying the `PREFETCH_MAX` limit to records from the cache as is done in `_prefetch_field`. --- openerp/models.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/openerp/models.py b/openerp/models.py index a9babb84126..4dd70e738d6 100644 --- a/openerp/models.py +++ b/openerp/models.py @@ -3206,9 +3206,6 @@ class BaseModel(object): # fetch the records of this model without field_name in their cache records = self._in_cache_without(field) - if len(records) > PREFETCH_MAX: - records = records[:PREFETCH_MAX] | self - # determine which fields can be prefetched if not self.env.in_draft and \ self._context.get('prefetch_fields', True) and \ @@ -5675,16 +5672,19 @@ class BaseModel(object): return RecordCache(self) @api.model - def _in_cache_without(self, field): - """ Make sure ``self`` is present in cache (for prefetching), and return - the records of model ``self`` in cache that have no value for ``field`` - (:class:`Field` instance). + def _in_cache_without(self, field, limit=PREFETCH_MAX): + """ Return records to prefetch that have no value in cache for ``field`` + (:class:`Field` instance), including ``self``. + Return at most ``limit`` records. """ env = self.env prefetch_ids = env.prefetch[self._name] prefetch_ids.update(self._ids) ids = filter(None, prefetch_ids - set(env.cache[field])) - return self.browse(ids) + recs = self.browse(ids) + if limit and len(recs) > limit: + recs = self + (recs - self)[:(limit - len(self))] + return recs @api.model def refresh(self):