gigafibre-fsm/scripts/migration/add_performance_indexes.sql
louispaulb 4693bcf60c feat: telephony UI, performance indexes, Twilio softphone, lazy-load invoices
- Add PostgreSQL performance indexes migration script (1000x faster queries)
  Sales Invoice: 1,248ms → 28ms, Payment Entry: 443ms → 31ms
  Indexes on customer/party columns for all major tables
- Disable 3CX poller (PBX_ENABLED flag, using Twilio instead)
- Add TelephonyPage: full CRUD UI for Routr/Fonoster resources
  (trunks, agents, credentials, numbers, domains, peers)
- Add PhoneModal + usePhone composable (Twilio WebRTC softphone)
- Lazy-load invoices/payments (initial 5, expand on demand)
- Parallelize all API calls in ClientDetailPage (no waterfall)
- Add targo-hub service (SSE relay, SMS, voice, telephony API)
- Customer portal: invoice detail, ticket detail, messages pages
- Remove dead Ollama nginx upstream

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-02 13:59:59 -04:00

64 lines
2.5 KiB
SQL

-- ============================================================
-- Performance indexes for ERPNext PostgreSQL
-- ============================================================
-- Problem: ERPNext v16 PostgreSQL migration does not create
-- indexes on 'customer' / 'party' columns. This causes full
-- table scans on tables with 100k-1M+ rows, resulting in
-- 5+ second page loads in Ops client detail page.
--
-- Impact: Sales Invoice query dropped from 1,248ms to 28ms
-- (EXPLAIN ANALYZE: 378ms → 0.36ms, 1000x improvement)
--
-- Run: docker exec erpnext-db-1 psql -U postgres -d <dbname> -f /path/to/this.sql
-- Or run each CREATE INDEX statement individually.
--
-- All indexes use CONCURRENTLY to avoid locking production tables.
-- Run each statement in its own transaction (outside BEGIN/COMMIT).
-- ============================================================
-- Customer lookup on Sales Invoice (630k+ rows)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_si_customer
ON "tabSales Invoice" (customer);
-- Composite index for sorted invoice listing per customer
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_si_posting
ON "tabSales Invoice" (customer, posting_date DESC);
-- Party lookup on Payment Entry (344k+ rows)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_pe_party
ON "tabPayment Entry" (party);
-- Composite index for sorted payment listing per party
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_pe_posting
ON "tabPayment Entry" (party, posting_date DESC);
-- Reference lookup on Comment (1.07M+ rows)
-- Used by: ticket comments, invoice notes, customer memos
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_comment_ref
ON "tabComment" (reference_doctype, reference_name);
-- Customer lookup on Issue (243k+ rows)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_issue_customer
ON "tabIssue" (customer);
-- Customer lookup on Service Location (17k+ rows)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_sl_customer
ON "tabService Location" (customer);
-- Customer lookup on Service Subscription (40k+ rows)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_ss_customer
ON "tabService Subscription" (customer);
-- Customer lookup on Service Equipment (10k+ rows)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_se_customer
ON "tabService Equipment" (customer);
-- Update planner statistics after index creation
ANALYZE "tabSales Invoice";
ANALYZE "tabPayment Entry";
ANALYZE "tabComment";
ANALYZE "tabIssue";
ANALYZE "tabService Location";
ANALYZE "tabService Subscription";
ANALYZE "tabService Equipment";