From 29cd04d3cbaaf2a166ef387af6034238b901b4f0 Mon Sep 17 00:00:00 2001 From: Diptesh Choudhuri Date: Wed, 11 Dec 2024 21:26:07 +0530 Subject: [PATCH] perf(services/exporter): remove n+1 query --- crates/services/exporter/src/lib.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/crates/services/exporter/src/lib.rs b/crates/services/exporter/src/lib.rs index 575f0ffd74..7414bf22b3 100644 --- a/crates/services/exporter/src/lib.rs +++ b/crates/services/exporter/src/lib.rs @@ -6,6 +6,7 @@ use chrono::{DateTime, Utc}; use common_models::ExportJob; use common_utils::{IsFeatureEnabled, TEMP_DIR}; use database_models::{ + exercise, prelude::{ Exercise, Metadata, MetadataGroup, Person, Seen, UserToEntity, Workout, WorkoutTemplate, }, @@ -372,19 +373,12 @@ impl ExporterService { user_id: &String, writer: &mut JsonStreamWriter, ) -> Result<()> { - let exercises = UserToEntity::find() - .filter(user_to_entity::Column::UserId.eq(user_id)) - .filter(user_to_entity::Column::ExerciseId.is_not_null()) + let exercises = Exercise::find() + .filter(exercise::Column::CreatedByUserId.eq(user_id)) .all(&self.0.db) .await .unwrap(); - for rm in exercises.iter() { - let e = rm - .find_related(Exercise) - .one(&self.0.db) - .await - .unwrap() - .unwrap(); + for e in exercises { let reviews = item_reviews(user_id, &e.id, EntityLot::Exercise, false, &self.0) .await? .into_iter() @@ -397,7 +391,7 @@ impl ExporterService { .map(|c| c.name) .collect(); let exp = ImportOrExportExerciseItem { - name: e.id, + name: e.name, collections, reviews, };