-- ============================================================================ -- COMPOUND INDICES - Indices compuestos para queries frecuentes -- Version: 1.0.0 -- Fecha: 2026-02-03 -- ============================================================================ -- Proposito: Optimizar queries que filtran por tenant_id + status + created_at -- Patron comun en listados paginados con filtros de estado -- ============================================================================ -- ============================================================================ -- SCHEMA: estimates -- ============================================================================ -- Indice compound para estimaciones (listado por estado y fecha) CREATE INDEX IF NOT EXISTS idx_estimaciones_tenant_status_created ON estimates.estimaciones(tenant_id, status, created_at DESC); -- Indice compound para generadores (listado por estado y fecha) CREATE INDEX IF NOT EXISTS idx_generadores_tenant_status_created ON estimates.generadores(tenant_id, status, created_at DESC); -- Indice compound para anticipos (busqueda por tipo y fecha) CREATE INDEX IF NOT EXISTS idx_anticipos_tenant_type_created ON estimates.anticipos(tenant_id, advance_type, created_at DESC); -- ============================================================================ -- SCHEMA: hse -- ============================================================================ -- Indice compound para incidentes (listado por estado y fecha) CREATE INDEX IF NOT EXISTS idx_incidentes_tenant_estado_created ON hse.incidentes(tenant_id, estado, created_at DESC); -- Indice compound para incidentes por tipo y gravedad CREATE INDEX IF NOT EXISTS idx_incidentes_tenant_tipo_gravedad ON hse.incidentes(tenant_id, tipo, gravedad); -- Indice compound para incidentes por fraccionamiento CREATE INDEX IF NOT EXISTS idx_incidentes_tenant_fracc_created ON hse.incidentes(tenant_id, fraccionamiento_id, created_at DESC); -- Indice compound para capacitaciones por tipo CREATE INDEX IF NOT EXISTS idx_capacitaciones_tenant_tipo ON hse.capacitaciones(tenant_id, tipo); -- ============================================================================ -- SCHEMA: construction -- ============================================================================ -- Indice compound para fraccionamientos (listado por estado y fecha) CREATE INDEX IF NOT EXISTS idx_fracc_tenant_status_created ON construction.fraccionamientos(tenant_id, status, created_at DESC); -- Indice compound para lotes (busqueda por estado y fraccionamiento) -- Nota: Necesitamos el fraccionamiento via manzana->etapa CREATE INDEX IF NOT EXISTS idx_lotes_tenant_status_created ON construction.lotes(tenant_id, status, created_at DESC); -- Indice compound para etapas (listado por estado) CREATE INDEX IF NOT EXISTS idx_etapas_tenant_status_created ON construction.etapas(tenant_id, status, created_at DESC); -- ============================================================================ -- SCHEMA: finance -- ============================================================================ -- Indice compound para cuentas bancarias (busqueda por estado) CREATE INDEX IF NOT EXISTS idx_bank_accounts_tenant_status_created ON finance.bank_accounts(tenant_id, status, created_at DESC); -- Indice compound para polizas contables (busqueda por tipo y fecha) CREATE INDEX IF NOT EXISTS idx_accounting_entries_tenant_type_date ON finance.accounting_entries(tenant_id, entry_type, entry_date DESC); -- Indice compound para CxC (busqueda por estado) CREATE INDEX IF NOT EXISTS idx_accounts_receivable_tenant_status_created ON finance.accounts_receivable(tenant_id, status, created_at DESC); -- Indice compound para CxP (busqueda por estado) CREATE INDEX IF NOT EXISTS idx_accounts_payable_tenant_status_created ON finance.accounts_payable(tenant_id, status, created_at DESC); -- ============================================================================ -- SCHEMA: hr -- ============================================================================ -- Indice compound para empleados (busqueda por estado) CREATE INDEX IF NOT EXISTS idx_employees_tenant_status_created ON hr.employees(tenant_id, status, created_at DESC); -- ============================================================================ -- COMENTARIOS -- ============================================================================ COMMENT ON INDEX estimates.idx_estimaciones_tenant_status_created IS 'Indice compound para listados de estimaciones filtrados por estado, ordenados por fecha'; COMMENT ON INDEX hse.idx_incidentes_tenant_estado_created IS 'Indice compound para listados de incidentes filtrados por estado, ordenados por fecha'; COMMENT ON INDEX construction.idx_fracc_tenant_status_created IS 'Indice compound para listados de fraccionamientos filtrados por estado, ordenados por fecha'; -- ============================================================================ -- FIN - Compound Indices -- ============================================================================