perf(health): get_health_summary 4次串行查询改为 tokio::join! 并行

总延迟从 sum(4次查询) 降为 max(4次查询),预估延迟降低约75%。
This commit is contained in:
iven
2026-04-27 09:52:31 +08:00
parent 04c5f3c0d5
commit 0a387c189a

View File

@@ -428,42 +428,44 @@ pub async fn get_health_summary(
find_patient(&state.db, tenant_id, patient_id).await?; find_patient(&state.db, tenant_id, patient_id).await?;
use crate::entity::{vital_signs, lab_report, appointment, follow_up_task}; use crate::entity::{vital_signs, lab_report, appointment, follow_up_task};
use sea_orm::QueryOrder;
// 最新体征 // 4 个查询并行执行
let latest_vitals = vital_signs::Entity::find() let (latest_vitals_res, latest_lab_res, upcoming_res, pending_follow_ups_res) = tokio::join!(
.filter(vital_signs::Column::TenantId.eq(tenant_id)) // 最新体征
.filter(vital_signs::Column::PatientId.eq(patient_id)) vital_signs::Entity::find()
.filter(vital_signs::Column::DeletedAt.is_null()) .filter(vital_signs::Column::TenantId.eq(tenant_id))
.order_by_desc(vital_signs::Column::RecordDate) .filter(vital_signs::Column::PatientId.eq(patient_id))
.one(&state.db) .filter(vital_signs::Column::DeletedAt.is_null())
.await?; .order_by_desc(vital_signs::Column::RecordDate)
.one(&state.db),
// 最新化验
lab_report::Entity::find()
.filter(lab_report::Column::TenantId.eq(tenant_id))
.filter(lab_report::Column::PatientId.eq(patient_id))
.filter(lab_report::Column::DeletedAt.is_null())
.order_by_desc(lab_report::Column::ReportDate)
.one(&state.db),
// 待处理预约数
appointment::Entity::find()
.filter(appointment::Column::TenantId.eq(tenant_id))
.filter(appointment::Column::PatientId.eq(patient_id))
.filter(appointment::Column::Status.eq("pending"))
.filter(appointment::Column::DeletedAt.is_null())
.count(&state.db),
// 待办随访数
follow_up_task::Entity::find()
.filter(follow_up_task::Column::TenantId.eq(tenant_id))
.filter(follow_up_task::Column::PatientId.eq(patient_id))
.filter(follow_up_task::Column::Status.eq("pending"))
.filter(follow_up_task::Column::DeletedAt.is_null())
.count(&state.db),
);
// 最新化验 let latest_vitals = latest_vitals_res?;
let latest_lab = lab_report::Entity::find() let latest_lab = latest_lab_res?;
.filter(lab_report::Column::TenantId.eq(tenant_id)) let upcoming = upcoming_res?;
.filter(lab_report::Column::PatientId.eq(patient_id)) let pending_follow_ups = pending_follow_ups_res?;
.filter(lab_report::Column::DeletedAt.is_null())
.order_by_desc(lab_report::Column::ReportDate)
.one(&state.db)
.await?;
// 待处理预约数
let upcoming = appointment::Entity::find()
.filter(appointment::Column::TenantId.eq(tenant_id))
.filter(appointment::Column::PatientId.eq(patient_id))
.filter(appointment::Column::Status.eq("pending"))
.filter(appointment::Column::DeletedAt.is_null())
.count(&state.db)
.await?;
// 待办随访数
let pending_follow_ups = follow_up_task::Entity::find()
.filter(follow_up_task::Column::TenantId.eq(tenant_id))
.filter(follow_up_task::Column::PatientId.eq(patient_id))
.filter(follow_up_task::Column::Status.eq("pending"))
.filter(follow_up_task::Column::DeletedAt.is_null())
.count(&state.db)
.await?;
Ok(serde_json::json!({ Ok(serde_json::json!({
"patient_id": patient_id, "patient_id": patient_id,