towerops/priv/repo/migrations/20260115011502_add_alerts_equipment_indexes.exs
Graham McIntire 54b08bfb18 Add database indexes and optimize SNMP polling
- Added partial indexes for alerts and SNMP equipment queries
- Fixed missing agent_assignments association in Equipment schema
- Fixed missing preloads to avoid N+1 queries
- Optimized Rust agent SNMP credential field access
2026-01-15 07:28:14 -06:00

28 lines
1.4 KiB
Elixir

defmodule Towerops.Repo.Migrations.AddAlertsEquipmentIndexes do
use Ecto.Migration
def change do
# Composite index for active alerts queries - filters by type and resolved status
# Used by list_organization_active_alerts/1 and get_active_alert/2
# Partial index only on unresolved alerts for efficiency
create_if_not_exists index(:alerts, [:equipment_id, :alert_type, :resolved_at],
where: "resolved_at IS NULL",
name: :idx_alerts_equipment_type_unresolved
)
# Composite index for SNMP-enabled equipment polling queries
# Used by list_snmp_enabled_equipment/0 and agent polling target queries
# Partial index only on SNMP-enabled equipment
create_if_not_exists index(:equipment, [:snmp_enabled, :last_snmp_poll_at],
where: "snmp_enabled = true",
name: :idx_equipment_snmp_poll
)
# Composite index for monitoring-enabled equipment with SNMP
# Covers common query pattern: WHERE snmp_enabled AND monitoring_enabled
create_if_not_exists index(:equipment, [:snmp_enabled, :monitoring_enabled, :site_id],
where: "snmp_enabled = true AND monitoring_enabled = true",
name: :idx_equipment_snmp_monitoring_site
)
end
end