# LeadPilot — Index Strategy & Performance Tuning

**Document Version:** 1.0.0 (Phase 5 Database Architecture Lock)  
**Status:** Approved & Formally Recorded  

---

## 1. High-Frequency Query Index Strategy

Every composite index in LeadPilot is designed to satisfy the **Leftmost Prefix Rule** and optimize the most critical operational queries for sub-10ms response times:

```
+---------------------------------------------------------------------------------------------------------------+
| TABLE               | INDEX NAME                  | COLUMNS INDEXED                            | TARGET QUERY |
+---------------------+-----------------------------+--------------------------------------------+--------------+
| `leads`             | `idx_leads_ws_status_stage` | `(workspace_id, status, pipeline_stage_id)`| Inbox Filter |
| `leads`             | `idx_leads_ws_temp_score`   | `(workspace_id, temperature, ai_score)`   | Hot Leads    |
| `leads`             | `idx_leads_ws_next_followup`| `(workspace_id, next_followup_at)`         | Attention Hub|
| `leads`             | `idx_leads_ws_assigned`     | `(workspace_id, assigned_user_id)`         | My Leads Tab |
| `leads`             | `idx_leads_ws_created`      | `(workspace_id, created_at)`               | Ingest Stream|
| `leads`             | `idx_leads_normalized_email`| `(workspace_id, normalized_email)`         | Exact Dedup  |
| `leads`             | `idx_leads_normalized_phone`| `(workspace_id, normalized_phone)`         | Phone Dedup  |
| `follow_ups`        | `idx_followups_ws_due_status`| `(workspace_id, due_at, status)`          | Cron Scanner |
| `follow_ups`        | `idx_followups_lead`        | `(lead_id, status)`                        | Lead Detail  |
| `lead_activities`   | `idx_activities_lead_created`| `(lead_id, created_at)`                   | 360 Timeline |
| `notifications`     | `idx_notifications_user_read`| `(user_id, read_at)`                      | Bell Badge   |
| `settings`          | `settings_ws_key_unique`    | `(workspace_id, namespace, key)`           | Setting Read |
+---------------------------------------------------------------------------------------------------------------+
```

---

## 2. Full-Text Search Strategy

* **Inquiry & Name Search:** For searching across `first_name`, `last_name`, `company`, and `inquiry_text` in MySQL 8.0+:
  ```sql
  ALTER TABLE `leads` ADD FULLTEXT KEY `idx_leads_fulltext` (`first_name`, `last_name`, `company`, `inquiry_text`);
  ```
* **Performance:** Executes in $< 15\text{ms}$ on workspaces containing 20,000+ leads without external Elasticsearch dependencies.
