fix: simplify JSONB extraction - remove redundant operators

- Replace 4 LIKE checks with 2 in build_custom_field_filter
- Simplify CASE blocks in migration trigger functions
- ->> operator always returns text, no need for -> + ::text fallback
- Performance improvement: 50% fewer LIKE operations
This commit is contained in:
Moritz 2025-12-11 13:33:52 +01:00
parent 014ef04853
commit 265e976d94
Signed by: moritz
GPG key ID: 1020A035E5DD0824
2 changed files with 11 additions and 47 deletions

View file

@ -529,12 +529,11 @@ defmodule Mv.Membership.Member do
# Builds search filter for custom field values using LIKE on JSONB
# Note: LIKE on JSONB is not index-optimized, may be slow with many custom fields
# This is a fallback for substring matching in custom fields (e.g., phone numbers)
# Uses ->> operator which always returns TEXT directly (no need for -> + ::text fallback)
defp build_custom_field_filter(pattern) do
expr(
fragment(
"EXISTS (SELECT 1 FROM custom_field_values WHERE member_id = id AND (value->>'_union_value' LIKE ? OR value->>'value' LIKE ? OR (value->'_union_value')::text LIKE ? OR (value->'value')::text LIKE ?))",
^pattern,
^pattern,
"EXISTS (SELECT 1 FROM custom_field_values WHERE member_id = id AND (value->>'_union_value' LIKE ? OR value->>'value' LIKE ?))",
^pattern,
^pattern
)