erp-construccion-database-v2/schemas/11-compound-indices.sql
Adrian Flores Cortes dcdc15e162 [ST-P2-002,ST-P2-003] feat: Add compound indices and JSONB schema documentation
- 11-compound-indices.sql: 14 compound indices for common queries
- docs/JSONB-SCHEMAS.md: 50+ TypeScript interfaces documenting JSONB fields
- DDL-EXECUTION-ORDER.md: Updated to include compound indices step

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-03 07:39:14 -06:00

107 lines
4.9 KiB
SQL

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