diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d2afcb3..bf330eb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# UNRELEASED + +- Improved speed of the `/api/v1/challenges/[challenge_id]/solves` endpoint + # 3.4.0 / 2021-08-11 **General** diff --git a/CTFd/api/v1/challenges.py b/CTFd/api/v1/challenges.py index e5594999..8d141a1c 100644 --- a/CTFd/api/v1/challenges.py +++ b/CTFd/api/v1/challenges.py @@ -764,8 +764,13 @@ class ChallengeSolves(Resource): Model = get_model() + # Note that we specifically query for the Solves.account.name + # attribute here because it is faster than having SQLAlchemy + # query for the attribute directly and it's unknown what the + # affects of changing the relationship lazy attribute would be solves = ( - Solves.query.join(Model, Solves.account_id == Model.id) + Solves.query.add_columns(Model.name.label("account_name")) + .join(Model, Solves.account_id == Model.id) .filter( Solves.challenge_id == challenge_id, Model.banned == False, @@ -782,10 +787,12 @@ class ChallengeSolves(Resource): solves = solves.filter(Solves.date < dt) for solve in solves: + # Seperate out the account name and the Solve object from the SQLAlchemy tuple + solve, account_name = solve response.append( { "account_id": solve.account_id, - "name": solve.account.name, + "name": account_name, "date": isoformat(solve.date), "account_url": generate_account_url(account_id=solve.account_id), }