-- Migration: Add covering indexes for task detail queries
-- Date: 2026-01-25
--
-- This migration adds covering indexes to optimize queries by including
-- all needed columns in the index, avoiding table lookups.
--
-- Covering indexes allow PostgreSQL to perform index-only scans,
-- getting all data directly from the index without accessing the table.

-- Covering index for subtasks query
-- Includes all columns needed for subtasks selection to avoid table lookups
CREATE INDEX IF NOT EXISTS idx_tasks_parent_deleted_covering 
ON tasks(parent_task_id, deleted, id) 
INCLUDE (name, completed, last_completed_at, reward_message, progression_base)
WHERE deleted = FALSE;

-- Covering index for wishlist name lookup
-- Includes name and deleted flag for quick lookup without table access
CREATE INDEX IF NOT EXISTS idx_wishlist_items_id_deleted_covering 
ON wishlist_items(id, deleted) 
INCLUDE (name)
WHERE deleted = FALSE;

COMMENT ON INDEX idx_tasks_parent_deleted_covering IS 'Covering index for subtasks query - includes all selected columns to avoid table lookups. Enables index-only scans for better performance.';
COMMENT ON INDEX idx_wishlist_items_id_deleted_covering IS 'Covering index for wishlist name lookup - includes name to avoid table lookup. Enables index-only scans for better performance.';
