-- ============================================
-- Migration: 003_add_indexes.sql
-- Purpose: 添加性能优化索引
-- ============================================

-- 1. 启用 pg_trgm 扩展 (支持模糊搜索索引)
CREATE EXTENSION IF NOT EXISTS pg_trgm;

-- 2. 材质名称模糊搜索索引 (GIN 索引，支持 ILIKE '%keyword%')
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_materials_name_trgm
ON materials USING gin (name gin_trgm_ops);

-- 3. 材质创建时间索引 (用于排序)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_materials_created_at
ON materials(created_at DESC);

-- 4. 材质更新时间索引
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_materials_updated_at
ON materials(updated_at DESC);

-- 注意: 如果使用了分区表 (002_partition_bindings.sql)，
-- 以下索引已在分区迁移中创建，可跳过

-- 5. binding 表 group_id + material_id 复合索引 (优化批量查询)
-- CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_bindings_group_material
-- ON material_bindings(group_id, material_id);
