From fe95c9e9bdb233aa6af30aa9fa3fe6c2d7165d4c Mon Sep 17 00:00:00 2001 From: cmyui Date: Fri, 10 Nov 2023 22:31:10 -0500 Subject: [PATCH] Eliminate extra queries on get_rework_scores endpoint --- src/api/routes/reworks/scores.rs | 26 ++++++++++++-------------- src/models/score.rs | 1 + 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/api/routes/reworks/scores.rs b/src/api/routes/reworks/scores.rs index 4b3e5f5..c26f43a 100644 --- a/src/api/routes/reworks/scores.rs +++ b/src/api/routes/reworks/scores.rs @@ -27,16 +27,16 @@ async fn get_rework_scores( ) -> Json>> { let base_scores: Vec = sqlx::query_as( - "SELECT user_id, rework_scores.beatmap_id, rework_scores.beatmapset_id, rework_id, score_id, rework_scores.max_combo, mods, accuracy, score, num_300s, num_100s, num_50s, num_gekis, - num_katus, num_misses, old_pp, new_pp, - DENSE_RANK() OVER (ORDER BY old_pp DESC) old_rank, DENSE_RANK() OVER (ORDER BY new_pp DESC) new_rank - FROM - rework_scores + "SELECT user_id, rework_scores.beatmap_id, rework_scores.beatmapset_id, beatmap.song_name, rework_id, score_id, rework_scores.max_combo, mods, accuracy, score, num_300s, num_100s, num_50s, num_gekis, + num_katus, num_misses, old_pp, new_pp, + DENSE_RANK() OVER (ORDER BY old_pp DESC) old_rank, DENSE_RANK() OVER (ORDER BY new_pp DESC) new_rank + FROM + rework_scores INNER JOIN beatmaps ON rework_scores.beatmap_id = beatmaps.beatmap_id - WHERE + WHERE user_id = ? AND rework_id = ? - ORDER BY + ORDER BY new_pp DESC LIMIT 100", ) @@ -48,13 +48,11 @@ async fn get_rework_scores( let mut scores: Vec = Vec::new(); for base_score in base_scores { - let beatmap: Beatmap = sqlx::query_as( - "SELECT beatmap_id, beatmapset_id, song_name FROM beatmaps WHERE beatmap_id = ?", - ) - .bind(base_score.beatmap_id) - .fetch_one(ctx.database.get().await.unwrap().deref_mut()) - .await - .unwrap(); + let beatmap = Beatmap { + beatmap_id: base_score.beatmap_id, + beatmapset_id: base_score.beatmapset_id, + song_name: base_score.song_name, + }; let score = APIReworkScore::from_base(base_score, beatmap); scores.push(score); diff --git a/src/models/score.rs b/src/models/score.rs index 9c4adef..8d67be4 100644 --- a/src/models/score.rs +++ b/src/models/score.rs @@ -50,6 +50,7 @@ pub struct APIBaseReworkScore { pub score_id: i64, pub beatmap_id: i32, pub beatmapset_id: i32, + pub song_name: String, pub user_id: i32, pub rework_id: i32, pub max_combo: i32,