Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
perf(calls): batch-fetch call record participants (#3836)
Evan Hutnikd5054f273f89
diff --git a/rust/cloud-storage/.sqlx/query-435f8b35060d08ba47b4676c968b0543e406adc48edfd2a72ed715164c4a093b.json b/rust/cloud-storage/.sqlx/query-435f8b35060d08ba47b4676c968b0543e406adc48edfd2a72ed715164c4a093b.jsonnew file mode 100644index 0000000000..bd8240acd1--- /dev/null+++ b/rust/cloud-storage/.sqlx/query-435f8b35060d08ba47b4676c968b0543e406adc48edfd2a72ed715164c4a093b.json@@ -0,0 +1,41 @@+{+ "db_name": "PostgreSQL",+ "query": "\n SELECT call_id AS \"id!\", user_id AS \"user_id!\", joined_at AS \"joined_at!\", left_at\n FROM call_participants\n WHERE call_id = ANY($1)\n UNION ALL\n SELECT call_record_id AS \"id!\", user_id AS \"user_id!\", joined_at AS \"joined_at!\", left_at\n FROM call_record_participants\n WHERE call_record_id = ANY($2)\n ORDER BY 3 ASC\n ",+ "describe": {+ "columns": [+ {+ "ordinal": 0,+ "name": "id!",+ "type_info": "Uuid"+ },+ {+ "ordinal": 1,+ "name": "user_id!",+ "type_info": "Text"+ },+ {+ "ordinal": 2,+ "name": "joined_at!",+ "type_info": "Timestamptz"+ },+ {+ "ordinal": 3,+ "name": "left_at",+ "type_info": "Timestamptz"+ }+ ],+ "parameters": {+ "Left": [+ "UuidArray",+ "UuidArray"+ ]+ },+ "nullable": [+ null,+ null,+ null,+ null+ ]+ },+ "hash": "435f8b35060d08ba47b4676c968b0543e406adc48edfd2a72ed715164c4a093b"+}diff --git a/rust/cloud-storage/.sqlx/query-7153394f8eb16ffb119e7cf8f3eb00791668bed040994bf21e00b397f4b05405.json b/rust/cloud-storage/.sqlx/query-7153394f8eb16ffb119e7cf8f3eb00791668bed040994bf21e00b397f4b05405.jsondeleted file mode 100644index 4f77384840..0000000000--- a/rust/cloud-storage/.sqlx/query-7153394f8eb16ffb119e7cf8f3eb00791668bed040994bf21e00b397f4b05405.json+++ /dev/null@@ -1,34 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n SELECT user_id, joined_at, left_at\n FROM call_participants\n WHERE call_id = $1\n ORDER BY joined_at ASC\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "user_id",- "type_info": "Text"- },- {- "ordinal": 1,- "name": "joined_at",- "type_info": "Timestamptz"- },- {- "ordinal": 2,- "name": "left_at",- "type_info": "Timestamptz"- }- ],- "parameters": {- "Left": [- "Uuid"- ]- },- "nullable": [- false,- false,- true- ]- },- "hash": "7153394f8eb16ffb119e7cf8f3eb00791668bed040994bf21e00b397f4b05405"-}diff --git a/rust/cloud-storage/.sqlx/query-7e26dfe09fa65aa0bad62337b51c2d22e4703c7fe2035725142f180193c35808.json b/rust/cloud-storage/.sqlx/query-7e26dfe09fa65aa0bad62337b51c2d22e4703c7fe2035725142f180193c35808.jsondeleted file mode 100644index a7c9206f75..0000000000--- a/rust/cloud-storage/.sqlx/query-7e26dfe09fa65aa0bad62337b51c2d22e4703c7fe2035725142f180193c35808.json+++ /dev/null@@ -1,34 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n SELECT user_id, joined_at, left_at\n FROM call_record_participants\n WHERE call_record_id = $1\n ORDER BY joined_at ASC\n ",- "describe": {- "columns": [- {- "ordinal": 0,- "name": "user_id",- "type_info": "Text"- },- {- "ordinal": 1,- "name": "joined_at",- "type_info": "Timestamptz"- },- {- "ordinal": 2,- "name": "left_at",- "type_info": "Timestamptz"- }- ],- "parameters": {- "Left": [- "Uuid"- ]- },- "nullable": [- false,- false,- true- ]- },- "hash": "7e26dfe09fa65aa0bad62337b51c2d22e4703c7fe2035725142f180193c35808"-}diff --git a/rust/cloud-storage/call/src/outbound/pg_call_repo.rs b/rust/cloud-storage/call/src/outbound/pg_call_repo.rsindex 99016f2660..dc9e325c4e 100644--- a/rust/cloud-storage/call/src/outbound/pg_call_repo.rs+++ b/rust/cloud-storage/call/src/outbound/pg_call_repo.rs@@ -1291,48 +1291,56 @@ impl CallRepository for PgCallRepo {.fetch_all(&self.pool).await?;- let mut records = Vec::with_capacity(rows.len());- for row in rows {- // Fetch participants from the appropriate table.- let participants = if row.is_active {- sqlx::query!(- r#"- SELECT user_id, joined_at, left_at- FROM call_participants- WHERE call_id = $1- ORDER BY joined_at ASC- "#,- row.call_id,- )- .fetch_all(&self.pool)- .await?- .into_iter()- .map(|p| CallRecordParticipant {- user_id: p.user_id,- joined_at: p.joined_at,- left_at: p.left_at,- })- .collect()+ // Split ids by source table: active participants live in+ // `call_participants` (keyed call_id), archived in+ // `call_record_participants` (keyed call_record_id). Ids are disjoint+ // across the two tables, so a single UNION ALL fetches all participants+ // in one round-trip; we then group by id in memory (avoids N+1).+ let mut active_ids: Vec<Uuid> = Vec::new();+ let mut archived_ids: Vec<Uuid> = Vec::new();+ for row in &rows {+ if row.is_active {+ active_ids.push(row.call_id);} else {- sqlx::query!(- r#"- SELECT user_id, joined_at, left_at- FROM call_record_participants- WHERE call_record_id = $1- ORDER BY joined_at ASC- "#,- row.call_id,- )- .fetch_all(&self.pool)- .await?- .into_iter()- .map(|p| CallRecordParticipant {+ archived_ids.push(row.call_id);+ }+ }++ let mut participants_by_call: HashMap<Uuid, Vec<CallRecordParticipant>> =+ HashMap::with_capacity(rows.len());++ for p in sqlx::query!(+ r#"+ SELECT call_id AS "id!", user_id AS "user_id!", joined_at AS "joined_at!", left_at+ FROM call_participants+ WHERE call_id = ANY($1)+ UNION ALL+ SELECT call_record_id AS "id!", user_id AS "user_id!", joined_at AS "joined_at!", left_at+ FROM call_record_participants+ WHERE call_record_id = ANY($2)+ ORDER BY 3 ASC+ "#,+ &active_ids,+ &archived_ids,+ )+ .fetch_all(&self.pool)+ .await?+ {+ participants_by_call+ .entry(p.id)+ .or_default()+ .push(CallRecordParticipant {user_id: p.user_id,joined_at: p.joined_at,left_at: p.left_at,- })- .collect()- };+ });+ }++ let mut records = Vec::with_capacity(rows.len());+ for row in rows {+ let participants = participants_by_call+ .remove(&row.call_id)+ .unwrap_or_default();records.push(CallRecord {call_id: row.call_id,