Skip to content

Commit

Permalink
Optimize SQL queries when searching a rule
Browse files Browse the repository at this point in the history
Searching all rules then filtering in python the parent path is
more efficient than finding all the parent locations and finding
the matching rules.
  • Loading branch information
guewen committed May 15, 2020
1 parent 89e4ea2 commit 768f186
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion stock_reserve_rule/models/stock_reserve_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ def _constraint_fallback_location_id(self):
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)]
Expand Down

0 comments on commit 768f186

Please sign in to comment.