-- Migration: Add Composite Indexes for Query Optimization
-- Date: 2026-01-21
-- Description: Adds multi-column indexes to optimize common query patterns in the instance query service
--
-- Performance Impact:
-- - Reduces query execution time for filtered instance searches
-- - Optimizes JOIN operations between instance_types, pricing, and regions tables
-- - Improves ORDER BY performance on price-sorted results
--
-- SQLite/Cloudflare D1 Compatible

-- ============================================================
-- Composite Indexes: Query Performance Optimization
-- ============================================================

-- Composite index for instance_types filtering and queries
-- Optimizes: Main instance query with provider, family, and spec filters
-- Query Pattern: WHERE p.name = ? AND it.instance_family = ? AND it.vcpu >= ? AND it.memory_mb >= ?
-- Benefit: Reduces full table scan by enabling index-based filtering on provider, family, and specs
CREATE INDEX IF NOT EXISTS idx_instance_types_provider_family_specs
ON instance_types(provider_id, instance_family, vcpu, memory_mb);

-- Composite index for pricing queries with sorting
-- Optimizes: Main pricing query with JOIN on instance_types and regions, sorted by price
-- Query Pattern: JOIN pricing pr ON pr.instance_type_id = it.id JOIN regions r ON pr.region_id = r.id ORDER BY pr.hourly_price
-- Benefit: Enables efficient JOIN filtering and ORDER BY without separate sort operation
CREATE INDEX IF NOT EXISTS idx_pricing_instance_region_price
ON pricing(instance_type_id, region_id, hourly_price);

-- Composite index for region lookups by provider
-- Optimizes: Region filtering in main instance query
-- Query Pattern: WHERE p.name = ? AND r.region_code = ?
-- Benefit: Fast region lookup by provider and region code combination (replaces sequential scan)
CREATE INDEX IF NOT EXISTS idx_regions_provider_code
ON regions(provider_id, region_code);

-- ============================================================
-- Index Usage Notes
-- ============================================================
--
-- 1. idx_instance_types_provider_family_specs:
--    - Used when filtering instances by provider + family + specs
--    - Supports range queries on vcpu and memory_mb (leftmost prefix rule)
--    - Example: GET /api/instances?provider=linode&family=compute&min_vcpu=4&min_memory=8192
--
-- 2. idx_pricing_instance_region_price:
--    - Critical for JOIN operations in main query (lines 186-187 in query.ts)
--    - Enables sorted results without additional sort step
--    - Example: Main query with ORDER BY pr.hourly_price (most common use case)
--
-- 3. idx_regions_provider_code:
--    - Replaces two separate index lookups with single composite lookup
--    - Unique constraint (provider_id, region_code) already exists, but this index optimizes reads
--    - Example: GET /api/instances?provider=vultr&region_code=ewr
--
-- ============================================================
-- Rollback
-- ============================================================
--
-- To rollback this migration:
-- DROP INDEX IF EXISTS idx_instance_types_provider_family_specs;
-- DROP INDEX IF EXISTS idx_pricing_instance_region_price;
-- DROP INDEX IF EXISTS idx_regions_provider_code;
