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(crm): constrain team_id on crm_domains in the domain lookup (#6330)
Will Hutchinson02a3d6e7310d
diff --git a/.sqlx/query-ebd8690a2a7068b485816697c3d6660c82f616f8f3456a6137b872043404d282.json b/.sqlx/query-d10c5e6c2a50c028d658c0344e9f99b9fc874cd804534bc2b7bfd8a147379a08.jsonsimilarity index 69%rename from .sqlx/query-ebd8690a2a7068b485816697c3d6660c82f616f8f3456a6137b872043404d282.jsonrename to .sqlx/query-d10c5e6c2a50c028d658c0344e9f99b9fc874cd804534bc2b7bfd8a147379a08.jsonindex d7dd77cdc8..4bd7476abc 100644--- a/.sqlx/query-ebd8690a2a7068b485816697c3d6660c82f616f8f3456a6137b872043404d282.json+++ b/.sqlx/query-d10c5e6c2a50c028d658c0344e9f99b9fc874cd804534bc2b7bfd8a147379a08.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT c.id, c.hidden\n FROM crm_companies c\n JOIN crm_domains d ON d.company_id = c.id\n WHERE c.team_id = $1\n AND LOWER(d.domain) = $2\n LIMIT 1\n ",+ "query": "\n SELECT c.id, c.hidden\n FROM crm_companies c\n JOIN crm_domains d ON d.company_id = c.id\n WHERE c.team_id = $1\n AND d.team_id = $1\n AND LOWER(d.domain) = $2\n LIMIT 1\n ","describe": {"columns": [{@@ -25,5 +25,5 @@false]},- "hash": "ebd8690a2a7068b485816697c3d6660c82f616f8f3456a6137b872043404d282"+ "hash": "d10c5e6c2a50c028d658c0344e9f99b9fc874cd804534bc2b7bfd8a147379a08"}diff --git a/crates/crm/src/outbound/companies_repo.rs b/crates/crm/src/outbound/companies_repo.rsindex 18a950fedc..903e7f6b06 100644--- a/crates/crm/src/outbound/companies_repo.rs+++ b/crates/crm/src/outbound/companies_repo.rs@@ -104,6 +104,15 @@ impl CompaniesRepositoryImpl {/// Look up the company owning `(team, lower(domain))`. Returns/// `(id, hidden)`; `hidden` is read so new contacts can inherit it.+ ///+ /// `d.team_id` is filtered as well as `c.team_id`: the two are invariably+ /// equal (`crm_domains.team_id` is NOT NULL and every insert writes the+ /// owning company's team, which is never updated), but only the predicate+ /// on `crm_domains` lets the planner probe+ /// `crm_domains_team_id_lower_domain_unique (team_id, LOWER(domain))`.+ /// Without it the leading index column is unconstrained, so the lookup+ /// degrades to a full scan of `crm_domains` joined against every company+ /// in the team — while holding this team+domain's advisory lock.async fn find_company_by_domain(tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,team_id: &uuid::Uuid,@@ -115,6 +124,7 @@ impl CompaniesRepositoryImpl {FROM crm_companies cJOIN crm_domains d ON d.company_id = c.idWHERE c.team_id = $1+ AND d.team_id = $1AND LOWER(d.domain) = $2LIMIT 1"#,