Optimize SQL queries when searching a rule

Searching all rules then filtering in python the parent path is
more efficient than finding all the parent locations and finding
the matching rules.
This commit is contained in:
Guewen Baconnier
2020-05-15 12:16:32 +02:00
committed by Sébastien Alix
parent 4ae12c21ce
commit c837adc236

View File

@@ -82,7 +82,14 @@ class StockReserveRule(models.Model):
raise ValidationError(msg)
def _rules_for_location(self, location):
return self.search([("location_id", "parent_of", location.id)])
# We'll typically have a handful of rules, so reading all of them then
# checking if they are a parent location of the location is pretty
# fast. Searching all the parent locations then the rules matching them
# can be much slower if we have many locations.
rules = self.search([]).filtered(
lambda rule: rule.location_id.parent_path.startswith(location.parent_path)
)
return rules
def _eval_rule_domain(self, move, domain):
move_domain = [("id", "=", move.id)]