mirror of
https://github.com/fusionpbx/fusionpbx.git
synced 2025-12-30 09:03:49 +00:00
Replaced unindexable CONCAT() with COALESCE(col, '') || ... and refactored dialplan lookup into a CTE with UNION to split destination-based and public-context paths. CONCAT() is marked as STABLE in PostgreSQL and cannot be used in functional indexes, which forced sequential scans during dialplan lookups. To enable future indexing and improve query optimizability, we replace all CONCAT(a, b, c) calls with (COALESCE(a, '') || COALESCE(b, '') || COALESCE(c, '')), which is functionally equivalent for text columns (treating NULLs as empty) and composed only of IMMUTABLE operations. Additionally, the query was refactored using a CTE with UNION to decompose a complex top-level OR condition into two independent branches: 1. Dialplans linked to matching destinations. 2. Public dialplans with domain_uuid IS NULL. This structure allows the planner to optimize each path separately, avoid full-table scans, and leverage primary key lookups efficiently - even without additional indexes. On a production dataset with 3kk records in v_dialplans, this change reduced dialplans query latency from ~1.5s to ~37ms (40.5x faster), with further gains possible via expression indexes.