Files
honeyDueAPI/migrations/006_performance_indexes.up.sql
Trey t 0cf64cfb0c Add performance optimizations and database indexes
Database Indexes (migrations 006-009):
- Add case-insensitive indexes for auth lookups (email, username)
- Add composite indexes for task kanban queries
- Add indexes for notification, document, and completion queries
- Add unique index for active share codes
- Remove redundant idx_share_code_active and idx_notification_user_sent

Repository Optimizations:
- Add FindResidenceIDsByUser() lightweight method (IDs only, no preloads)
- Optimize GetResidenceUsers() with single UNION query (was 2 queries)
- Optimize kanban completion preloads to minimal columns (id, task_id, completed_at)

Service Optimizations:
- Remove Category/Priority/Frequency preloads from task queries
- Remove summary calculations from CRUD responses (client calculates)
- Use lightweight FindResidenceIDsByUser() instead of full FindByUser()

These changes reduce database load and response times for common operations.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-14 01:06:08 -06:00

76 lines
3.0 KiB
SQL

-- 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;