Skip to content

Commit

Permalink
Merge pull request #161 from zorun/optimise_sql
Browse files Browse the repository at this point in the history
Optimise sql queries
  • Loading branch information
almet authored Jan 16, 2017
2 parents dc75a72 + 8e5ac8a commit 8615fde
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion budget/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def balance(self):
# for each person
for person in self.members:
# get the list of bills he has to pay
bills = Bill.query.filter(Bill.owers.contains(person))
bills = Bill.query.options(orm.subqueryload(Bill.owers)).filter(Bill.owers.contains(person))
for bill in bills.all():
if person != bill.payer:
share = bill.pay_each() * person.weight
Expand Down
4 changes: 3 additions & 1 deletion budget/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from flask.ext.babel import get_locale, gettext as _
from smtplib import SMTPRecipientsRefused
import werkzeug
from sqlalchemy import orm

# local modules
from models import db, Project, Person, Bill
Expand Down Expand Up @@ -277,7 +278,8 @@ def list_bills():
# set the last selected payer as default choice if exists
if 'last_selected_payer' in session:
bill_form.payer.data = session['last_selected_payer']
bills = g.project.get_bills()
# Preload the "owers" relationship for all bills
bills = g.project.get_bills().options(orm.subqueryload(Bill.owers))

return render_template("list_bills.html",
bills=bills, member_form=MemberForm(g.project),
Expand Down

0 comments on commit 8615fde

Please sign in to comment.