[FIX] prefetch issues on computed/related fields (#18644)
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`. Complementarily, when traversing related fields the prefetch environment would be lost and every record would get an empty prefetch environment, so the values would ultimately be read one by one. Example: select (search) 1000 product.product records, access a related field (e.g. categ_id) in a loop, on the first iteration the system would first read 1000 templates, then it would read each categ_id individually, resulting in >1000 SQL queries rather than the ~2 we would expect. Fixes #18511
This commit is contained in:
+2
-1
@@ -565,7 +565,7 @@ class Field(object):
|
||||
""" Traverse the fields of the related field `self` except for the last
|
||||
one, and return it as a pair `(last_record, last_field)`. """
|
||||
for name in self.related[:-1]:
|
||||
record = record[name][:1]
|
||||
record = record[name][:1].with_prefetch(record._prefetch)
|
||||
return record, self.related_field
|
||||
|
||||
def _compute_related(self, records):
|
||||
@@ -978,6 +978,7 @@ class Field(object):
|
||||
self.compute_value(record)
|
||||
else:
|
||||
recs = record._in_cache_without(self)
|
||||
recs = recs.with_prefetch(record._prefetch)
|
||||
self.compute_value(recs)
|
||||
|
||||
else:
|
||||
|
||||
+8
-9
@@ -3058,10 +3058,6 @@ class BaseModel(object):
|
||||
else:
|
||||
records &= self._in_cache_without(f)
|
||||
|
||||
# prefetch at most PREFETCH_MAX records
|
||||
if len(records) > PREFETCH_MAX:
|
||||
records = records[:PREFETCH_MAX] | self
|
||||
|
||||
# fetch records with read()
|
||||
assert self in records and field in fs
|
||||
records = records.with_prefetch(self._prefetch)
|
||||
@@ -5232,13 +5228,16 @@ 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.
|
||||
"""
|
||||
ids = filter(None, self._prefetch[self._name] - set(self.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):
|
||||
|
||||
Reference in New Issue
Block a user