|
| 1 | +-- Query: findMembersByVerifiedUsernames |
| 2 | +-- |
| 3 | +-- Joins memberIdentities on (platform, lower(value)) with WHERE: |
| 4 | +-- verified = true AND type = 'username' AND deletedAt IS NULL |
| 5 | +-- |
| 6 | +-- The existing idx_memberIdentities_platform_type_lower_value_memberId is |
| 7 | +-- missing verified = true in its partial condition, so PostgreSQL must |
| 8 | +-- heap-fetch every row to recheck verified, which is expensive when there |
| 9 | +-- are many unverified identities. |
| 10 | +-- |
| 11 | +-- This index adds verified = true to the partial condition and includes memberId |
| 12 | +-- so the join to members can read memberId from the index without a heap fetch. |
| 13 | +CREATE INDEX CONCURRENTLY IF NOT EXISTS "idx_memberIdentities_verified_username_platform_lower_value" |
| 14 | + ON "memberIdentities" (platform, lower(value), "memberId") |
| 15 | + WHERE verified = true |
| 16 | + AND type = 'username' |
| 17 | + AND "deletedAt" IS NULL; |
| 18 | + |
| 19 | +-- Query: findMembersByVerifiedEmails |
| 20 | +-- |
| 21 | +-- Joins memberIdentities on lower(value) with WHERE: |
| 22 | +-- verified = true AND type = 'email' AND deletedAt IS NULL |
| 23 | +-- |
| 24 | +-- The existing idx_memberIdentities_verified_email_lower_value has the right |
| 25 | +-- partial condition but only stores lower(value) — no memberId. Every matched |
| 26 | +-- index entry requires a heap fetch to get memberId for the join to members. |
| 27 | +-- Under concurrent insert/update load, those heap fetches queue behind buffer |
| 28 | +-- pin locks, causing multi-second delays even for small inputs. |
| 29 | +-- |
| 30 | +-- This index adds memberId so the join to members can proceed without |
| 31 | +-- touching the memberIdentities heap pages. |
| 32 | +CREATE INDEX CONCURRENTLY IF NOT EXISTS "idx_memberIdentities_verified_email_lower_value_memberid" |
| 33 | + ON "memberIdentities" (lower(value), "memberId") |
| 34 | + WHERE verified = true |
| 35 | + AND type = 'email' |
| 36 | + AND "deletedAt" IS NULL; |
0 commit comments