Skip to content

Commit

Permalink
Massive performance improvement
Browse files Browse the repository at this point in the history
No clue why I put in a super inefficient count query
  • Loading branch information
jdabtieu committed Nov 20, 2023
1 parent 4c6b14f commit 4562afb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def index():

data = db.execute(
"SELECT * FROM announcements ORDER BY id DESC LIMIT 10 OFFSET ?", page)
length = len(db.execute("SELECT * FROM announcements"))
length = db.execute("SELECT COUNT(*) AS cnt FROM announcements")[0]["cnt"]

if not session or 'username' not in session:
template = read_file(app.config['HOMEPAGE_FILE'])
Expand Down Expand Up @@ -664,15 +664,15 @@ def problems():
"problems.id=problem_solved.problem_id WHERE (draft=0 AND category=?)"
"GROUP BY problems.id ORDER BY id ASC LIMIT 50 OFFSET ?"),
category, page)
length = len(db.execute("SELECT * FROM problems WHERE (draft=0 AND category=?)",
category))
length = db.execute(("SELECT COUNT(*) AS cnt FROM problems WHERE "
"draft=0 AND category=?"), category)[0]["cnt"]
else:
data = db.execute(
("SELECT problems.*, COUNT(DISTINCT problem_solved.user_id) AS sols "
"FROM problems LEFT JOIN problem_solved ON "
"problems.id=problem_solved.problem_id WHERE draft=0 "
"GROUP BY problems.id ORDER BY id ASC LIMIT 50 OFFSET ?"), page)
length = len(db.execute("SELECT * FROM problems WHERE draft=0"))
length = db.execute("SELECT COUNT(*) AS cnt FROM problems WHERE draft=0")[0]["cnt"] # noqa E501

categories = db.execute("SELECT DISTINCT category FROM problems WHERE draft=0")
categories.sort(key=lambda x: x['category'])
Expand Down Expand Up @@ -768,7 +768,7 @@ def draft_problems():
page = (int(page) - 1) * 50

data = db.execute("SELECT * FROM problems WHERE draft=1 LIMIT 50 OFFSET ?", page)
length = len(db.execute("SELECT * FROM problems WHERE draft=1"))
length = db.execute("SELECT COUNT(*) AS cnt FROM problems WHERE draft=1")[0]["cnt"]

return render_template('problem/draft_problems.html',
data=data, length=-(-length // 50))
Expand Down
8 changes: 4 additions & 4 deletions src/views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def admin_submissions():
page = (int(page) - 1) * 50
modifier += " 1=1"

length = len(db.execute(("SELECT submissions.*, users.username FROM submissions "
"LEFT JOIN users ON user_id=users.id") + modifier, *args))
length = db.execute(("SELECT COUNT(*) AS cnt FROM submissions LEFT JOIN users ON "
"user_id=users.id") + modifier, *args)[0]["cnt"]

args.append(page)
submissions = db.execute(("SELECT submissions.*, users.username FROM submissions "
Expand All @@ -80,7 +80,7 @@ def admin_users():
page = (int(page) - 1) * 50

data = db.execute("SELECT * FROM users ORDER BY id ASC LIMIT 50 OFFSET ?", page)
length = len(db.execute("SELECT * FROM users"))
length = db.execute("SELECT COUNT(*) AS cnt FROM users")[0]["cnt"]

perms = db.execute(("SELECT * FROM user_perms WHERE user_id IN "
"(SELECT id FROM users ORDER BY id ASC LIMIT 50 OFFSET ?)"), page)
Expand Down Expand Up @@ -352,7 +352,7 @@ def preview_homepage():

data = db.execute(
"SELECT * FROM announcements ORDER BY id DESC LIMIT 10 OFFSET ?", page)
length = len(db.execute("SELECT * FROM announcements"))
length = db.execute("SELECT COUNT(*) AS cnt FROM announcements")[0]["cnt"]

template_type = read_file(app.config['HOMEPAGE_FILE'])[0]
return render_template(f"home_fragment/home{template_type}.html",
Expand Down

0 comments on commit 4562afb

Please sign in to comment.