-- Performance optimization indexes -- Migration: 006_performance_indexes -- ===================================================== -- CRITICAL: Case-insensitive indexes for auth lookups -- ===================================================== -- These eliminate full table scans on login/registration CREATE INDEX IF NOT EXISTS idx_user_email_lower ON auth_user (LOWER(email)); CREATE INDEX IF NOT EXISTS idx_user_username_lower ON auth_user (LOWER(username)); CREATE INDEX IF NOT EXISTS idx_admin_email_lower ON admin_users (LOWER(email)); -- ===================================================== -- HIGH PRIORITY: Composite indexes for common queries -- ===================================================== -- Tasks: Most common query pattern is by residence with status filters CREATE INDEX IF NOT EXISTS idx_task_residence_status ON task_task (residence_id, is_cancelled, is_archived); -- Tasks: For kanban board queries (active tasks by residence) CREATE INDEX IF NOT EXISTS idx_task_residence_active ON task_task (residence_id, is_archived, in_progress) WHERE is_cancelled = false; -- Tasks: For overdue queries (next_due_date lookups) CREATE INDEX IF NOT EXISTS idx_task_next_due_date ON task_task (next_due_date) WHERE is_cancelled = false AND is_archived = false AND next_due_date IS NOT NULL; -- Notifications: Queried constantly for unread count CREATE INDEX IF NOT EXISTS idx_notification_user_read ON notifications_notification (user_id, read); -- Notifications: For pending notification worker CREATE INDEX IF NOT EXISTS idx_notification_user_sent ON notifications_notification (user_id, sent); -- Notifications: Task-based lookups CREATE INDEX IF NOT EXISTS idx_notification_task ON notifications_notification (task_id) WHERE task_id IS NOT NULL; -- Documents: Warranty expiry queries CREATE INDEX IF NOT EXISTS idx_document_residence_active_type ON task_document (residence_id, is_active, document_type); -- Documents: Expiring warranties lookup CREATE INDEX IF NOT EXISTS idx_document_expiry_active ON task_document (expiry_date, is_active) WHERE document_type = 'warranty' AND is_active = true; -- ===================================================== -- MEDIUM PRIORITY: Foreign key indexes -- ===================================================== -- Contractor: Query by creator (user's personal contractors) CREATE INDEX IF NOT EXISTS idx_contractor_created_by ON task_contractor (created_by_id); -- Task completions: Query by task CREATE INDEX IF NOT EXISTS idx_completion_task ON task_taskcompletion (task_id); -- Task completions: Query by user who completed CREATE INDEX IF NOT EXISTS idx_completion_completed_by ON task_taskcompletion (completed_by_id); -- Residence members: Query by user CREATE INDEX IF NOT EXISTS idx_residence_member_user ON residence_residencemember (user_id); -- Share codes: Active code lookups CREATE INDEX IF NOT EXISTS idx_share_code_active ON residence_residencesharecode (code, is_active) WHERE is_active = true;